scan.c 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * cfg80211 scan result handling
  4. *
  5. * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
  6. * Copyright 2013-2014 Intel Mobile Communications GmbH
  7. * Copyright 2016 Intel Deutschland GmbH
  8. * Copyright (C) 2018-2024 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 <kunit/visibility.h>
  24. #include "core.h"
  25. #include "nl80211.h"
  26. #include "wext-compat.h"
  27. #include "rdev-ops.h"
  28. /**
  29. * DOC: BSS tree/list structure
  30. *
  31. * At the top level, the BSS list is kept in both a list in each
  32. * registered device (@bss_list) as well as an RB-tree for faster
  33. * lookup. In the RB-tree, entries can be looked up using their
  34. * channel, MESHID, MESHCONF (for MBSSes) or channel, BSSID, SSID
  35. * for other BSSes.
  36. *
  37. * Due to the possibility of hidden SSIDs, there's a second level
  38. * structure, the "hidden_list" and "hidden_beacon_bss" pointer.
  39. * The hidden_list connects all BSSes belonging to a single AP
  40. * that has a hidden SSID, and connects beacon and probe response
  41. * entries. For a probe response entry for a hidden SSID, the
  42. * hidden_beacon_bss pointer points to the BSS struct holding the
  43. * beacon's information.
  44. *
  45. * Reference counting is done for all these references except for
  46. * the hidden_list, so that a beacon BSS struct that is otherwise
  47. * not referenced has one reference for being on the bss_list and
  48. * one for each probe response entry that points to it using the
  49. * hidden_beacon_bss pointer. When a BSS struct that has such a
  50. * pointer is get/put, the refcount update is also propagated to
  51. * the referenced struct, this ensure that it cannot get removed
  52. * while somebody is using the probe response version.
  53. *
  54. * Note that the hidden_beacon_bss pointer never changes, due to
  55. * the reference counting. Therefore, no locking is needed for
  56. * it.
  57. *
  58. * Also note that the hidden_beacon_bss pointer is only relevant
  59. * if the driver uses something other than the IEs, e.g. private
  60. * data stored in the BSS struct, since the beacon IEs are
  61. * also linked into the probe response struct.
  62. */
  63. /*
  64. * Limit the number of BSS entries stored in mac80211. Each one is
  65. * a bit over 4k at most, so this limits to roughly 4-5M of memory.
  66. * If somebody wants to really attack this though, they'd likely
  67. * use small beacons, and only one type of frame, limiting each of
  68. * the entries to a much smaller size (in order to generate more
  69. * entries in total, so overhead is bigger.)
  70. */
  71. static int bss_entries_limit = 1000;
  72. module_param(bss_entries_limit, int, 0644);
  73. MODULE_PARM_DESC(bss_entries_limit,
  74. "limit to number of scan BSS entries (per wiphy, default 1000)");
  75. #define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
  76. static void bss_free(struct cfg80211_internal_bss *bss)
  77. {
  78. struct cfg80211_bss_ies *ies;
  79. if (WARN_ON(atomic_read(&bss->hold)))
  80. return;
  81. ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
  82. if (ies && !bss->pub.hidden_beacon_bss)
  83. kfree_rcu(ies, rcu_head);
  84. ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);
  85. if (ies)
  86. kfree_rcu(ies, rcu_head);
  87. /*
  88. * This happens when the module is removed, it doesn't
  89. * really matter any more save for completeness
  90. */
  91. if (!list_empty(&bss->hidden_list))
  92. list_del(&bss->hidden_list);
  93. kfree(bss);
  94. }
  95. static inline void bss_ref_get(struct cfg80211_registered_device *rdev,
  96. struct cfg80211_internal_bss *bss)
  97. {
  98. lockdep_assert_held(&rdev->bss_lock);
  99. bss->refcount++;
  100. if (bss->pub.hidden_beacon_bss)
  101. bss_from_pub(bss->pub.hidden_beacon_bss)->refcount++;
  102. if (bss->pub.transmitted_bss)
  103. bss_from_pub(bss->pub.transmitted_bss)->refcount++;
  104. }
  105. static inline void bss_ref_put(struct cfg80211_registered_device *rdev,
  106. struct cfg80211_internal_bss *bss)
  107. {
  108. lockdep_assert_held(&rdev->bss_lock);
  109. if (bss->pub.hidden_beacon_bss) {
  110. struct cfg80211_internal_bss *hbss;
  111. hbss = bss_from_pub(bss->pub.hidden_beacon_bss);
  112. hbss->refcount--;
  113. if (hbss->refcount == 0)
  114. bss_free(hbss);
  115. }
  116. if (bss->pub.transmitted_bss) {
  117. struct cfg80211_internal_bss *tbss;
  118. tbss = bss_from_pub(bss->pub.transmitted_bss);
  119. tbss->refcount--;
  120. if (tbss->refcount == 0)
  121. bss_free(tbss);
  122. }
  123. bss->refcount--;
  124. if (bss->refcount == 0)
  125. bss_free(bss);
  126. }
  127. static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev,
  128. struct cfg80211_internal_bss *bss)
  129. {
  130. lockdep_assert_held(&rdev->bss_lock);
  131. if (!list_empty(&bss->hidden_list)) {
  132. /*
  133. * don't remove the beacon entry if it has
  134. * probe responses associated with it
  135. */
  136. if (!bss->pub.hidden_beacon_bss)
  137. return false;
  138. /*
  139. * if it's a probe response entry break its
  140. * link to the other entries in the group
  141. */
  142. list_del_init(&bss->hidden_list);
  143. }
  144. list_del_init(&bss->list);
  145. list_del_init(&bss->pub.nontrans_list);
  146. rb_erase(&bss->rbn, &rdev->bss_tree);
  147. rdev->bss_entries--;
  148. WARN_ONCE((rdev->bss_entries == 0) ^ list_empty(&rdev->bss_list),
  149. "rdev bss entries[%d]/list[empty:%d] corruption\n",
  150. rdev->bss_entries, list_empty(&rdev->bss_list));
  151. bss_ref_put(rdev, bss);
  152. return true;
  153. }
  154. bool cfg80211_is_element_inherited(const struct element *elem,
  155. const struct element *non_inherit_elem)
  156. {
  157. u8 id_len, ext_id_len, i, loop_len, id;
  158. const u8 *list;
  159. if (elem->id == WLAN_EID_MULTIPLE_BSSID)
  160. return false;
  161. if (elem->id == WLAN_EID_EXTENSION && elem->datalen > 1 &&
  162. elem->data[0] == WLAN_EID_EXT_EHT_MULTI_LINK)
  163. return false;
  164. if (!non_inherit_elem || non_inherit_elem->datalen < 2)
  165. return true;
  166. /*
  167. * non inheritance element format is:
  168. * ext ID (56) | IDs list len | list | extension IDs list len | list
  169. * Both lists are optional. Both lengths are mandatory.
  170. * This means valid length is:
  171. * elem_len = 1 (extension ID) + 2 (list len fields) + list lengths
  172. */
  173. id_len = non_inherit_elem->data[1];
  174. if (non_inherit_elem->datalen < 3 + id_len)
  175. return true;
  176. ext_id_len = non_inherit_elem->data[2 + id_len];
  177. if (non_inherit_elem->datalen < 3 + id_len + ext_id_len)
  178. return true;
  179. if (elem->id == WLAN_EID_EXTENSION) {
  180. if (!ext_id_len)
  181. return true;
  182. loop_len = ext_id_len;
  183. list = &non_inherit_elem->data[3 + id_len];
  184. id = elem->data[0];
  185. } else {
  186. if (!id_len)
  187. return true;
  188. loop_len = id_len;
  189. list = &non_inherit_elem->data[2];
  190. id = elem->id;
  191. }
  192. for (i = 0; i < loop_len; i++) {
  193. if (list[i] == id)
  194. return false;
  195. }
  196. return true;
  197. }
  198. EXPORT_SYMBOL(cfg80211_is_element_inherited);
  199. static size_t cfg80211_copy_elem_with_frags(const struct element *elem,
  200. const u8 *ie, size_t ie_len,
  201. u8 **pos, u8 *buf, size_t buf_len)
  202. {
  203. if (WARN_ON((u8 *)elem < ie || elem->data > ie + ie_len ||
  204. elem->data + elem->datalen > ie + ie_len))
  205. return 0;
  206. if (elem->datalen + 2 > buf + buf_len - *pos)
  207. return 0;
  208. memcpy(*pos, elem, elem->datalen + 2);
  209. *pos += elem->datalen + 2;
  210. /* Finish if it is not fragmented */
  211. if (elem->datalen != 255)
  212. return *pos - buf;
  213. ie_len = ie + ie_len - elem->data - elem->datalen;
  214. ie = (const u8 *)elem->data + elem->datalen;
  215. for_each_element(elem, ie, ie_len) {
  216. if (elem->id != WLAN_EID_FRAGMENT)
  217. break;
  218. if (elem->datalen + 2 > buf + buf_len - *pos)
  219. return 0;
  220. memcpy(*pos, elem, elem->datalen + 2);
  221. *pos += elem->datalen + 2;
  222. if (elem->datalen != 255)
  223. break;
  224. }
  225. return *pos - buf;
  226. }
  227. VISIBLE_IF_CFG80211_KUNIT size_t
  228. cfg80211_gen_new_ie(const u8 *ie, size_t ielen,
  229. const u8 *subie, size_t subie_len,
  230. u8 *new_ie, size_t new_ie_len)
  231. {
  232. const struct element *non_inherit_elem, *parent, *sub;
  233. u8 *pos = new_ie;
  234. u8 id, ext_id;
  235. unsigned int match_len;
  236. non_inherit_elem = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
  237. subie, subie_len);
  238. /* We copy the elements one by one from the parent to the generated
  239. * elements.
  240. * If they are not inherited (included in subie or in the non
  241. * inheritance element), then we copy all occurrences the first time
  242. * we see this element type.
  243. */
  244. for_each_element(parent, ie, ielen) {
  245. if (parent->id == WLAN_EID_FRAGMENT)
  246. continue;
  247. if (parent->id == WLAN_EID_EXTENSION) {
  248. if (parent->datalen < 1)
  249. continue;
  250. id = WLAN_EID_EXTENSION;
  251. ext_id = parent->data[0];
  252. match_len = 1;
  253. } else {
  254. id = parent->id;
  255. match_len = 0;
  256. }
  257. /* Find first occurrence in subie */
  258. sub = cfg80211_find_elem_match(id, subie, subie_len,
  259. &ext_id, match_len, 0);
  260. /* Copy from parent if not in subie and inherited */
  261. if (!sub &&
  262. cfg80211_is_element_inherited(parent, non_inherit_elem)) {
  263. if (!cfg80211_copy_elem_with_frags(parent,
  264. ie, ielen,
  265. &pos, new_ie,
  266. new_ie_len))
  267. return 0;
  268. continue;
  269. }
  270. /* Already copied if an earlier element had the same type */
  271. if (cfg80211_find_elem_match(id, ie, (u8 *)parent - ie,
  272. &ext_id, match_len, 0))
  273. continue;
  274. /* Not inheriting, copy all similar elements from subie */
  275. while (sub) {
  276. if (!cfg80211_copy_elem_with_frags(sub,
  277. subie, subie_len,
  278. &pos, new_ie,
  279. new_ie_len))
  280. return 0;
  281. sub = cfg80211_find_elem_match(id,
  282. sub->data + sub->datalen,
  283. subie_len + subie -
  284. (sub->data +
  285. sub->datalen),
  286. &ext_id, match_len, 0);
  287. }
  288. }
  289. /* The above misses elements that are included in subie but not in the
  290. * parent, so do a pass over subie and append those.
  291. * Skip the non-tx BSSID caps and non-inheritance element.
  292. */
  293. for_each_element(sub, subie, subie_len) {
  294. if (sub->id == WLAN_EID_NON_TX_BSSID_CAP)
  295. continue;
  296. if (sub->id == WLAN_EID_FRAGMENT)
  297. continue;
  298. if (sub->id == WLAN_EID_EXTENSION) {
  299. if (sub->datalen < 1)
  300. continue;
  301. id = WLAN_EID_EXTENSION;
  302. ext_id = sub->data[0];
  303. match_len = 1;
  304. if (ext_id == WLAN_EID_EXT_NON_INHERITANCE)
  305. continue;
  306. } else {
  307. id = sub->id;
  308. match_len = 0;
  309. }
  310. /* Processed if one was included in the parent */
  311. if (cfg80211_find_elem_match(id, ie, ielen,
  312. &ext_id, match_len, 0))
  313. continue;
  314. if (!cfg80211_copy_elem_with_frags(sub, subie, subie_len,
  315. &pos, new_ie, new_ie_len))
  316. return 0;
  317. }
  318. return pos - new_ie;
  319. }
  320. EXPORT_SYMBOL_IF_CFG80211_KUNIT(cfg80211_gen_new_ie);
  321. static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
  322. const u8 *ssid, size_t ssid_len)
  323. {
  324. const struct cfg80211_bss_ies *ies;
  325. const struct element *ssid_elem;
  326. if (bssid && !ether_addr_equal(a->bssid, bssid))
  327. return false;
  328. if (!ssid)
  329. return true;
  330. ies = rcu_access_pointer(a->ies);
  331. if (!ies)
  332. return false;
  333. ssid_elem = cfg80211_find_elem(WLAN_EID_SSID, ies->data, ies->len);
  334. if (!ssid_elem)
  335. return false;
  336. if (ssid_elem->datalen != ssid_len)
  337. return false;
  338. return memcmp(ssid_elem->data, ssid, ssid_len) == 0;
  339. }
  340. static int
  341. cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
  342. struct cfg80211_bss *nontrans_bss)
  343. {
  344. const struct element *ssid_elem;
  345. struct cfg80211_bss *bss = NULL;
  346. rcu_read_lock();
  347. ssid_elem = ieee80211_bss_get_elem(nontrans_bss, WLAN_EID_SSID);
  348. if (!ssid_elem) {
  349. rcu_read_unlock();
  350. return -EINVAL;
  351. }
  352. /* check if nontrans_bss is in the list */
  353. list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {
  354. if (is_bss(bss, nontrans_bss->bssid, ssid_elem->data,
  355. ssid_elem->datalen)) {
  356. rcu_read_unlock();
  357. return 0;
  358. }
  359. }
  360. rcu_read_unlock();
  361. /*
  362. * This is a bit weird - it's not on the list, but already on another
  363. * one! The only way that could happen is if there's some BSSID/SSID
  364. * shared by multiple APs in their multi-BSSID profiles, potentially
  365. * with hidden SSID mixed in ... ignore it.
  366. */
  367. if (!list_empty(&nontrans_bss->nontrans_list))
  368. return -EINVAL;
  369. /* add to the list */
  370. list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);
  371. return 0;
  372. }
  373. static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
  374. unsigned long expire_time)
  375. {
  376. struct cfg80211_internal_bss *bss, *tmp;
  377. bool expired = false;
  378. lockdep_assert_held(&rdev->bss_lock);
  379. list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
  380. if (atomic_read(&bss->hold))
  381. continue;
  382. if (!time_after(expire_time, bss->ts))
  383. continue;
  384. if (__cfg80211_unlink_bss(rdev, bss))
  385. expired = true;
  386. }
  387. if (expired)
  388. rdev->bss_generation++;
  389. }
  390. static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
  391. {
  392. struct cfg80211_internal_bss *bss, *oldest = NULL;
  393. bool ret;
  394. lockdep_assert_held(&rdev->bss_lock);
  395. list_for_each_entry(bss, &rdev->bss_list, list) {
  396. if (atomic_read(&bss->hold))
  397. continue;
  398. if (!list_empty(&bss->hidden_list) &&
  399. !bss->pub.hidden_beacon_bss)
  400. continue;
  401. if (oldest && time_before(oldest->ts, bss->ts))
  402. continue;
  403. oldest = bss;
  404. }
  405. if (WARN_ON(!oldest))
  406. return false;
  407. /*
  408. * The callers make sure to increase rdev->bss_generation if anything
  409. * gets removed (and a new entry added), so there's no need to also do
  410. * it here.
  411. */
  412. ret = __cfg80211_unlink_bss(rdev, oldest);
  413. WARN_ON(!ret);
  414. return ret;
  415. }
  416. static u8 cfg80211_parse_bss_param(u8 data,
  417. struct cfg80211_colocated_ap *coloc_ap)
  418. {
  419. coloc_ap->oct_recommended =
  420. u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_OCT_RECOMMENDED);
  421. coloc_ap->same_ssid =
  422. u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_SAME_SSID);
  423. coloc_ap->multi_bss =
  424. u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_MULTI_BSSID);
  425. coloc_ap->transmitted_bssid =
  426. u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_TRANSMITTED_BSSID);
  427. coloc_ap->unsolicited_probe =
  428. u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_PROBE_ACTIVE);
  429. coloc_ap->colocated_ess =
  430. u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_COLOC_ESS);
  431. return u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_COLOC_AP);
  432. }
  433. static int cfg80211_calc_short_ssid(const struct cfg80211_bss_ies *ies,
  434. const struct element **elem, u32 *s_ssid)
  435. {
  436. *elem = cfg80211_find_elem(WLAN_EID_SSID, ies->data, ies->len);
  437. if (!*elem || (*elem)->datalen > IEEE80211_MAX_SSID_LEN)
  438. return -EINVAL;
  439. *s_ssid = ~crc32_le(~0, (*elem)->data, (*elem)->datalen);
  440. return 0;
  441. }
  442. VISIBLE_IF_CFG80211_KUNIT void
  443. cfg80211_free_coloc_ap_list(struct list_head *coloc_ap_list)
  444. {
  445. struct cfg80211_colocated_ap *ap, *tmp_ap;
  446. list_for_each_entry_safe(ap, tmp_ap, coloc_ap_list, list) {
  447. list_del(&ap->list);
  448. kfree(ap);
  449. }
  450. }
  451. EXPORT_SYMBOL_IF_CFG80211_KUNIT(cfg80211_free_coloc_ap_list);
  452. static int cfg80211_parse_ap_info(struct cfg80211_colocated_ap *entry,
  453. const u8 *pos, u8 length,
  454. const struct element *ssid_elem,
  455. u32 s_ssid_tmp)
  456. {
  457. u8 bss_params;
  458. entry->psd_20 = IEEE80211_RNR_TBTT_PARAMS_PSD_RESERVED;
  459. /* The length is already verified by the caller to contain bss_params */
  460. if (length > sizeof(struct ieee80211_tbtt_info_7_8_9)) {
  461. struct ieee80211_tbtt_info_ge_11 *tbtt_info = (void *)pos;
  462. memcpy(entry->bssid, tbtt_info->bssid, ETH_ALEN);
  463. entry->short_ssid = le32_to_cpu(tbtt_info->short_ssid);
  464. entry->short_ssid_valid = true;
  465. bss_params = tbtt_info->bss_params;
  466. /* Ignore disabled links */
  467. if (length >= offsetofend(typeof(*tbtt_info), mld_params)) {
  468. if (le16_get_bits(tbtt_info->mld_params.params,
  469. IEEE80211_RNR_MLD_PARAMS_DISABLED_LINK))
  470. return -EINVAL;
  471. }
  472. if (length >= offsetofend(struct ieee80211_tbtt_info_ge_11,
  473. psd_20))
  474. entry->psd_20 = tbtt_info->psd_20;
  475. } else {
  476. struct ieee80211_tbtt_info_7_8_9 *tbtt_info = (void *)pos;
  477. memcpy(entry->bssid, tbtt_info->bssid, ETH_ALEN);
  478. bss_params = tbtt_info->bss_params;
  479. if (length == offsetofend(struct ieee80211_tbtt_info_7_8_9,
  480. psd_20))
  481. entry->psd_20 = tbtt_info->psd_20;
  482. }
  483. /* ignore entries with invalid BSSID */
  484. if (!is_valid_ether_addr(entry->bssid))
  485. return -EINVAL;
  486. /* skip non colocated APs */
  487. if (!cfg80211_parse_bss_param(bss_params, entry))
  488. return -EINVAL;
  489. /* no information about the short ssid. Consider the entry valid
  490. * for now. It would later be dropped in case there are explicit
  491. * SSIDs that need to be matched
  492. */
  493. if (!entry->same_ssid && !entry->short_ssid_valid)
  494. return 0;
  495. if (entry->same_ssid) {
  496. entry->short_ssid = s_ssid_tmp;
  497. entry->short_ssid_valid = true;
  498. /*
  499. * This is safe because we validate datalen in
  500. * cfg80211_parse_colocated_ap(), before calling this
  501. * function.
  502. */
  503. memcpy(&entry->ssid, &ssid_elem->data, ssid_elem->datalen);
  504. entry->ssid_len = ssid_elem->datalen;
  505. }
  506. return 0;
  507. }
  508. bool cfg80211_iter_rnr(const u8 *elems, size_t elems_len,
  509. enum cfg80211_rnr_iter_ret
  510. (*iter)(void *data, u8 type,
  511. const struct ieee80211_neighbor_ap_info *info,
  512. const u8 *tbtt_info, u8 tbtt_info_len),
  513. void *iter_data)
  514. {
  515. const struct element *rnr;
  516. const u8 *pos, *end;
  517. for_each_element_id(rnr, WLAN_EID_REDUCED_NEIGHBOR_REPORT,
  518. elems, elems_len) {
  519. const struct ieee80211_neighbor_ap_info *info;
  520. pos = rnr->data;
  521. end = rnr->data + rnr->datalen;
  522. /* RNR IE may contain more than one NEIGHBOR_AP_INFO */
  523. while (sizeof(*info) <= end - pos) {
  524. u8 length, i, count;
  525. u8 type;
  526. info = (void *)pos;
  527. count = u8_get_bits(info->tbtt_info_hdr,
  528. IEEE80211_AP_INFO_TBTT_HDR_COUNT) +
  529. 1;
  530. length = info->tbtt_info_len;
  531. pos += sizeof(*info);
  532. if (count * length > end - pos)
  533. return false;
  534. type = u8_get_bits(info->tbtt_info_hdr,
  535. IEEE80211_AP_INFO_TBTT_HDR_TYPE);
  536. for (i = 0; i < count; i++) {
  537. switch (iter(iter_data, type, info,
  538. pos, length)) {
  539. case RNR_ITER_CONTINUE:
  540. break;
  541. case RNR_ITER_BREAK:
  542. return true;
  543. case RNR_ITER_ERROR:
  544. return false;
  545. }
  546. pos += length;
  547. }
  548. }
  549. if (pos != end)
  550. return false;
  551. }
  552. return true;
  553. }
  554. EXPORT_SYMBOL_GPL(cfg80211_iter_rnr);
  555. struct colocated_ap_data {
  556. const struct element *ssid_elem;
  557. struct list_head ap_list;
  558. u32 s_ssid_tmp;
  559. int n_coloc;
  560. };
  561. static enum cfg80211_rnr_iter_ret
  562. cfg80211_parse_colocated_ap_iter(void *_data, u8 type,
  563. const struct ieee80211_neighbor_ap_info *info,
  564. const u8 *tbtt_info, u8 tbtt_info_len)
  565. {
  566. struct colocated_ap_data *data = _data;
  567. struct cfg80211_colocated_ap *entry;
  568. enum nl80211_band band;
  569. if (type != IEEE80211_TBTT_INFO_TYPE_TBTT)
  570. return RNR_ITER_CONTINUE;
  571. if (!ieee80211_operating_class_to_band(info->op_class, &band))
  572. return RNR_ITER_CONTINUE;
  573. /* TBTT info must include bss param + BSSID + (short SSID or
  574. * same_ssid bit to be set). Ignore other options, and move to
  575. * the next AP info
  576. */
  577. if (band != NL80211_BAND_6GHZ ||
  578. !(tbtt_info_len == offsetofend(struct ieee80211_tbtt_info_7_8_9,
  579. bss_params) ||
  580. tbtt_info_len == sizeof(struct ieee80211_tbtt_info_7_8_9) ||
  581. tbtt_info_len >= offsetofend(struct ieee80211_tbtt_info_ge_11,
  582. bss_params)))
  583. return RNR_ITER_CONTINUE;
  584. entry = kzalloc(sizeof(*entry) + IEEE80211_MAX_SSID_LEN, GFP_ATOMIC);
  585. if (!entry)
  586. return RNR_ITER_ERROR;
  587. entry->center_freq =
  588. ieee80211_channel_to_frequency(info->channel, band);
  589. if (!cfg80211_parse_ap_info(entry, tbtt_info, tbtt_info_len,
  590. data->ssid_elem, data->s_ssid_tmp)) {
  591. data->n_coloc++;
  592. list_add_tail(&entry->list, &data->ap_list);
  593. } else {
  594. kfree(entry);
  595. }
  596. return RNR_ITER_CONTINUE;
  597. }
  598. VISIBLE_IF_CFG80211_KUNIT int
  599. cfg80211_parse_colocated_ap(const struct cfg80211_bss_ies *ies,
  600. struct list_head *list)
  601. {
  602. struct colocated_ap_data data = {};
  603. int ret;
  604. INIT_LIST_HEAD(&data.ap_list);
  605. ret = cfg80211_calc_short_ssid(ies, &data.ssid_elem, &data.s_ssid_tmp);
  606. if (ret)
  607. return 0;
  608. if (!cfg80211_iter_rnr(ies->data, ies->len,
  609. cfg80211_parse_colocated_ap_iter, &data)) {
  610. cfg80211_free_coloc_ap_list(&data.ap_list);
  611. return 0;
  612. }
  613. list_splice_tail(&data.ap_list, list);
  614. return data.n_coloc;
  615. }
  616. EXPORT_SYMBOL_IF_CFG80211_KUNIT(cfg80211_parse_colocated_ap);
  617. static void cfg80211_scan_req_add_chan(struct cfg80211_scan_request *request,
  618. struct ieee80211_channel *chan,
  619. bool add_to_6ghz)
  620. {
  621. int i;
  622. u32 n_channels = request->n_channels;
  623. struct cfg80211_scan_6ghz_params *params =
  624. &request->scan_6ghz_params[request->n_6ghz_params];
  625. for (i = 0; i < n_channels; i++) {
  626. if (request->channels[i] == chan) {
  627. if (add_to_6ghz)
  628. params->channel_idx = i;
  629. return;
  630. }
  631. }
  632. request->n_channels++;
  633. request->channels[n_channels] = chan;
  634. if (add_to_6ghz)
  635. request->scan_6ghz_params[request->n_6ghz_params].channel_idx =
  636. n_channels;
  637. }
  638. static bool cfg80211_find_ssid_match(struct cfg80211_colocated_ap *ap,
  639. struct cfg80211_scan_request *request)
  640. {
  641. int i;
  642. u32 s_ssid;
  643. for (i = 0; i < request->n_ssids; i++) {
  644. /* wildcard ssid in the scan request */
  645. if (!request->ssids[i].ssid_len) {
  646. if (ap->multi_bss && !ap->transmitted_bssid)
  647. continue;
  648. return true;
  649. }
  650. if (ap->ssid_len &&
  651. ap->ssid_len == request->ssids[i].ssid_len) {
  652. if (!memcmp(request->ssids[i].ssid, ap->ssid,
  653. ap->ssid_len))
  654. return true;
  655. } else if (ap->short_ssid_valid) {
  656. s_ssid = ~crc32_le(~0, request->ssids[i].ssid,
  657. request->ssids[i].ssid_len);
  658. if (ap->short_ssid == s_ssid)
  659. return true;
  660. }
  661. }
  662. return false;
  663. }
  664. static int cfg80211_scan_6ghz(struct cfg80211_registered_device *rdev)
  665. {
  666. u8 i;
  667. struct cfg80211_colocated_ap *ap;
  668. int n_channels, count = 0, err;
  669. struct cfg80211_scan_request *request, *rdev_req = rdev->scan_req;
  670. LIST_HEAD(coloc_ap_list);
  671. bool need_scan_psc = true;
  672. const struct ieee80211_sband_iftype_data *iftd;
  673. size_t size, offs_ssids, offs_6ghz_params, offs_ies;
  674. rdev_req->scan_6ghz = true;
  675. if (!rdev->wiphy.bands[NL80211_BAND_6GHZ])
  676. return -EOPNOTSUPP;
  677. iftd = ieee80211_get_sband_iftype_data(rdev->wiphy.bands[NL80211_BAND_6GHZ],
  678. rdev_req->wdev->iftype);
  679. if (!iftd || !iftd->he_cap.has_he)
  680. return -EOPNOTSUPP;
  681. n_channels = rdev->wiphy.bands[NL80211_BAND_6GHZ]->n_channels;
  682. if (rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ) {
  683. struct cfg80211_internal_bss *intbss;
  684. spin_lock_bh(&rdev->bss_lock);
  685. list_for_each_entry(intbss, &rdev->bss_list, list) {
  686. struct cfg80211_bss *res = &intbss->pub;
  687. const struct cfg80211_bss_ies *ies;
  688. const struct element *ssid_elem;
  689. struct cfg80211_colocated_ap *entry;
  690. u32 s_ssid_tmp;
  691. int ret;
  692. ies = rcu_access_pointer(res->ies);
  693. count += cfg80211_parse_colocated_ap(ies,
  694. &coloc_ap_list);
  695. /* In case the scan request specified a specific BSSID
  696. * and the BSS is found and operating on 6GHz band then
  697. * add this AP to the collocated APs list.
  698. * This is relevant for ML probe requests when the lower
  699. * band APs have not been discovered.
  700. */
  701. if (is_broadcast_ether_addr(rdev_req->bssid) ||
  702. !ether_addr_equal(rdev_req->bssid, res->bssid) ||
  703. res->channel->band != NL80211_BAND_6GHZ)
  704. continue;
  705. ret = cfg80211_calc_short_ssid(ies, &ssid_elem,
  706. &s_ssid_tmp);
  707. if (ret)
  708. continue;
  709. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  710. if (!entry)
  711. continue;
  712. memcpy(entry->bssid, res->bssid, ETH_ALEN);
  713. entry->short_ssid = s_ssid_tmp;
  714. memcpy(entry->ssid, ssid_elem->data,
  715. ssid_elem->datalen);
  716. entry->ssid_len = ssid_elem->datalen;
  717. entry->short_ssid_valid = true;
  718. entry->center_freq = res->channel->center_freq;
  719. list_add_tail(&entry->list, &coloc_ap_list);
  720. count++;
  721. }
  722. spin_unlock_bh(&rdev->bss_lock);
  723. }
  724. size = struct_size(request, channels, n_channels);
  725. offs_ssids = size;
  726. size += sizeof(*request->ssids) * rdev_req->n_ssids;
  727. offs_6ghz_params = size;
  728. size += sizeof(*request->scan_6ghz_params) * count;
  729. offs_ies = size;
  730. size += rdev_req->ie_len;
  731. request = kzalloc(size, GFP_KERNEL);
  732. if (!request) {
  733. cfg80211_free_coloc_ap_list(&coloc_ap_list);
  734. return -ENOMEM;
  735. }
  736. *request = *rdev_req;
  737. request->n_channels = 0;
  738. request->n_6ghz_params = 0;
  739. if (rdev_req->n_ssids) {
  740. /*
  741. * Add the ssids from the parent scan request to the new
  742. * scan request, so the driver would be able to use them
  743. * in its probe requests to discover hidden APs on PSC
  744. * channels.
  745. */
  746. request->ssids = (void *)request + offs_ssids;
  747. memcpy(request->ssids, rdev_req->ssids,
  748. sizeof(*request->ssids) * request->n_ssids);
  749. }
  750. request->scan_6ghz_params = (void *)request + offs_6ghz_params;
  751. if (rdev_req->ie_len) {
  752. void *ie = (void *)request + offs_ies;
  753. memcpy(ie, rdev_req->ie, rdev_req->ie_len);
  754. request->ie = ie;
  755. }
  756. /*
  757. * PSC channels should not be scanned in case of direct scan with 1 SSID
  758. * and at least one of the reported co-located APs with same SSID
  759. * indicating that all APs in the same ESS are co-located
  760. */
  761. if (count && request->n_ssids == 1 && request->ssids[0].ssid_len) {
  762. list_for_each_entry(ap, &coloc_ap_list, list) {
  763. if (ap->colocated_ess &&
  764. cfg80211_find_ssid_match(ap, request)) {
  765. need_scan_psc = false;
  766. break;
  767. }
  768. }
  769. }
  770. /*
  771. * add to the scan request the channels that need to be scanned
  772. * regardless of the collocated APs (PSC channels or all channels
  773. * in case that NL80211_SCAN_FLAG_COLOCATED_6GHZ is not set)
  774. */
  775. for (i = 0; i < rdev_req->n_channels; i++) {
  776. if (rdev_req->channels[i]->band == NL80211_BAND_6GHZ &&
  777. ((need_scan_psc &&
  778. cfg80211_channel_is_psc(rdev_req->channels[i])) ||
  779. !(rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ))) {
  780. cfg80211_scan_req_add_chan(request,
  781. rdev_req->channels[i],
  782. false);
  783. }
  784. }
  785. if (!(rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ))
  786. goto skip;
  787. list_for_each_entry(ap, &coloc_ap_list, list) {
  788. bool found = false;
  789. struct cfg80211_scan_6ghz_params *scan_6ghz_params =
  790. &request->scan_6ghz_params[request->n_6ghz_params];
  791. struct ieee80211_channel *chan =
  792. ieee80211_get_channel(&rdev->wiphy, ap->center_freq);
  793. if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
  794. continue;
  795. for (i = 0; i < rdev_req->n_channels; i++) {
  796. if (rdev_req->channels[i] == chan)
  797. found = true;
  798. }
  799. if (!found)
  800. continue;
  801. if (request->n_ssids > 0 &&
  802. !cfg80211_find_ssid_match(ap, request))
  803. continue;
  804. if (!is_broadcast_ether_addr(request->bssid) &&
  805. !ether_addr_equal(request->bssid, ap->bssid))
  806. continue;
  807. if (!request->n_ssids && ap->multi_bss && !ap->transmitted_bssid)
  808. continue;
  809. cfg80211_scan_req_add_chan(request, chan, true);
  810. memcpy(scan_6ghz_params->bssid, ap->bssid, ETH_ALEN);
  811. scan_6ghz_params->short_ssid = ap->short_ssid;
  812. scan_6ghz_params->short_ssid_valid = ap->short_ssid_valid;
  813. scan_6ghz_params->unsolicited_probe = ap->unsolicited_probe;
  814. scan_6ghz_params->psd_20 = ap->psd_20;
  815. /*
  816. * If a PSC channel is added to the scan and 'need_scan_psc' is
  817. * set to false, then all the APs that the scan logic is
  818. * interested with on the channel are collocated and thus there
  819. * is no need to perform the initial PSC channel listen.
  820. */
  821. if (cfg80211_channel_is_psc(chan) && !need_scan_psc)
  822. scan_6ghz_params->psc_no_listen = true;
  823. request->n_6ghz_params++;
  824. }
  825. skip:
  826. cfg80211_free_coloc_ap_list(&coloc_ap_list);
  827. if (request->n_channels) {
  828. struct cfg80211_scan_request *old = rdev->int_scan_req;
  829. rdev->int_scan_req = request;
  830. /*
  831. * If this scan follows a previous scan, save the scan start
  832. * info from the first part of the scan
  833. */
  834. if (old)
  835. rdev->int_scan_req->info = old->info;
  836. err = rdev_scan(rdev, request);
  837. if (err) {
  838. rdev->int_scan_req = old;
  839. kfree(request);
  840. } else {
  841. kfree(old);
  842. }
  843. return err;
  844. }
  845. kfree(request);
  846. return -EINVAL;
  847. }
  848. int cfg80211_scan(struct cfg80211_registered_device *rdev)
  849. {
  850. struct cfg80211_scan_request *request;
  851. struct cfg80211_scan_request *rdev_req = rdev->scan_req;
  852. u32 n_channels = 0, idx, i;
  853. if (!(rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ))
  854. return rdev_scan(rdev, rdev_req);
  855. for (i = 0; i < rdev_req->n_channels; i++) {
  856. if (rdev_req->channels[i]->band != NL80211_BAND_6GHZ)
  857. n_channels++;
  858. }
  859. if (!n_channels)
  860. return cfg80211_scan_6ghz(rdev);
  861. request = kzalloc(struct_size(request, channels, n_channels),
  862. GFP_KERNEL);
  863. if (!request)
  864. return -ENOMEM;
  865. *request = *rdev_req;
  866. request->n_channels = n_channels;
  867. for (i = idx = 0; i < rdev_req->n_channels; i++) {
  868. if (rdev_req->channels[i]->band != NL80211_BAND_6GHZ)
  869. request->channels[idx++] = rdev_req->channels[i];
  870. }
  871. rdev_req->scan_6ghz = false;
  872. rdev->int_scan_req = request;
  873. return rdev_scan(rdev, request);
  874. }
  875. void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
  876. bool send_message)
  877. {
  878. struct cfg80211_scan_request *request, *rdev_req;
  879. struct wireless_dev *wdev;
  880. struct sk_buff *msg;
  881. #ifdef CONFIG_CFG80211_WEXT
  882. union iwreq_data wrqu;
  883. #endif
  884. lockdep_assert_held(&rdev->wiphy.mtx);
  885. if (rdev->scan_msg) {
  886. nl80211_send_scan_msg(rdev, rdev->scan_msg);
  887. rdev->scan_msg = NULL;
  888. return;
  889. }
  890. rdev_req = rdev->scan_req;
  891. if (!rdev_req)
  892. return;
  893. wdev = rdev_req->wdev;
  894. request = rdev->int_scan_req ? rdev->int_scan_req : rdev_req;
  895. if (wdev_running(wdev) &&
  896. (rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ) &&
  897. !rdev_req->scan_6ghz && !request->info.aborted &&
  898. !cfg80211_scan_6ghz(rdev))
  899. return;
  900. /*
  901. * This must be before sending the other events!
  902. * Otherwise, wpa_supplicant gets completely confused with
  903. * wext events.
  904. */
  905. if (wdev->netdev)
  906. cfg80211_sme_scan_done(wdev->netdev);
  907. if (!request->info.aborted &&
  908. request->flags & NL80211_SCAN_FLAG_FLUSH) {
  909. /* flush entries from previous scans */
  910. spin_lock_bh(&rdev->bss_lock);
  911. __cfg80211_bss_expire(rdev, request->scan_start);
  912. spin_unlock_bh(&rdev->bss_lock);
  913. }
  914. msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
  915. #ifdef CONFIG_CFG80211_WEXT
  916. if (wdev->netdev && !request->info.aborted) {
  917. memset(&wrqu, 0, sizeof(wrqu));
  918. wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
  919. }
  920. #endif
  921. dev_put(wdev->netdev);
  922. kfree(rdev->int_scan_req);
  923. rdev->int_scan_req = NULL;
  924. kfree(rdev->scan_req);
  925. rdev->scan_req = NULL;
  926. if (!send_message)
  927. rdev->scan_msg = msg;
  928. else
  929. nl80211_send_scan_msg(rdev, msg);
  930. }
  931. void __cfg80211_scan_done(struct wiphy *wiphy, struct wiphy_work *wk)
  932. {
  933. ___cfg80211_scan_done(wiphy_to_rdev(wiphy), true);
  934. }
  935. void cfg80211_scan_done(struct cfg80211_scan_request *request,
  936. struct cfg80211_scan_info *info)
  937. {
  938. struct cfg80211_scan_info old_info = request->info;
  939. trace_cfg80211_scan_done(request, info);
  940. WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req &&
  941. request != wiphy_to_rdev(request->wiphy)->int_scan_req);
  942. request->info = *info;
  943. /*
  944. * In case the scan is split, the scan_start_tsf and tsf_bssid should
  945. * be of the first part. In such a case old_info.scan_start_tsf should
  946. * be non zero.
  947. */
  948. if (request->scan_6ghz && old_info.scan_start_tsf) {
  949. request->info.scan_start_tsf = old_info.scan_start_tsf;
  950. memcpy(request->info.tsf_bssid, old_info.tsf_bssid,
  951. sizeof(request->info.tsf_bssid));
  952. }
  953. request->notified = true;
  954. wiphy_work_queue(request->wiphy,
  955. &wiphy_to_rdev(request->wiphy)->scan_done_wk);
  956. }
  957. EXPORT_SYMBOL(cfg80211_scan_done);
  958. void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
  959. struct cfg80211_sched_scan_request *req)
  960. {
  961. lockdep_assert_held(&rdev->wiphy.mtx);
  962. list_add_rcu(&req->list, &rdev->sched_scan_req_list);
  963. }
  964. static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
  965. struct cfg80211_sched_scan_request *req)
  966. {
  967. lockdep_assert_held(&rdev->wiphy.mtx);
  968. list_del_rcu(&req->list);
  969. kfree_rcu(req, rcu_head);
  970. }
  971. static struct cfg80211_sched_scan_request *
  972. cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
  973. {
  974. struct cfg80211_sched_scan_request *pos;
  975. list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list,
  976. lockdep_is_held(&rdev->wiphy.mtx)) {
  977. if (pos->reqid == reqid)
  978. return pos;
  979. }
  980. return NULL;
  981. }
  982. /*
  983. * Determines if a scheduled scan request can be handled. When a legacy
  984. * scheduled scan is running no other scheduled scan is allowed regardless
  985. * whether the request is for legacy or multi-support scan. When a multi-support
  986. * scheduled scan is running a request for legacy scan is not allowed. In this
  987. * case a request for multi-support scan can be handled if resources are
  988. * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
  989. */
  990. int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
  991. bool want_multi)
  992. {
  993. struct cfg80211_sched_scan_request *pos;
  994. int i = 0;
  995. list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
  996. /* request id zero means legacy in progress */
  997. if (!i && !pos->reqid)
  998. return -EINPROGRESS;
  999. i++;
  1000. }
  1001. if (i) {
  1002. /* no legacy allowed when multi request(s) are active */
  1003. if (!want_multi)
  1004. return -EINPROGRESS;
  1005. /* resource limit reached */
  1006. if (i == rdev->wiphy.max_sched_scan_reqs)
  1007. return -ENOSPC;
  1008. }
  1009. return 0;
  1010. }
  1011. void cfg80211_sched_scan_results_wk(struct work_struct *work)
  1012. {
  1013. struct cfg80211_registered_device *rdev;
  1014. struct cfg80211_sched_scan_request *req, *tmp;
  1015. rdev = container_of(work, struct cfg80211_registered_device,
  1016. sched_scan_res_wk);
  1017. wiphy_lock(&rdev->wiphy);
  1018. list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
  1019. if (req->report_results) {
  1020. req->report_results = false;
  1021. if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
  1022. /* flush entries from previous scans */
  1023. spin_lock_bh(&rdev->bss_lock);
  1024. __cfg80211_bss_expire(rdev, req->scan_start);
  1025. spin_unlock_bh(&rdev->bss_lock);
  1026. req->scan_start = jiffies;
  1027. }
  1028. nl80211_send_sched_scan(req,
  1029. NL80211_CMD_SCHED_SCAN_RESULTS);
  1030. }
  1031. }
  1032. wiphy_unlock(&rdev->wiphy);
  1033. }
  1034. void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
  1035. {
  1036. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1037. struct cfg80211_sched_scan_request *request;
  1038. trace_cfg80211_sched_scan_results(wiphy, reqid);
  1039. /* ignore if we're not scanning */
  1040. rcu_read_lock();
  1041. request = cfg80211_find_sched_scan_req(rdev, reqid);
  1042. if (request) {
  1043. request->report_results = true;
  1044. queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
  1045. }
  1046. rcu_read_unlock();
  1047. }
  1048. EXPORT_SYMBOL(cfg80211_sched_scan_results);
  1049. void cfg80211_sched_scan_stopped_locked(struct wiphy *wiphy, u64 reqid)
  1050. {
  1051. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1052. lockdep_assert_held(&wiphy->mtx);
  1053. trace_cfg80211_sched_scan_stopped(wiphy, reqid);
  1054. __cfg80211_stop_sched_scan(rdev, reqid, true);
  1055. }
  1056. EXPORT_SYMBOL(cfg80211_sched_scan_stopped_locked);
  1057. void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
  1058. {
  1059. wiphy_lock(wiphy);
  1060. cfg80211_sched_scan_stopped_locked(wiphy, reqid);
  1061. wiphy_unlock(wiphy);
  1062. }
  1063. EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
  1064. int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
  1065. struct cfg80211_sched_scan_request *req,
  1066. bool driver_initiated)
  1067. {
  1068. lockdep_assert_held(&rdev->wiphy.mtx);
  1069. if (!driver_initiated) {
  1070. int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
  1071. if (err)
  1072. return err;
  1073. }
  1074. nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
  1075. cfg80211_del_sched_scan_req(rdev, req);
  1076. return 0;
  1077. }
  1078. int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
  1079. u64 reqid, bool driver_initiated)
  1080. {
  1081. struct cfg80211_sched_scan_request *sched_scan_req;
  1082. lockdep_assert_held(&rdev->wiphy.mtx);
  1083. sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
  1084. if (!sched_scan_req)
  1085. return -ENOENT;
  1086. return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
  1087. driver_initiated);
  1088. }
  1089. void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
  1090. unsigned long age_secs)
  1091. {
  1092. struct cfg80211_internal_bss *bss;
  1093. unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
  1094. spin_lock_bh(&rdev->bss_lock);
  1095. list_for_each_entry(bss, &rdev->bss_list, list)
  1096. bss->ts -= age_jiffies;
  1097. spin_unlock_bh(&rdev->bss_lock);
  1098. }
  1099. void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
  1100. {
  1101. __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
  1102. }
  1103. void cfg80211_bss_flush(struct wiphy *wiphy)
  1104. {
  1105. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1106. spin_lock_bh(&rdev->bss_lock);
  1107. __cfg80211_bss_expire(rdev, jiffies);
  1108. spin_unlock_bh(&rdev->bss_lock);
  1109. }
  1110. EXPORT_SYMBOL(cfg80211_bss_flush);
  1111. const struct element *
  1112. cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
  1113. const u8 *match, unsigned int match_len,
  1114. unsigned int match_offset)
  1115. {
  1116. const struct element *elem;
  1117. for_each_element_id(elem, eid, ies, len) {
  1118. if (elem->datalen >= match_offset + match_len &&
  1119. !memcmp(elem->data + match_offset, match, match_len))
  1120. return elem;
  1121. }
  1122. return NULL;
  1123. }
  1124. EXPORT_SYMBOL(cfg80211_find_elem_match);
  1125. const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
  1126. const u8 *ies,
  1127. unsigned int len)
  1128. {
  1129. const struct element *elem;
  1130. u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
  1131. int match_len = (oui_type < 0) ? 3 : sizeof(match);
  1132. if (WARN_ON(oui_type > 0xff))
  1133. return NULL;
  1134. elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
  1135. match, match_len, 0);
  1136. if (!elem || elem->datalen < 4)
  1137. return NULL;
  1138. return elem;
  1139. }
  1140. EXPORT_SYMBOL(cfg80211_find_vendor_elem);
  1141. /**
  1142. * enum bss_compare_mode - BSS compare mode
  1143. * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
  1144. * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
  1145. * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
  1146. */
  1147. enum bss_compare_mode {
  1148. BSS_CMP_REGULAR,
  1149. BSS_CMP_HIDE_ZLEN,
  1150. BSS_CMP_HIDE_NUL,
  1151. };
  1152. static int cmp_bss(struct cfg80211_bss *a,
  1153. struct cfg80211_bss *b,
  1154. enum bss_compare_mode mode)
  1155. {
  1156. const struct cfg80211_bss_ies *a_ies, *b_ies;
  1157. const u8 *ie1 = NULL;
  1158. const u8 *ie2 = NULL;
  1159. int i, r;
  1160. if (a->channel != b->channel)
  1161. return (b->channel->center_freq * 1000 + b->channel->freq_offset) -
  1162. (a->channel->center_freq * 1000 + a->channel->freq_offset);
  1163. a_ies = rcu_access_pointer(a->ies);
  1164. if (!a_ies)
  1165. return -1;
  1166. b_ies = rcu_access_pointer(b->ies);
  1167. if (!b_ies)
  1168. return 1;
  1169. if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
  1170. ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
  1171. a_ies->data, a_ies->len);
  1172. if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
  1173. ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
  1174. b_ies->data, b_ies->len);
  1175. if (ie1 && ie2) {
  1176. int mesh_id_cmp;
  1177. if (ie1[1] == ie2[1])
  1178. mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  1179. else
  1180. mesh_id_cmp = ie2[1] - ie1[1];
  1181. ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
  1182. a_ies->data, a_ies->len);
  1183. ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
  1184. b_ies->data, b_ies->len);
  1185. if (ie1 && ie2) {
  1186. if (mesh_id_cmp)
  1187. return mesh_id_cmp;
  1188. if (ie1[1] != ie2[1])
  1189. return ie2[1] - ie1[1];
  1190. return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  1191. }
  1192. }
  1193. r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
  1194. if (r)
  1195. return r;
  1196. ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
  1197. ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
  1198. if (!ie1 && !ie2)
  1199. return 0;
  1200. /*
  1201. * Note that with "hide_ssid", the function returns a match if
  1202. * the already-present BSS ("b") is a hidden SSID beacon for
  1203. * the new BSS ("a").
  1204. */
  1205. /* sort missing IE before (left of) present IE */
  1206. if (!ie1)
  1207. return -1;
  1208. if (!ie2)
  1209. return 1;
  1210. switch (mode) {
  1211. case BSS_CMP_HIDE_ZLEN:
  1212. /*
  1213. * In ZLEN mode we assume the BSS entry we're
  1214. * looking for has a zero-length SSID. So if
  1215. * the one we're looking at right now has that,
  1216. * return 0. Otherwise, return the difference
  1217. * in length, but since we're looking for the
  1218. * 0-length it's really equivalent to returning
  1219. * the length of the one we're looking at.
  1220. *
  1221. * No content comparison is needed as we assume
  1222. * the content length is zero.
  1223. */
  1224. return ie2[1];
  1225. case BSS_CMP_REGULAR:
  1226. default:
  1227. /* sort by length first, then by contents */
  1228. if (ie1[1] != ie2[1])
  1229. return ie2[1] - ie1[1];
  1230. return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  1231. case BSS_CMP_HIDE_NUL:
  1232. if (ie1[1] != ie2[1])
  1233. return ie2[1] - ie1[1];
  1234. /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
  1235. for (i = 0; i < ie2[1]; i++)
  1236. if (ie2[i + 2])
  1237. return -1;
  1238. return 0;
  1239. }
  1240. }
  1241. static bool cfg80211_bss_type_match(u16 capability,
  1242. enum nl80211_band band,
  1243. enum ieee80211_bss_type bss_type)
  1244. {
  1245. bool ret = true;
  1246. u16 mask, val;
  1247. if (bss_type == IEEE80211_BSS_TYPE_ANY)
  1248. return ret;
  1249. if (band == NL80211_BAND_60GHZ) {
  1250. mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
  1251. switch (bss_type) {
  1252. case IEEE80211_BSS_TYPE_ESS:
  1253. val = WLAN_CAPABILITY_DMG_TYPE_AP;
  1254. break;
  1255. case IEEE80211_BSS_TYPE_PBSS:
  1256. val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
  1257. break;
  1258. case IEEE80211_BSS_TYPE_IBSS:
  1259. val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
  1260. break;
  1261. default:
  1262. return false;
  1263. }
  1264. } else {
  1265. mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
  1266. switch (bss_type) {
  1267. case IEEE80211_BSS_TYPE_ESS:
  1268. val = WLAN_CAPABILITY_ESS;
  1269. break;
  1270. case IEEE80211_BSS_TYPE_IBSS:
  1271. val = WLAN_CAPABILITY_IBSS;
  1272. break;
  1273. case IEEE80211_BSS_TYPE_MBSS:
  1274. val = 0;
  1275. break;
  1276. default:
  1277. return false;
  1278. }
  1279. }
  1280. ret = ((capability & mask) == val);
  1281. return ret;
  1282. }
  1283. /* Returned bss is reference counted and must be cleaned up appropriately. */
  1284. struct cfg80211_bss *__cfg80211_get_bss(struct wiphy *wiphy,
  1285. struct ieee80211_channel *channel,
  1286. const u8 *bssid,
  1287. const u8 *ssid, size_t ssid_len,
  1288. enum ieee80211_bss_type bss_type,
  1289. enum ieee80211_privacy privacy,
  1290. u32 use_for)
  1291. {
  1292. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1293. struct cfg80211_internal_bss *bss, *res = NULL;
  1294. unsigned long now = jiffies;
  1295. int bss_privacy;
  1296. trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
  1297. privacy);
  1298. spin_lock_bh(&rdev->bss_lock);
  1299. list_for_each_entry(bss, &rdev->bss_list, list) {
  1300. if (!cfg80211_bss_type_match(bss->pub.capability,
  1301. bss->pub.channel->band, bss_type))
  1302. continue;
  1303. bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
  1304. if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
  1305. (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
  1306. continue;
  1307. if (channel && bss->pub.channel != channel)
  1308. continue;
  1309. if (!is_valid_ether_addr(bss->pub.bssid))
  1310. continue;
  1311. if ((bss->pub.use_for & use_for) != use_for)
  1312. continue;
  1313. /* Don't get expired BSS structs */
  1314. if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
  1315. !atomic_read(&bss->hold))
  1316. continue;
  1317. if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
  1318. res = bss;
  1319. bss_ref_get(rdev, res);
  1320. break;
  1321. }
  1322. }
  1323. spin_unlock_bh(&rdev->bss_lock);
  1324. if (!res)
  1325. return NULL;
  1326. trace_cfg80211_return_bss(&res->pub);
  1327. return &res->pub;
  1328. }
  1329. EXPORT_SYMBOL(__cfg80211_get_bss);
  1330. static bool rb_insert_bss(struct cfg80211_registered_device *rdev,
  1331. struct cfg80211_internal_bss *bss)
  1332. {
  1333. struct rb_node **p = &rdev->bss_tree.rb_node;
  1334. struct rb_node *parent = NULL;
  1335. struct cfg80211_internal_bss *tbss;
  1336. int cmp;
  1337. while (*p) {
  1338. parent = *p;
  1339. tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
  1340. cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
  1341. if (WARN_ON(!cmp)) {
  1342. /* will sort of leak this BSS */
  1343. return false;
  1344. }
  1345. if (cmp < 0)
  1346. p = &(*p)->rb_left;
  1347. else
  1348. p = &(*p)->rb_right;
  1349. }
  1350. rb_link_node(&bss->rbn, parent, p);
  1351. rb_insert_color(&bss->rbn, &rdev->bss_tree);
  1352. return true;
  1353. }
  1354. static struct cfg80211_internal_bss *
  1355. rb_find_bss(struct cfg80211_registered_device *rdev,
  1356. struct cfg80211_internal_bss *res,
  1357. enum bss_compare_mode mode)
  1358. {
  1359. struct rb_node *n = rdev->bss_tree.rb_node;
  1360. struct cfg80211_internal_bss *bss;
  1361. int r;
  1362. while (n) {
  1363. bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
  1364. r = cmp_bss(&res->pub, &bss->pub, mode);
  1365. if (r == 0)
  1366. return bss;
  1367. else if (r < 0)
  1368. n = n->rb_left;
  1369. else
  1370. n = n->rb_right;
  1371. }
  1372. return NULL;
  1373. }
  1374. static void cfg80211_insert_bss(struct cfg80211_registered_device *rdev,
  1375. struct cfg80211_internal_bss *bss)
  1376. {
  1377. lockdep_assert_held(&rdev->bss_lock);
  1378. if (!rb_insert_bss(rdev, bss))
  1379. return;
  1380. list_add_tail(&bss->list, &rdev->bss_list);
  1381. rdev->bss_entries++;
  1382. }
  1383. static void cfg80211_rehash_bss(struct cfg80211_registered_device *rdev,
  1384. struct cfg80211_internal_bss *bss)
  1385. {
  1386. lockdep_assert_held(&rdev->bss_lock);
  1387. rb_erase(&bss->rbn, &rdev->bss_tree);
  1388. if (!rb_insert_bss(rdev, bss)) {
  1389. list_del(&bss->list);
  1390. if (!list_empty(&bss->hidden_list))
  1391. list_del_init(&bss->hidden_list);
  1392. if (!list_empty(&bss->pub.nontrans_list))
  1393. list_del_init(&bss->pub.nontrans_list);
  1394. rdev->bss_entries--;
  1395. }
  1396. rdev->bss_generation++;
  1397. }
  1398. static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
  1399. struct cfg80211_internal_bss *new)
  1400. {
  1401. const struct cfg80211_bss_ies *ies;
  1402. struct cfg80211_internal_bss *bss;
  1403. const u8 *ie;
  1404. int i, ssidlen;
  1405. u8 fold = 0;
  1406. u32 n_entries = 0;
  1407. ies = rcu_access_pointer(new->pub.beacon_ies);
  1408. if (WARN_ON(!ies))
  1409. return false;
  1410. ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
  1411. if (!ie) {
  1412. /* nothing to do */
  1413. return true;
  1414. }
  1415. ssidlen = ie[1];
  1416. for (i = 0; i < ssidlen; i++)
  1417. fold |= ie[2 + i];
  1418. if (fold) {
  1419. /* not a hidden SSID */
  1420. return true;
  1421. }
  1422. /* This is the bad part ... */
  1423. list_for_each_entry(bss, &rdev->bss_list, list) {
  1424. /*
  1425. * we're iterating all the entries anyway, so take the
  1426. * opportunity to validate the list length accounting
  1427. */
  1428. n_entries++;
  1429. if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
  1430. continue;
  1431. if (bss->pub.channel != new->pub.channel)
  1432. continue;
  1433. if (rcu_access_pointer(bss->pub.beacon_ies))
  1434. continue;
  1435. ies = rcu_access_pointer(bss->pub.ies);
  1436. if (!ies)
  1437. continue;
  1438. ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
  1439. if (!ie)
  1440. continue;
  1441. if (ssidlen && ie[1] != ssidlen)
  1442. continue;
  1443. if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
  1444. continue;
  1445. if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
  1446. list_del(&bss->hidden_list);
  1447. /* combine them */
  1448. list_add(&bss->hidden_list, &new->hidden_list);
  1449. bss->pub.hidden_beacon_bss = &new->pub;
  1450. new->refcount += bss->refcount;
  1451. rcu_assign_pointer(bss->pub.beacon_ies,
  1452. new->pub.beacon_ies);
  1453. }
  1454. WARN_ONCE(n_entries != rdev->bss_entries,
  1455. "rdev bss entries[%d]/list[len:%d] corruption\n",
  1456. rdev->bss_entries, n_entries);
  1457. return true;
  1458. }
  1459. static void cfg80211_update_hidden_bsses(struct cfg80211_internal_bss *known,
  1460. const struct cfg80211_bss_ies *new_ies,
  1461. const struct cfg80211_bss_ies *old_ies)
  1462. {
  1463. struct cfg80211_internal_bss *bss;
  1464. /* Assign beacon IEs to all sub entries */
  1465. list_for_each_entry(bss, &known->hidden_list, hidden_list) {
  1466. const struct cfg80211_bss_ies *ies;
  1467. ies = rcu_access_pointer(bss->pub.beacon_ies);
  1468. WARN_ON(ies != old_ies);
  1469. rcu_assign_pointer(bss->pub.beacon_ies, new_ies);
  1470. }
  1471. }
  1472. static void cfg80211_check_stuck_ecsa(struct cfg80211_registered_device *rdev,
  1473. struct cfg80211_internal_bss *known,
  1474. const struct cfg80211_bss_ies *old)
  1475. {
  1476. const struct ieee80211_ext_chansw_ie *ecsa;
  1477. const struct element *elem_new, *elem_old;
  1478. const struct cfg80211_bss_ies *new, *bcn;
  1479. if (known->pub.proberesp_ecsa_stuck)
  1480. return;
  1481. new = rcu_dereference_protected(known->pub.proberesp_ies,
  1482. lockdep_is_held(&rdev->bss_lock));
  1483. if (WARN_ON(!new))
  1484. return;
  1485. if (new->tsf - old->tsf < USEC_PER_SEC)
  1486. return;
  1487. elem_old = cfg80211_find_elem(WLAN_EID_EXT_CHANSWITCH_ANN,
  1488. old->data, old->len);
  1489. if (!elem_old)
  1490. return;
  1491. elem_new = cfg80211_find_elem(WLAN_EID_EXT_CHANSWITCH_ANN,
  1492. new->data, new->len);
  1493. if (!elem_new)
  1494. return;
  1495. bcn = rcu_dereference_protected(known->pub.beacon_ies,
  1496. lockdep_is_held(&rdev->bss_lock));
  1497. if (bcn &&
  1498. cfg80211_find_elem(WLAN_EID_EXT_CHANSWITCH_ANN,
  1499. bcn->data, bcn->len))
  1500. return;
  1501. if (elem_new->datalen != elem_old->datalen)
  1502. return;
  1503. if (elem_new->datalen < sizeof(struct ieee80211_ext_chansw_ie))
  1504. return;
  1505. if (memcmp(elem_new->data, elem_old->data, elem_new->datalen))
  1506. return;
  1507. ecsa = (void *)elem_new->data;
  1508. if (!ecsa->mode)
  1509. return;
  1510. if (ecsa->new_ch_num !=
  1511. ieee80211_frequency_to_channel(known->pub.channel->center_freq))
  1512. return;
  1513. known->pub.proberesp_ecsa_stuck = 1;
  1514. }
  1515. static bool
  1516. cfg80211_update_known_bss(struct cfg80211_registered_device *rdev,
  1517. struct cfg80211_internal_bss *known,
  1518. struct cfg80211_internal_bss *new,
  1519. bool signal_valid)
  1520. {
  1521. lockdep_assert_held(&rdev->bss_lock);
  1522. /* Update IEs */
  1523. if (rcu_access_pointer(new->pub.proberesp_ies)) {
  1524. const struct cfg80211_bss_ies *old;
  1525. old = rcu_access_pointer(known->pub.proberesp_ies);
  1526. rcu_assign_pointer(known->pub.proberesp_ies,
  1527. new->pub.proberesp_ies);
  1528. /* Override possible earlier Beacon frame IEs */
  1529. rcu_assign_pointer(known->pub.ies,
  1530. new->pub.proberesp_ies);
  1531. if (old) {
  1532. cfg80211_check_stuck_ecsa(rdev, known, old);
  1533. kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
  1534. }
  1535. }
  1536. if (rcu_access_pointer(new->pub.beacon_ies)) {
  1537. const struct cfg80211_bss_ies *old;
  1538. if (known->pub.hidden_beacon_bss &&
  1539. !list_empty(&known->hidden_list)) {
  1540. const struct cfg80211_bss_ies *f;
  1541. /* The known BSS struct is one of the probe
  1542. * response members of a group, but we're
  1543. * receiving a beacon (beacon_ies in the new
  1544. * bss is used). This can only mean that the
  1545. * AP changed its beacon from not having an
  1546. * SSID to showing it, which is confusing so
  1547. * drop this information.
  1548. */
  1549. f = rcu_access_pointer(new->pub.beacon_ies);
  1550. if (!new->pub.hidden_beacon_bss)
  1551. kfree_rcu((struct cfg80211_bss_ies *)f, rcu_head);
  1552. return false;
  1553. }
  1554. old = rcu_access_pointer(known->pub.beacon_ies);
  1555. rcu_assign_pointer(known->pub.beacon_ies, new->pub.beacon_ies);
  1556. /* Override IEs if they were from a beacon before */
  1557. if (old == rcu_access_pointer(known->pub.ies))
  1558. rcu_assign_pointer(known->pub.ies, new->pub.beacon_ies);
  1559. cfg80211_update_hidden_bsses(known,
  1560. rcu_access_pointer(new->pub.beacon_ies),
  1561. old);
  1562. if (old)
  1563. kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
  1564. }
  1565. known->pub.beacon_interval = new->pub.beacon_interval;
  1566. /* don't update the signal if beacon was heard on
  1567. * adjacent channel.
  1568. */
  1569. if (signal_valid)
  1570. known->pub.signal = new->pub.signal;
  1571. known->pub.capability = new->pub.capability;
  1572. known->ts = new->ts;
  1573. known->ts_boottime = new->ts_boottime;
  1574. known->parent_tsf = new->parent_tsf;
  1575. known->pub.chains = new->pub.chains;
  1576. memcpy(known->pub.chain_signal, new->pub.chain_signal,
  1577. IEEE80211_MAX_CHAINS);
  1578. ether_addr_copy(known->parent_bssid, new->parent_bssid);
  1579. known->pub.max_bssid_indicator = new->pub.max_bssid_indicator;
  1580. known->pub.bssid_index = new->pub.bssid_index;
  1581. known->pub.use_for &= new->pub.use_for;
  1582. known->pub.cannot_use_reasons = new->pub.cannot_use_reasons;
  1583. known->bss_source = new->bss_source;
  1584. return true;
  1585. }
  1586. /* Returned bss is reference counted and must be cleaned up appropriately. */
  1587. static struct cfg80211_internal_bss *
  1588. __cfg80211_bss_update(struct cfg80211_registered_device *rdev,
  1589. struct cfg80211_internal_bss *tmp,
  1590. bool signal_valid, unsigned long ts)
  1591. {
  1592. struct cfg80211_internal_bss *found = NULL;
  1593. struct cfg80211_bss_ies *ies;
  1594. if (WARN_ON(!tmp->pub.channel))
  1595. goto free_ies;
  1596. tmp->ts = ts;
  1597. if (WARN_ON(!rcu_access_pointer(tmp->pub.ies)))
  1598. goto free_ies;
  1599. found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
  1600. if (found) {
  1601. if (!cfg80211_update_known_bss(rdev, found, tmp, signal_valid))
  1602. return NULL;
  1603. } else {
  1604. struct cfg80211_internal_bss *new;
  1605. struct cfg80211_internal_bss *hidden;
  1606. /*
  1607. * create a copy -- the "res" variable that is passed in
  1608. * is allocated on the stack since it's not needed in the
  1609. * more common case of an update
  1610. */
  1611. new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
  1612. GFP_ATOMIC);
  1613. if (!new)
  1614. goto free_ies;
  1615. memcpy(new, tmp, sizeof(*new));
  1616. new->refcount = 1;
  1617. INIT_LIST_HEAD(&new->hidden_list);
  1618. INIT_LIST_HEAD(&new->pub.nontrans_list);
  1619. /* we'll set this later if it was non-NULL */
  1620. new->pub.transmitted_bss = NULL;
  1621. if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
  1622. hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
  1623. if (!hidden)
  1624. hidden = rb_find_bss(rdev, tmp,
  1625. BSS_CMP_HIDE_NUL);
  1626. if (hidden) {
  1627. new->pub.hidden_beacon_bss = &hidden->pub;
  1628. list_add(&new->hidden_list,
  1629. &hidden->hidden_list);
  1630. hidden->refcount++;
  1631. ies = (void *)rcu_access_pointer(new->pub.beacon_ies);
  1632. rcu_assign_pointer(new->pub.beacon_ies,
  1633. hidden->pub.beacon_ies);
  1634. if (ies)
  1635. kfree_rcu(ies, rcu_head);
  1636. }
  1637. } else {
  1638. /*
  1639. * Ok so we found a beacon, and don't have an entry. If
  1640. * it's a beacon with hidden SSID, we might be in for an
  1641. * expensive search for any probe responses that should
  1642. * be grouped with this beacon for updates ...
  1643. */
  1644. if (!cfg80211_combine_bsses(rdev, new)) {
  1645. bss_ref_put(rdev, new);
  1646. return NULL;
  1647. }
  1648. }
  1649. if (rdev->bss_entries >= bss_entries_limit &&
  1650. !cfg80211_bss_expire_oldest(rdev)) {
  1651. bss_ref_put(rdev, new);
  1652. return NULL;
  1653. }
  1654. /* This must be before the call to bss_ref_get */
  1655. if (tmp->pub.transmitted_bss) {
  1656. new->pub.transmitted_bss = tmp->pub.transmitted_bss;
  1657. bss_ref_get(rdev, bss_from_pub(tmp->pub.transmitted_bss));
  1658. }
  1659. cfg80211_insert_bss(rdev, new);
  1660. found = new;
  1661. }
  1662. rdev->bss_generation++;
  1663. bss_ref_get(rdev, found);
  1664. return found;
  1665. free_ies:
  1666. ies = (void *)rcu_access_pointer(tmp->pub.beacon_ies);
  1667. if (ies)
  1668. kfree_rcu(ies, rcu_head);
  1669. ies = (void *)rcu_access_pointer(tmp->pub.proberesp_ies);
  1670. if (ies)
  1671. kfree_rcu(ies, rcu_head);
  1672. return NULL;
  1673. }
  1674. struct cfg80211_internal_bss *
  1675. cfg80211_bss_update(struct cfg80211_registered_device *rdev,
  1676. struct cfg80211_internal_bss *tmp,
  1677. bool signal_valid, unsigned long ts)
  1678. {
  1679. struct cfg80211_internal_bss *res;
  1680. spin_lock_bh(&rdev->bss_lock);
  1681. res = __cfg80211_bss_update(rdev, tmp, signal_valid, ts);
  1682. spin_unlock_bh(&rdev->bss_lock);
  1683. return res;
  1684. }
  1685. int cfg80211_get_ies_channel_number(const u8 *ie, size_t ielen,
  1686. enum nl80211_band band)
  1687. {
  1688. const struct element *tmp;
  1689. if (band == NL80211_BAND_6GHZ) {
  1690. struct ieee80211_he_operation *he_oper;
  1691. tmp = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ie,
  1692. ielen);
  1693. if (tmp && tmp->datalen >= sizeof(*he_oper) &&
  1694. tmp->datalen >= ieee80211_he_oper_size(&tmp->data[1])) {
  1695. const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
  1696. he_oper = (void *)&tmp->data[1];
  1697. he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
  1698. if (!he_6ghz_oper)
  1699. return -1;
  1700. return he_6ghz_oper->primary;
  1701. }
  1702. } else if (band == NL80211_BAND_S1GHZ) {
  1703. tmp = cfg80211_find_elem(WLAN_EID_S1G_OPERATION, ie, ielen);
  1704. if (tmp && tmp->datalen >= sizeof(struct ieee80211_s1g_oper_ie)) {
  1705. struct ieee80211_s1g_oper_ie *s1gop = (void *)tmp->data;
  1706. return s1gop->oper_ch;
  1707. }
  1708. } else {
  1709. tmp = cfg80211_find_elem(WLAN_EID_DS_PARAMS, ie, ielen);
  1710. if (tmp && tmp->datalen == 1)
  1711. return tmp->data[0];
  1712. tmp = cfg80211_find_elem(WLAN_EID_HT_OPERATION, ie, ielen);
  1713. if (tmp &&
  1714. tmp->datalen >= sizeof(struct ieee80211_ht_operation)) {
  1715. struct ieee80211_ht_operation *htop = (void *)tmp->data;
  1716. return htop->primary_chan;
  1717. }
  1718. }
  1719. return -1;
  1720. }
  1721. EXPORT_SYMBOL(cfg80211_get_ies_channel_number);
  1722. /*
  1723. * Update RX channel information based on the available frame payload
  1724. * information. This is mainly for the 2.4 GHz band where frames can be received
  1725. * from neighboring channels and the Beacon frames use the DSSS Parameter Set
  1726. * element to indicate the current (transmitting) channel, but this might also
  1727. * be needed on other bands if RX frequency does not match with the actual
  1728. * operating channel of a BSS, or if the AP reports a different primary channel.
  1729. */
  1730. static struct ieee80211_channel *
  1731. cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
  1732. struct ieee80211_channel *channel)
  1733. {
  1734. u32 freq;
  1735. int channel_number;
  1736. struct ieee80211_channel *alt_channel;
  1737. channel_number = cfg80211_get_ies_channel_number(ie, ielen,
  1738. channel->band);
  1739. if (channel_number < 0) {
  1740. /* No channel information in frame payload */
  1741. return channel;
  1742. }
  1743. freq = ieee80211_channel_to_freq_khz(channel_number, channel->band);
  1744. /*
  1745. * Frame info (beacon/prob res) is the same as received channel,
  1746. * no need for further processing.
  1747. */
  1748. if (freq == ieee80211_channel_to_khz(channel))
  1749. return channel;
  1750. alt_channel = ieee80211_get_channel_khz(wiphy, freq);
  1751. if (!alt_channel) {
  1752. if (channel->band == NL80211_BAND_2GHZ ||
  1753. channel->band == NL80211_BAND_6GHZ) {
  1754. /*
  1755. * Better not allow unexpected channels when that could
  1756. * be going beyond the 1-11 range (e.g., discovering
  1757. * BSS on channel 12 when radio is configured for
  1758. * channel 11) or beyond the 6 GHz channel range.
  1759. */
  1760. return NULL;
  1761. }
  1762. /* No match for the payload channel number - ignore it */
  1763. return channel;
  1764. }
  1765. /*
  1766. * Use the channel determined through the payload channel number
  1767. * instead of the RX channel reported by the driver.
  1768. */
  1769. if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
  1770. return NULL;
  1771. return alt_channel;
  1772. }
  1773. struct cfg80211_inform_single_bss_data {
  1774. struct cfg80211_inform_bss *drv_data;
  1775. enum cfg80211_bss_frame_type ftype;
  1776. struct ieee80211_channel *channel;
  1777. u8 bssid[ETH_ALEN];
  1778. u64 tsf;
  1779. u16 capability;
  1780. u16 beacon_interval;
  1781. const u8 *ie;
  1782. size_t ielen;
  1783. enum bss_source_type bss_source;
  1784. /* Set if reporting bss_source != BSS_SOURCE_DIRECT */
  1785. struct cfg80211_bss *source_bss;
  1786. u8 max_bssid_indicator;
  1787. u8 bssid_index;
  1788. u8 use_for;
  1789. u64 cannot_use_reasons;
  1790. };
  1791. enum ieee80211_ap_reg_power
  1792. cfg80211_get_6ghz_power_type(const u8 *elems, size_t elems_len)
  1793. {
  1794. const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
  1795. struct ieee80211_he_operation *he_oper;
  1796. const struct element *tmp;
  1797. tmp = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION,
  1798. elems, elems_len);
  1799. if (!tmp || tmp->datalen < sizeof(*he_oper) + 1 ||
  1800. tmp->datalen < ieee80211_he_oper_size(tmp->data + 1))
  1801. return IEEE80211_REG_UNSET_AP;
  1802. he_oper = (void *)&tmp->data[1];
  1803. he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
  1804. if (!he_6ghz_oper)
  1805. return IEEE80211_REG_UNSET_AP;
  1806. switch (u8_get_bits(he_6ghz_oper->control,
  1807. IEEE80211_HE_6GHZ_OPER_CTRL_REG_INFO)) {
  1808. case IEEE80211_6GHZ_CTRL_REG_LPI_AP:
  1809. case IEEE80211_6GHZ_CTRL_REG_INDOOR_LPI_AP:
  1810. return IEEE80211_REG_LPI_AP;
  1811. case IEEE80211_6GHZ_CTRL_REG_SP_AP:
  1812. case IEEE80211_6GHZ_CTRL_REG_INDOOR_SP_AP:
  1813. return IEEE80211_REG_SP_AP;
  1814. case IEEE80211_6GHZ_CTRL_REG_VLP_AP:
  1815. return IEEE80211_REG_VLP_AP;
  1816. default:
  1817. return IEEE80211_REG_UNSET_AP;
  1818. }
  1819. }
  1820. static bool cfg80211_6ghz_power_type_valid(const u8 *elems, size_t elems_len,
  1821. const u32 flags)
  1822. {
  1823. switch (cfg80211_get_6ghz_power_type(elems, elems_len)) {
  1824. case IEEE80211_REG_LPI_AP:
  1825. return true;
  1826. case IEEE80211_REG_SP_AP:
  1827. return !(flags & IEEE80211_CHAN_NO_6GHZ_AFC_CLIENT);
  1828. case IEEE80211_REG_VLP_AP:
  1829. return !(flags & IEEE80211_CHAN_NO_6GHZ_VLP_CLIENT);
  1830. default:
  1831. return false;
  1832. }
  1833. }
  1834. /* Returned bss is reference counted and must be cleaned up appropriately. */
  1835. static struct cfg80211_bss *
  1836. cfg80211_inform_single_bss_data(struct wiphy *wiphy,
  1837. struct cfg80211_inform_single_bss_data *data,
  1838. gfp_t gfp)
  1839. {
  1840. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1841. struct cfg80211_inform_bss *drv_data = data->drv_data;
  1842. struct cfg80211_bss_ies *ies;
  1843. struct ieee80211_channel *channel;
  1844. struct cfg80211_internal_bss tmp = {}, *res;
  1845. int bss_type;
  1846. bool signal_valid;
  1847. unsigned long ts;
  1848. if (WARN_ON(!wiphy))
  1849. return NULL;
  1850. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  1851. (drv_data->signal < 0 || drv_data->signal > 100)))
  1852. return NULL;
  1853. if (WARN_ON(data->bss_source != BSS_SOURCE_DIRECT && !data->source_bss))
  1854. return NULL;
  1855. channel = data->channel;
  1856. if (!channel)
  1857. channel = cfg80211_get_bss_channel(wiphy, data->ie, data->ielen,
  1858. drv_data->chan);
  1859. if (!channel)
  1860. return NULL;
  1861. if (channel->band == NL80211_BAND_6GHZ &&
  1862. !cfg80211_6ghz_power_type_valid(data->ie, data->ielen,
  1863. channel->flags)) {
  1864. data->use_for = 0;
  1865. data->cannot_use_reasons =
  1866. NL80211_BSS_CANNOT_USE_6GHZ_PWR_MISMATCH;
  1867. }
  1868. memcpy(tmp.pub.bssid, data->bssid, ETH_ALEN);
  1869. tmp.pub.channel = channel;
  1870. if (data->bss_source != BSS_SOURCE_STA_PROFILE)
  1871. tmp.pub.signal = drv_data->signal;
  1872. else
  1873. tmp.pub.signal = 0;
  1874. tmp.pub.beacon_interval = data->beacon_interval;
  1875. tmp.pub.capability = data->capability;
  1876. tmp.ts_boottime = drv_data->boottime_ns;
  1877. tmp.parent_tsf = drv_data->parent_tsf;
  1878. ether_addr_copy(tmp.parent_bssid, drv_data->parent_bssid);
  1879. tmp.pub.chains = drv_data->chains;
  1880. memcpy(tmp.pub.chain_signal, drv_data->chain_signal,
  1881. IEEE80211_MAX_CHAINS);
  1882. tmp.pub.use_for = data->use_for;
  1883. tmp.pub.cannot_use_reasons = data->cannot_use_reasons;
  1884. tmp.bss_source = data->bss_source;
  1885. switch (data->bss_source) {
  1886. case BSS_SOURCE_MBSSID:
  1887. tmp.pub.transmitted_bss = data->source_bss;
  1888. fallthrough;
  1889. case BSS_SOURCE_STA_PROFILE:
  1890. ts = bss_from_pub(data->source_bss)->ts;
  1891. tmp.pub.bssid_index = data->bssid_index;
  1892. tmp.pub.max_bssid_indicator = data->max_bssid_indicator;
  1893. break;
  1894. case BSS_SOURCE_DIRECT:
  1895. ts = jiffies;
  1896. if (channel->band == NL80211_BAND_60GHZ) {
  1897. bss_type = data->capability &
  1898. WLAN_CAPABILITY_DMG_TYPE_MASK;
  1899. if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
  1900. bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
  1901. regulatory_hint_found_beacon(wiphy, channel,
  1902. gfp);
  1903. } else {
  1904. if (data->capability & WLAN_CAPABILITY_ESS)
  1905. regulatory_hint_found_beacon(wiphy, channel,
  1906. gfp);
  1907. }
  1908. break;
  1909. }
  1910. /*
  1911. * If we do not know here whether the IEs are from a Beacon or Probe
  1912. * Response frame, we need to pick one of the options and only use it
  1913. * with the driver that does not provide the full Beacon/Probe Response
  1914. * frame. Use Beacon frame pointer to avoid indicating that this should
  1915. * override the IEs pointer should we have received an earlier
  1916. * indication of Probe Response data.
  1917. */
  1918. ies = kzalloc(sizeof(*ies) + data->ielen, gfp);
  1919. if (!ies)
  1920. return NULL;
  1921. ies->len = data->ielen;
  1922. ies->tsf = data->tsf;
  1923. ies->from_beacon = false;
  1924. memcpy(ies->data, data->ie, data->ielen);
  1925. switch (data->ftype) {
  1926. case CFG80211_BSS_FTYPE_BEACON:
  1927. case CFG80211_BSS_FTYPE_S1G_BEACON:
  1928. ies->from_beacon = true;
  1929. fallthrough;
  1930. case CFG80211_BSS_FTYPE_UNKNOWN:
  1931. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  1932. break;
  1933. case CFG80211_BSS_FTYPE_PRESP:
  1934. rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
  1935. break;
  1936. }
  1937. rcu_assign_pointer(tmp.pub.ies, ies);
  1938. signal_valid = drv_data->chan == channel;
  1939. spin_lock_bh(&rdev->bss_lock);
  1940. res = __cfg80211_bss_update(rdev, &tmp, signal_valid, ts);
  1941. if (!res)
  1942. goto drop;
  1943. rdev_inform_bss(rdev, &res->pub, ies, drv_data->drv_data);
  1944. if (data->bss_source == BSS_SOURCE_MBSSID) {
  1945. /* this is a nontransmitting bss, we need to add it to
  1946. * transmitting bss' list if it is not there
  1947. */
  1948. if (cfg80211_add_nontrans_list(data->source_bss, &res->pub)) {
  1949. if (__cfg80211_unlink_bss(rdev, res)) {
  1950. rdev->bss_generation++;
  1951. res = NULL;
  1952. }
  1953. }
  1954. if (!res)
  1955. goto drop;
  1956. }
  1957. spin_unlock_bh(&rdev->bss_lock);
  1958. trace_cfg80211_return_bss(&res->pub);
  1959. /* __cfg80211_bss_update gives us a referenced result */
  1960. return &res->pub;
  1961. drop:
  1962. spin_unlock_bh(&rdev->bss_lock);
  1963. return NULL;
  1964. }
  1965. static const struct element
  1966. *cfg80211_get_profile_continuation(const u8 *ie, size_t ielen,
  1967. const struct element *mbssid_elem,
  1968. const struct element *sub_elem)
  1969. {
  1970. const u8 *mbssid_end = mbssid_elem->data + mbssid_elem->datalen;
  1971. const struct element *next_mbssid;
  1972. const struct element *next_sub;
  1973. next_mbssid = cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
  1974. mbssid_end,
  1975. ielen - (mbssid_end - ie));
  1976. /*
  1977. * If it is not the last subelement in current MBSSID IE or there isn't
  1978. * a next MBSSID IE - profile is complete.
  1979. */
  1980. if ((sub_elem->data + sub_elem->datalen < mbssid_end - 1) ||
  1981. !next_mbssid)
  1982. return NULL;
  1983. /* For any length error, just return NULL */
  1984. if (next_mbssid->datalen < 4)
  1985. return NULL;
  1986. next_sub = (void *)&next_mbssid->data[1];
  1987. if (next_mbssid->data + next_mbssid->datalen <
  1988. next_sub->data + next_sub->datalen)
  1989. return NULL;
  1990. if (next_sub->id != 0 || next_sub->datalen < 2)
  1991. return NULL;
  1992. /*
  1993. * Check if the first element in the next sub element is a start
  1994. * of a new profile
  1995. */
  1996. return next_sub->data[0] == WLAN_EID_NON_TX_BSSID_CAP ?
  1997. NULL : next_mbssid;
  1998. }
  1999. size_t cfg80211_merge_profile(const u8 *ie, size_t ielen,
  2000. const struct element *mbssid_elem,
  2001. const struct element *sub_elem,
  2002. u8 *merged_ie, size_t max_copy_len)
  2003. {
  2004. size_t copied_len = sub_elem->datalen;
  2005. const struct element *next_mbssid;
  2006. if (sub_elem->datalen > max_copy_len)
  2007. return 0;
  2008. memcpy(merged_ie, sub_elem->data, sub_elem->datalen);
  2009. while ((next_mbssid = cfg80211_get_profile_continuation(ie, ielen,
  2010. mbssid_elem,
  2011. sub_elem))) {
  2012. const struct element *next_sub = (void *)&next_mbssid->data[1];
  2013. if (copied_len + next_sub->datalen > max_copy_len)
  2014. break;
  2015. memcpy(merged_ie + copied_len, next_sub->data,
  2016. next_sub->datalen);
  2017. copied_len += next_sub->datalen;
  2018. }
  2019. return copied_len;
  2020. }
  2021. EXPORT_SYMBOL(cfg80211_merge_profile);
  2022. static void
  2023. cfg80211_parse_mbssid_data(struct wiphy *wiphy,
  2024. struct cfg80211_inform_single_bss_data *tx_data,
  2025. struct cfg80211_bss *source_bss,
  2026. gfp_t gfp)
  2027. {
  2028. struct cfg80211_inform_single_bss_data data = {
  2029. .drv_data = tx_data->drv_data,
  2030. .ftype = tx_data->ftype,
  2031. .tsf = tx_data->tsf,
  2032. .beacon_interval = tx_data->beacon_interval,
  2033. .source_bss = source_bss,
  2034. .bss_source = BSS_SOURCE_MBSSID,
  2035. .use_for = tx_data->use_for,
  2036. .cannot_use_reasons = tx_data->cannot_use_reasons,
  2037. };
  2038. const u8 *mbssid_index_ie;
  2039. const struct element *elem, *sub;
  2040. u8 *new_ie, *profile;
  2041. u64 seen_indices = 0;
  2042. struct cfg80211_bss *bss;
  2043. if (!source_bss)
  2044. return;
  2045. if (!cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
  2046. tx_data->ie, tx_data->ielen))
  2047. return;
  2048. if (!wiphy->support_mbssid)
  2049. return;
  2050. if (wiphy->support_only_he_mbssid &&
  2051. !cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY,
  2052. tx_data->ie, tx_data->ielen))
  2053. return;
  2054. new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
  2055. if (!new_ie)
  2056. return;
  2057. profile = kmalloc(tx_data->ielen, gfp);
  2058. if (!profile)
  2059. goto out;
  2060. for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID,
  2061. tx_data->ie, tx_data->ielen) {
  2062. if (elem->datalen < 4)
  2063. continue;
  2064. if (elem->data[0] < 1 || (int)elem->data[0] > 8)
  2065. continue;
  2066. for_each_element(sub, elem->data + 1, elem->datalen - 1) {
  2067. u8 profile_len;
  2068. if (sub->id != 0 || sub->datalen < 4) {
  2069. /* not a valid BSS profile */
  2070. continue;
  2071. }
  2072. if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
  2073. sub->data[1] != 2) {
  2074. /* The first element within the Nontransmitted
  2075. * BSSID Profile is not the Nontransmitted
  2076. * BSSID Capability element.
  2077. */
  2078. continue;
  2079. }
  2080. memset(profile, 0, tx_data->ielen);
  2081. profile_len = cfg80211_merge_profile(tx_data->ie,
  2082. tx_data->ielen,
  2083. elem,
  2084. sub,
  2085. profile,
  2086. tx_data->ielen);
  2087. /* found a Nontransmitted BSSID Profile */
  2088. mbssid_index_ie = cfg80211_find_ie
  2089. (WLAN_EID_MULTI_BSSID_IDX,
  2090. profile, profile_len);
  2091. if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
  2092. mbssid_index_ie[2] == 0 ||
  2093. mbssid_index_ie[2] > 46 ||
  2094. mbssid_index_ie[2] >= (1 << elem->data[0])) {
  2095. /* No valid Multiple BSSID-Index element */
  2096. continue;
  2097. }
  2098. if (seen_indices & BIT_ULL(mbssid_index_ie[2]))
  2099. /* We don't support legacy split of a profile */
  2100. net_dbg_ratelimited("Partial info for BSSID index %d\n",
  2101. mbssid_index_ie[2]);
  2102. seen_indices |= BIT_ULL(mbssid_index_ie[2]);
  2103. data.bssid_index = mbssid_index_ie[2];
  2104. data.max_bssid_indicator = elem->data[0];
  2105. cfg80211_gen_new_bssid(tx_data->bssid,
  2106. data.max_bssid_indicator,
  2107. data.bssid_index,
  2108. data.bssid);
  2109. memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
  2110. data.ie = new_ie;
  2111. data.ielen = cfg80211_gen_new_ie(tx_data->ie,
  2112. tx_data->ielen,
  2113. profile,
  2114. profile_len,
  2115. new_ie,
  2116. IEEE80211_MAX_DATA_LEN);
  2117. if (!data.ielen)
  2118. continue;
  2119. data.capability = get_unaligned_le16(profile + 2);
  2120. bss = cfg80211_inform_single_bss_data(wiphy, &data, gfp);
  2121. if (!bss)
  2122. break;
  2123. cfg80211_put_bss(wiphy, bss);
  2124. }
  2125. }
  2126. out:
  2127. kfree(new_ie);
  2128. kfree(profile);
  2129. }
  2130. ssize_t cfg80211_defragment_element(const struct element *elem, const u8 *ies,
  2131. size_t ieslen, u8 *data, size_t data_len,
  2132. u8 frag_id)
  2133. {
  2134. const struct element *next;
  2135. ssize_t copied;
  2136. u8 elem_datalen;
  2137. if (!elem)
  2138. return -EINVAL;
  2139. /* elem might be invalid after the memmove */
  2140. next = (void *)(elem->data + elem->datalen);
  2141. elem_datalen = elem->datalen;
  2142. if (elem->id == WLAN_EID_EXTENSION) {
  2143. copied = elem->datalen - 1;
  2144. if (data) {
  2145. if (copied > data_len)
  2146. return -ENOSPC;
  2147. memmove(data, elem->data + 1, copied);
  2148. }
  2149. } else {
  2150. copied = elem->datalen;
  2151. if (data) {
  2152. if (copied > data_len)
  2153. return -ENOSPC;
  2154. memmove(data, elem->data, copied);
  2155. }
  2156. }
  2157. /* Fragmented elements must have 255 bytes */
  2158. if (elem_datalen < 255)
  2159. return copied;
  2160. for (elem = next;
  2161. elem->data < ies + ieslen &&
  2162. elem->data + elem->datalen <= ies + ieslen;
  2163. elem = next) {
  2164. /* elem might be invalid after the memmove */
  2165. next = (void *)(elem->data + elem->datalen);
  2166. if (elem->id != frag_id)
  2167. break;
  2168. elem_datalen = elem->datalen;
  2169. if (data) {
  2170. if (copied + elem_datalen > data_len)
  2171. return -ENOSPC;
  2172. memmove(data + copied, elem->data, elem_datalen);
  2173. }
  2174. copied += elem_datalen;
  2175. /* Only the last fragment may be short */
  2176. if (elem_datalen != 255)
  2177. break;
  2178. }
  2179. return copied;
  2180. }
  2181. EXPORT_SYMBOL(cfg80211_defragment_element);
  2182. struct cfg80211_mle {
  2183. struct ieee80211_multi_link_elem *mle;
  2184. struct ieee80211_mle_per_sta_profile
  2185. *sta_prof[IEEE80211_MLD_MAX_NUM_LINKS];
  2186. ssize_t sta_prof_len[IEEE80211_MLD_MAX_NUM_LINKS];
  2187. u8 data[];
  2188. };
  2189. static struct cfg80211_mle *
  2190. cfg80211_defrag_mle(const struct element *mle, const u8 *ie, size_t ielen,
  2191. gfp_t gfp)
  2192. {
  2193. const struct element *elem;
  2194. struct cfg80211_mle *res;
  2195. size_t buf_len;
  2196. ssize_t mle_len;
  2197. u8 common_size, idx;
  2198. if (!mle || !ieee80211_mle_size_ok(mle->data + 1, mle->datalen - 1))
  2199. return NULL;
  2200. /* Required length for first defragmentation */
  2201. buf_len = mle->datalen - 1;
  2202. for_each_element(elem, mle->data + mle->datalen,
  2203. ie + ielen - mle->data - mle->datalen) {
  2204. if (elem->id != WLAN_EID_FRAGMENT)
  2205. break;
  2206. buf_len += elem->datalen;
  2207. }
  2208. res = kzalloc(struct_size(res, data, buf_len), gfp);
  2209. if (!res)
  2210. return NULL;
  2211. mle_len = cfg80211_defragment_element(mle, ie, ielen,
  2212. res->data, buf_len,
  2213. WLAN_EID_FRAGMENT);
  2214. if (mle_len < 0)
  2215. goto error;
  2216. res->mle = (void *)res->data;
  2217. /* Find the sub-element area in the buffer */
  2218. common_size = ieee80211_mle_common_size((u8 *)res->mle);
  2219. ie = res->data + common_size;
  2220. ielen = mle_len - common_size;
  2221. idx = 0;
  2222. for_each_element_id(elem, IEEE80211_MLE_SUBELEM_PER_STA_PROFILE,
  2223. ie, ielen) {
  2224. res->sta_prof[idx] = (void *)elem->data;
  2225. res->sta_prof_len[idx] = elem->datalen;
  2226. idx++;
  2227. if (idx >= IEEE80211_MLD_MAX_NUM_LINKS)
  2228. break;
  2229. }
  2230. if (!for_each_element_completed(elem, ie, ielen))
  2231. goto error;
  2232. /* Defragment sta_info in-place */
  2233. for (idx = 0; idx < IEEE80211_MLD_MAX_NUM_LINKS && res->sta_prof[idx];
  2234. idx++) {
  2235. if (res->sta_prof_len[idx] < 255)
  2236. continue;
  2237. elem = (void *)res->sta_prof[idx] - 2;
  2238. if (idx + 1 < ARRAY_SIZE(res->sta_prof) &&
  2239. res->sta_prof[idx + 1])
  2240. buf_len = (u8 *)res->sta_prof[idx + 1] -
  2241. (u8 *)res->sta_prof[idx];
  2242. else
  2243. buf_len = ielen + ie - (u8 *)elem;
  2244. res->sta_prof_len[idx] =
  2245. cfg80211_defragment_element(elem,
  2246. (u8 *)elem, buf_len,
  2247. (u8 *)res->sta_prof[idx],
  2248. buf_len,
  2249. IEEE80211_MLE_SUBELEM_FRAGMENT);
  2250. if (res->sta_prof_len[idx] < 0)
  2251. goto error;
  2252. }
  2253. return res;
  2254. error:
  2255. kfree(res);
  2256. return NULL;
  2257. }
  2258. struct tbtt_info_iter_data {
  2259. const struct ieee80211_neighbor_ap_info *ap_info;
  2260. u8 param_ch_count;
  2261. u32 use_for;
  2262. u8 mld_id, link_id;
  2263. bool non_tx;
  2264. };
  2265. static enum cfg80211_rnr_iter_ret
  2266. cfg802121_mld_ap_rnr_iter(void *_data, u8 type,
  2267. const struct ieee80211_neighbor_ap_info *info,
  2268. const u8 *tbtt_info, u8 tbtt_info_len)
  2269. {
  2270. const struct ieee80211_rnr_mld_params *mld_params;
  2271. struct tbtt_info_iter_data *data = _data;
  2272. u8 link_id;
  2273. bool non_tx = false;
  2274. if (type == IEEE80211_TBTT_INFO_TYPE_TBTT &&
  2275. tbtt_info_len >= offsetofend(struct ieee80211_tbtt_info_ge_11,
  2276. mld_params)) {
  2277. const struct ieee80211_tbtt_info_ge_11 *tbtt_info_ge_11 =
  2278. (void *)tbtt_info;
  2279. non_tx = (tbtt_info_ge_11->bss_params &
  2280. (IEEE80211_RNR_TBTT_PARAMS_MULTI_BSSID |
  2281. IEEE80211_RNR_TBTT_PARAMS_TRANSMITTED_BSSID)) ==
  2282. IEEE80211_RNR_TBTT_PARAMS_MULTI_BSSID;
  2283. mld_params = &tbtt_info_ge_11->mld_params;
  2284. } else if (type == IEEE80211_TBTT_INFO_TYPE_MLD &&
  2285. tbtt_info_len >= sizeof(struct ieee80211_rnr_mld_params))
  2286. mld_params = (void *)tbtt_info;
  2287. else
  2288. return RNR_ITER_CONTINUE;
  2289. link_id = le16_get_bits(mld_params->params,
  2290. IEEE80211_RNR_MLD_PARAMS_LINK_ID);
  2291. if (data->mld_id != mld_params->mld_id)
  2292. return RNR_ITER_CONTINUE;
  2293. if (data->link_id != link_id)
  2294. return RNR_ITER_CONTINUE;
  2295. data->ap_info = info;
  2296. data->param_ch_count =
  2297. le16_get_bits(mld_params->params,
  2298. IEEE80211_RNR_MLD_PARAMS_BSS_CHANGE_COUNT);
  2299. data->non_tx = non_tx;
  2300. if (type == IEEE80211_TBTT_INFO_TYPE_TBTT)
  2301. data->use_for = NL80211_BSS_USE_FOR_ALL;
  2302. else
  2303. data->use_for = NL80211_BSS_USE_FOR_MLD_LINK;
  2304. return RNR_ITER_BREAK;
  2305. }
  2306. static u8
  2307. cfg80211_rnr_info_for_mld_ap(const u8 *ie, size_t ielen, u8 mld_id, u8 link_id,
  2308. const struct ieee80211_neighbor_ap_info **ap_info,
  2309. u8 *param_ch_count, bool *non_tx)
  2310. {
  2311. struct tbtt_info_iter_data data = {
  2312. .mld_id = mld_id,
  2313. .link_id = link_id,
  2314. };
  2315. cfg80211_iter_rnr(ie, ielen, cfg802121_mld_ap_rnr_iter, &data);
  2316. *ap_info = data.ap_info;
  2317. *param_ch_count = data.param_ch_count;
  2318. *non_tx = data.non_tx;
  2319. return data.use_for;
  2320. }
  2321. static struct element *
  2322. cfg80211_gen_reporter_rnr(struct cfg80211_bss *source_bss, bool is_mbssid,
  2323. bool same_mld, u8 link_id, u8 bss_change_count,
  2324. gfp_t gfp)
  2325. {
  2326. const struct cfg80211_bss_ies *ies;
  2327. struct ieee80211_neighbor_ap_info ap_info;
  2328. struct ieee80211_tbtt_info_ge_11 tbtt_info;
  2329. u32 short_ssid;
  2330. const struct element *elem;
  2331. struct element *res;
  2332. /*
  2333. * We only generate the RNR to permit ML lookups. For that we do not
  2334. * need an entry for the corresponding transmitting BSS, lets just skip
  2335. * it even though it would be easy to add.
  2336. */
  2337. if (!same_mld)
  2338. return NULL;
  2339. /* We could use tx_data->ies if we change cfg80211_calc_short_ssid */
  2340. rcu_read_lock();
  2341. ies = rcu_dereference(source_bss->ies);
  2342. ap_info.tbtt_info_len = offsetofend(typeof(tbtt_info), mld_params);
  2343. ap_info.tbtt_info_hdr =
  2344. u8_encode_bits(IEEE80211_TBTT_INFO_TYPE_TBTT,
  2345. IEEE80211_AP_INFO_TBTT_HDR_TYPE) |
  2346. u8_encode_bits(0, IEEE80211_AP_INFO_TBTT_HDR_COUNT);
  2347. ap_info.channel = ieee80211_frequency_to_channel(source_bss->channel->center_freq);
  2348. /* operating class */
  2349. elem = cfg80211_find_elem(WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  2350. ies->data, ies->len);
  2351. if (elem && elem->datalen >= 1) {
  2352. ap_info.op_class = elem->data[0];
  2353. } else {
  2354. struct cfg80211_chan_def chandef;
  2355. /* The AP is not providing us with anything to work with. So
  2356. * make up a somewhat reasonable operating class, but don't
  2357. * bother with it too much as no one will ever use the
  2358. * information.
  2359. */
  2360. cfg80211_chandef_create(&chandef, source_bss->channel,
  2361. NL80211_CHAN_NO_HT);
  2362. if (!ieee80211_chandef_to_operating_class(&chandef,
  2363. &ap_info.op_class))
  2364. goto out_unlock;
  2365. }
  2366. /* Just set TBTT offset and PSD 20 to invalid/unknown */
  2367. tbtt_info.tbtt_offset = 255;
  2368. tbtt_info.psd_20 = IEEE80211_RNR_TBTT_PARAMS_PSD_RESERVED;
  2369. memcpy(tbtt_info.bssid, source_bss->bssid, ETH_ALEN);
  2370. if (cfg80211_calc_short_ssid(ies, &elem, &short_ssid))
  2371. goto out_unlock;
  2372. rcu_read_unlock();
  2373. tbtt_info.short_ssid = cpu_to_le32(short_ssid);
  2374. tbtt_info.bss_params = IEEE80211_RNR_TBTT_PARAMS_SAME_SSID;
  2375. if (is_mbssid) {
  2376. tbtt_info.bss_params |= IEEE80211_RNR_TBTT_PARAMS_MULTI_BSSID;
  2377. tbtt_info.bss_params |= IEEE80211_RNR_TBTT_PARAMS_TRANSMITTED_BSSID;
  2378. }
  2379. tbtt_info.mld_params.mld_id = 0;
  2380. tbtt_info.mld_params.params =
  2381. le16_encode_bits(link_id, IEEE80211_RNR_MLD_PARAMS_LINK_ID) |
  2382. le16_encode_bits(bss_change_count,
  2383. IEEE80211_RNR_MLD_PARAMS_BSS_CHANGE_COUNT);
  2384. res = kzalloc(struct_size(res, data,
  2385. sizeof(ap_info) + ap_info.tbtt_info_len),
  2386. gfp);
  2387. if (!res)
  2388. return NULL;
  2389. /* Copy the data */
  2390. res->id = WLAN_EID_REDUCED_NEIGHBOR_REPORT;
  2391. res->datalen = sizeof(ap_info) + ap_info.tbtt_info_len;
  2392. memcpy(res->data, &ap_info, sizeof(ap_info));
  2393. memcpy(res->data + sizeof(ap_info), &tbtt_info, ap_info.tbtt_info_len);
  2394. return res;
  2395. out_unlock:
  2396. rcu_read_unlock();
  2397. return NULL;
  2398. }
  2399. static void
  2400. cfg80211_parse_ml_elem_sta_data(struct wiphy *wiphy,
  2401. struct cfg80211_inform_single_bss_data *tx_data,
  2402. struct cfg80211_bss *source_bss,
  2403. const struct element *elem,
  2404. gfp_t gfp)
  2405. {
  2406. struct cfg80211_inform_single_bss_data data = {
  2407. .drv_data = tx_data->drv_data,
  2408. .ftype = tx_data->ftype,
  2409. .source_bss = source_bss,
  2410. .bss_source = BSS_SOURCE_STA_PROFILE,
  2411. };
  2412. struct element *reporter_rnr = NULL;
  2413. struct ieee80211_multi_link_elem *ml_elem;
  2414. struct cfg80211_mle *mle;
  2415. const struct element *ssid_elem;
  2416. const u8 *ssid = NULL;
  2417. size_t ssid_len = 0;
  2418. u16 control;
  2419. u8 ml_common_len;
  2420. u8 *new_ie = NULL;
  2421. struct cfg80211_bss *bss;
  2422. u8 mld_id, reporter_link_id, bss_change_count;
  2423. u16 seen_links = 0;
  2424. u8 i;
  2425. if (!ieee80211_mle_type_ok(elem->data + 1,
  2426. IEEE80211_ML_CONTROL_TYPE_BASIC,
  2427. elem->datalen - 1))
  2428. return;
  2429. ml_elem = (void *)(elem->data + 1);
  2430. control = le16_to_cpu(ml_elem->control);
  2431. ml_common_len = ml_elem->variable[0];
  2432. /* Must be present when transmitted by an AP (in a probe response) */
  2433. if (!(control & IEEE80211_MLC_BASIC_PRES_BSS_PARAM_CH_CNT) ||
  2434. !(control & IEEE80211_MLC_BASIC_PRES_LINK_ID) ||
  2435. !(control & IEEE80211_MLC_BASIC_PRES_MLD_CAPA_OP))
  2436. return;
  2437. reporter_link_id = ieee80211_mle_get_link_id(elem->data + 1);
  2438. bss_change_count = ieee80211_mle_get_bss_param_ch_cnt(elem->data + 1);
  2439. /*
  2440. * The MLD ID of the reporting AP is always zero. It is set if the AP
  2441. * is part of an MBSSID set and will be non-zero for ML Elements
  2442. * relating to a nontransmitted BSS (matching the Multi-BSSID Index,
  2443. * Draft P802.11be_D3.2, 35.3.4.2)
  2444. */
  2445. mld_id = ieee80211_mle_get_mld_id(elem->data + 1);
  2446. /* Fully defrag the ML element for sta information/profile iteration */
  2447. mle = cfg80211_defrag_mle(elem, tx_data->ie, tx_data->ielen, gfp);
  2448. if (!mle)
  2449. return;
  2450. /* No point in doing anything if there is no per-STA profile */
  2451. if (!mle->sta_prof[0])
  2452. goto out;
  2453. new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
  2454. if (!new_ie)
  2455. goto out;
  2456. reporter_rnr = cfg80211_gen_reporter_rnr(source_bss,
  2457. u16_get_bits(control,
  2458. IEEE80211_MLC_BASIC_PRES_MLD_ID),
  2459. mld_id == 0, reporter_link_id,
  2460. bss_change_count,
  2461. gfp);
  2462. ssid_elem = cfg80211_find_elem(WLAN_EID_SSID, tx_data->ie,
  2463. tx_data->ielen);
  2464. if (ssid_elem) {
  2465. ssid = ssid_elem->data;
  2466. ssid_len = ssid_elem->datalen;
  2467. }
  2468. for (i = 0; i < ARRAY_SIZE(mle->sta_prof) && mle->sta_prof[i]; i++) {
  2469. const struct ieee80211_neighbor_ap_info *ap_info;
  2470. enum nl80211_band band;
  2471. u32 freq;
  2472. const u8 *profile;
  2473. ssize_t profile_len;
  2474. u8 param_ch_count;
  2475. u8 link_id, use_for;
  2476. bool non_tx;
  2477. if (!ieee80211_mle_basic_sta_prof_size_ok((u8 *)mle->sta_prof[i],
  2478. mle->sta_prof_len[i]))
  2479. continue;
  2480. control = le16_to_cpu(mle->sta_prof[i]->control);
  2481. if (!(control & IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE))
  2482. continue;
  2483. link_id = u16_get_bits(control,
  2484. IEEE80211_MLE_STA_CONTROL_LINK_ID);
  2485. if (seen_links & BIT(link_id))
  2486. break;
  2487. seen_links |= BIT(link_id);
  2488. if (!(control & IEEE80211_MLE_STA_CONTROL_BEACON_INT_PRESENT) ||
  2489. !(control & IEEE80211_MLE_STA_CONTROL_TSF_OFFS_PRESENT) ||
  2490. !(control & IEEE80211_MLE_STA_CONTROL_STA_MAC_ADDR_PRESENT))
  2491. continue;
  2492. memcpy(data.bssid, mle->sta_prof[i]->variable, ETH_ALEN);
  2493. data.beacon_interval =
  2494. get_unaligned_le16(mle->sta_prof[i]->variable + 6);
  2495. data.tsf = tx_data->tsf +
  2496. get_unaligned_le64(mle->sta_prof[i]->variable + 8);
  2497. /* sta_info_len counts itself */
  2498. profile = mle->sta_prof[i]->variable +
  2499. mle->sta_prof[i]->sta_info_len - 1;
  2500. profile_len = (u8 *)mle->sta_prof[i] + mle->sta_prof_len[i] -
  2501. profile;
  2502. if (profile_len < 2)
  2503. continue;
  2504. data.capability = get_unaligned_le16(profile);
  2505. profile += 2;
  2506. profile_len -= 2;
  2507. /* Find in RNR to look up channel information */
  2508. use_for = cfg80211_rnr_info_for_mld_ap(tx_data->ie,
  2509. tx_data->ielen,
  2510. mld_id, link_id,
  2511. &ap_info,
  2512. &param_ch_count,
  2513. &non_tx);
  2514. if (!use_for)
  2515. continue;
  2516. /*
  2517. * As of 802.11be_D5.0, the specification does not give us any
  2518. * way of discovering both the MaxBSSID and the Multiple-BSSID
  2519. * Index. It does seem like the Multiple-BSSID Index element
  2520. * may be provided, but section 9.4.2.45 explicitly forbids
  2521. * including a Multiple-BSSID Element (in this case without any
  2522. * subelements).
  2523. * Without both pieces of information we cannot calculate the
  2524. * reference BSSID, so simply ignore the BSS.
  2525. */
  2526. if (non_tx)
  2527. continue;
  2528. /* We could sanity check the BSSID is included */
  2529. if (!ieee80211_operating_class_to_band(ap_info->op_class,
  2530. &band))
  2531. continue;
  2532. freq = ieee80211_channel_to_freq_khz(ap_info->channel, band);
  2533. data.channel = ieee80211_get_channel_khz(wiphy, freq);
  2534. /* Skip if RNR element specifies an unsupported channel */
  2535. if (!data.channel)
  2536. continue;
  2537. /* Skip if BSS entry generated from MBSSID or DIRECT source
  2538. * frame data available already.
  2539. */
  2540. bss = cfg80211_get_bss(wiphy, data.channel, data.bssid, ssid,
  2541. ssid_len, IEEE80211_BSS_TYPE_ANY,
  2542. IEEE80211_PRIVACY_ANY);
  2543. if (bss) {
  2544. struct cfg80211_internal_bss *ibss = bss_from_pub(bss);
  2545. if (data.capability == bss->capability &&
  2546. ibss->bss_source != BSS_SOURCE_STA_PROFILE) {
  2547. cfg80211_put_bss(wiphy, bss);
  2548. continue;
  2549. }
  2550. cfg80211_put_bss(wiphy, bss);
  2551. }
  2552. if (use_for == NL80211_BSS_USE_FOR_MLD_LINK &&
  2553. !(wiphy->flags & WIPHY_FLAG_SUPPORTS_NSTR_NONPRIMARY)) {
  2554. use_for = 0;
  2555. data.cannot_use_reasons =
  2556. NL80211_BSS_CANNOT_USE_NSTR_NONPRIMARY;
  2557. }
  2558. data.use_for = use_for;
  2559. /* Generate new elements */
  2560. memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
  2561. data.ie = new_ie;
  2562. data.ielen = cfg80211_gen_new_ie(tx_data->ie, tx_data->ielen,
  2563. profile, profile_len,
  2564. new_ie,
  2565. IEEE80211_MAX_DATA_LEN);
  2566. if (!data.ielen)
  2567. continue;
  2568. /* The generated elements do not contain:
  2569. * - Basic ML element
  2570. * - A TBTT entry in the RNR for the transmitting AP
  2571. *
  2572. * This information is needed both internally and in userspace
  2573. * as such, we should append it here.
  2574. */
  2575. if (data.ielen + 3 + sizeof(*ml_elem) + ml_common_len >
  2576. IEEE80211_MAX_DATA_LEN)
  2577. continue;
  2578. /* Copy the Basic Multi-Link element including the common
  2579. * information, and then fix up the link ID and BSS param
  2580. * change count.
  2581. * Note that the ML element length has been verified and we
  2582. * also checked that it contains the link ID.
  2583. */
  2584. new_ie[data.ielen++] = WLAN_EID_EXTENSION;
  2585. new_ie[data.ielen++] = 1 + sizeof(*ml_elem) + ml_common_len;
  2586. new_ie[data.ielen++] = WLAN_EID_EXT_EHT_MULTI_LINK;
  2587. memcpy(new_ie + data.ielen, ml_elem,
  2588. sizeof(*ml_elem) + ml_common_len);
  2589. new_ie[data.ielen + sizeof(*ml_elem) + 1 + ETH_ALEN] = link_id;
  2590. new_ie[data.ielen + sizeof(*ml_elem) + 1 + ETH_ALEN + 1] =
  2591. param_ch_count;
  2592. data.ielen += sizeof(*ml_elem) + ml_common_len;
  2593. if (reporter_rnr && (use_for & NL80211_BSS_USE_FOR_NORMAL)) {
  2594. if (data.ielen + sizeof(struct element) +
  2595. reporter_rnr->datalen > IEEE80211_MAX_DATA_LEN)
  2596. continue;
  2597. memcpy(new_ie + data.ielen, reporter_rnr,
  2598. sizeof(struct element) + reporter_rnr->datalen);
  2599. data.ielen += sizeof(struct element) +
  2600. reporter_rnr->datalen;
  2601. }
  2602. bss = cfg80211_inform_single_bss_data(wiphy, &data, gfp);
  2603. if (!bss)
  2604. break;
  2605. cfg80211_put_bss(wiphy, bss);
  2606. }
  2607. out:
  2608. kfree(reporter_rnr);
  2609. kfree(new_ie);
  2610. kfree(mle);
  2611. }
  2612. static void cfg80211_parse_ml_sta_data(struct wiphy *wiphy,
  2613. struct cfg80211_inform_single_bss_data *tx_data,
  2614. struct cfg80211_bss *source_bss,
  2615. gfp_t gfp)
  2616. {
  2617. const struct element *elem;
  2618. if (!source_bss)
  2619. return;
  2620. if (tx_data->ftype != CFG80211_BSS_FTYPE_PRESP)
  2621. return;
  2622. for_each_element_extid(elem, WLAN_EID_EXT_EHT_MULTI_LINK,
  2623. tx_data->ie, tx_data->ielen)
  2624. cfg80211_parse_ml_elem_sta_data(wiphy, tx_data, source_bss,
  2625. elem, gfp);
  2626. }
  2627. struct cfg80211_bss *
  2628. cfg80211_inform_bss_data(struct wiphy *wiphy,
  2629. struct cfg80211_inform_bss *data,
  2630. enum cfg80211_bss_frame_type ftype,
  2631. const u8 *bssid, u64 tsf, u16 capability,
  2632. u16 beacon_interval, const u8 *ie, size_t ielen,
  2633. gfp_t gfp)
  2634. {
  2635. struct cfg80211_inform_single_bss_data inform_data = {
  2636. .drv_data = data,
  2637. .ftype = ftype,
  2638. .tsf = tsf,
  2639. .capability = capability,
  2640. .beacon_interval = beacon_interval,
  2641. .ie = ie,
  2642. .ielen = ielen,
  2643. .use_for = data->restrict_use ?
  2644. data->use_for :
  2645. NL80211_BSS_USE_FOR_ALL,
  2646. .cannot_use_reasons = data->cannot_use_reasons,
  2647. };
  2648. struct cfg80211_bss *res;
  2649. memcpy(inform_data.bssid, bssid, ETH_ALEN);
  2650. res = cfg80211_inform_single_bss_data(wiphy, &inform_data, gfp);
  2651. if (!res)
  2652. return NULL;
  2653. /* don't do any further MBSSID/ML handling for S1G */
  2654. if (ftype == CFG80211_BSS_FTYPE_S1G_BEACON)
  2655. return res;
  2656. cfg80211_parse_mbssid_data(wiphy, &inform_data, res, gfp);
  2657. cfg80211_parse_ml_sta_data(wiphy, &inform_data, res, gfp);
  2658. return res;
  2659. }
  2660. EXPORT_SYMBOL(cfg80211_inform_bss_data);
  2661. struct cfg80211_bss *
  2662. cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
  2663. struct cfg80211_inform_bss *data,
  2664. struct ieee80211_mgmt *mgmt, size_t len,
  2665. gfp_t gfp)
  2666. {
  2667. size_t min_hdr_len;
  2668. struct ieee80211_ext *ext = NULL;
  2669. enum cfg80211_bss_frame_type ftype;
  2670. u16 beacon_interval;
  2671. const u8 *bssid;
  2672. u16 capability;
  2673. const u8 *ie;
  2674. size_t ielen;
  2675. u64 tsf;
  2676. size_t s1g_optional_len;
  2677. if (WARN_ON(!mgmt))
  2678. return NULL;
  2679. if (WARN_ON(!wiphy))
  2680. return NULL;
  2681. BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
  2682. offsetof(struct ieee80211_mgmt, u.beacon.variable));
  2683. trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
  2684. if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
  2685. ext = (void *) mgmt;
  2686. s1g_optional_len =
  2687. ieee80211_s1g_optional_len(ext->frame_control);
  2688. min_hdr_len =
  2689. offsetof(struct ieee80211_ext, u.s1g_beacon.variable) +
  2690. s1g_optional_len;
  2691. } else {
  2692. /* same for beacons */
  2693. min_hdr_len = offsetof(struct ieee80211_mgmt,
  2694. u.probe_resp.variable);
  2695. }
  2696. if (WARN_ON(len < min_hdr_len))
  2697. return NULL;
  2698. ielen = len - min_hdr_len;
  2699. ie = mgmt->u.probe_resp.variable;
  2700. if (ext) {
  2701. const struct ieee80211_s1g_bcn_compat_ie *compat;
  2702. const struct element *elem;
  2703. ie = ext->u.s1g_beacon.variable + s1g_optional_len;
  2704. elem = cfg80211_find_elem(WLAN_EID_S1G_BCN_COMPAT, ie, ielen);
  2705. if (!elem)
  2706. return NULL;
  2707. if (elem->datalen < sizeof(*compat))
  2708. return NULL;
  2709. compat = (void *)elem->data;
  2710. bssid = ext->u.s1g_beacon.sa;
  2711. capability = le16_to_cpu(compat->compat_info);
  2712. beacon_interval = le16_to_cpu(compat->beacon_int);
  2713. } else {
  2714. bssid = mgmt->bssid;
  2715. beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
  2716. capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
  2717. }
  2718. tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
  2719. if (ieee80211_is_probe_resp(mgmt->frame_control))
  2720. ftype = CFG80211_BSS_FTYPE_PRESP;
  2721. else if (ext)
  2722. ftype = CFG80211_BSS_FTYPE_S1G_BEACON;
  2723. else
  2724. ftype = CFG80211_BSS_FTYPE_BEACON;
  2725. return cfg80211_inform_bss_data(wiphy, data, ftype,
  2726. bssid, tsf, capability,
  2727. beacon_interval, ie, ielen,
  2728. gfp);
  2729. }
  2730. EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
  2731. void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  2732. {
  2733. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  2734. if (!pub)
  2735. return;
  2736. spin_lock_bh(&rdev->bss_lock);
  2737. bss_ref_get(rdev, bss_from_pub(pub));
  2738. spin_unlock_bh(&rdev->bss_lock);
  2739. }
  2740. EXPORT_SYMBOL(cfg80211_ref_bss);
  2741. void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  2742. {
  2743. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  2744. if (!pub)
  2745. return;
  2746. spin_lock_bh(&rdev->bss_lock);
  2747. bss_ref_put(rdev, bss_from_pub(pub));
  2748. spin_unlock_bh(&rdev->bss_lock);
  2749. }
  2750. EXPORT_SYMBOL(cfg80211_put_bss);
  2751. void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  2752. {
  2753. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  2754. struct cfg80211_internal_bss *bss, *tmp1;
  2755. struct cfg80211_bss *nontrans_bss, *tmp;
  2756. if (WARN_ON(!pub))
  2757. return;
  2758. bss = bss_from_pub(pub);
  2759. spin_lock_bh(&rdev->bss_lock);
  2760. if (list_empty(&bss->list))
  2761. goto out;
  2762. list_for_each_entry_safe(nontrans_bss, tmp,
  2763. &pub->nontrans_list,
  2764. nontrans_list) {
  2765. tmp1 = bss_from_pub(nontrans_bss);
  2766. if (__cfg80211_unlink_bss(rdev, tmp1))
  2767. rdev->bss_generation++;
  2768. }
  2769. if (__cfg80211_unlink_bss(rdev, bss))
  2770. rdev->bss_generation++;
  2771. out:
  2772. spin_unlock_bh(&rdev->bss_lock);
  2773. }
  2774. EXPORT_SYMBOL(cfg80211_unlink_bss);
  2775. void cfg80211_bss_iter(struct wiphy *wiphy,
  2776. struct cfg80211_chan_def *chandef,
  2777. void (*iter)(struct wiphy *wiphy,
  2778. struct cfg80211_bss *bss,
  2779. void *data),
  2780. void *iter_data)
  2781. {
  2782. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  2783. struct cfg80211_internal_bss *bss;
  2784. spin_lock_bh(&rdev->bss_lock);
  2785. list_for_each_entry(bss, &rdev->bss_list, list) {
  2786. if (!chandef || cfg80211_is_sub_chan(chandef, bss->pub.channel,
  2787. false))
  2788. iter(wiphy, &bss->pub, iter_data);
  2789. }
  2790. spin_unlock_bh(&rdev->bss_lock);
  2791. }
  2792. EXPORT_SYMBOL(cfg80211_bss_iter);
  2793. void cfg80211_update_assoc_bss_entry(struct wireless_dev *wdev,
  2794. unsigned int link_id,
  2795. struct ieee80211_channel *chan)
  2796. {
  2797. struct wiphy *wiphy = wdev->wiphy;
  2798. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  2799. struct cfg80211_internal_bss *cbss = wdev->links[link_id].client.current_bss;
  2800. struct cfg80211_internal_bss *new = NULL;
  2801. struct cfg80211_internal_bss *bss;
  2802. struct cfg80211_bss *nontrans_bss;
  2803. struct cfg80211_bss *tmp;
  2804. spin_lock_bh(&rdev->bss_lock);
  2805. /*
  2806. * Some APs use CSA also for bandwidth changes, i.e., without actually
  2807. * changing the control channel, so no need to update in such a case.
  2808. */
  2809. if (cbss->pub.channel == chan)
  2810. goto done;
  2811. /* use transmitting bss */
  2812. if (cbss->pub.transmitted_bss)
  2813. cbss = bss_from_pub(cbss->pub.transmitted_bss);
  2814. cbss->pub.channel = chan;
  2815. list_for_each_entry(bss, &rdev->bss_list, list) {
  2816. if (!cfg80211_bss_type_match(bss->pub.capability,
  2817. bss->pub.channel->band,
  2818. wdev->conn_bss_type))
  2819. continue;
  2820. if (bss == cbss)
  2821. continue;
  2822. if (!cmp_bss(&bss->pub, &cbss->pub, BSS_CMP_REGULAR)) {
  2823. new = bss;
  2824. break;
  2825. }
  2826. }
  2827. if (new) {
  2828. /* to save time, update IEs for transmitting bss only */
  2829. cfg80211_update_known_bss(rdev, cbss, new, false);
  2830. new->pub.proberesp_ies = NULL;
  2831. new->pub.beacon_ies = NULL;
  2832. list_for_each_entry_safe(nontrans_bss, tmp,
  2833. &new->pub.nontrans_list,
  2834. nontrans_list) {
  2835. bss = bss_from_pub(nontrans_bss);
  2836. if (__cfg80211_unlink_bss(rdev, bss))
  2837. rdev->bss_generation++;
  2838. }
  2839. WARN_ON(atomic_read(&new->hold));
  2840. if (!WARN_ON(!__cfg80211_unlink_bss(rdev, new)))
  2841. rdev->bss_generation++;
  2842. }
  2843. cfg80211_rehash_bss(rdev, cbss);
  2844. list_for_each_entry_safe(nontrans_bss, tmp,
  2845. &cbss->pub.nontrans_list,
  2846. nontrans_list) {
  2847. bss = bss_from_pub(nontrans_bss);
  2848. bss->pub.channel = chan;
  2849. cfg80211_rehash_bss(rdev, bss);
  2850. }
  2851. done:
  2852. spin_unlock_bh(&rdev->bss_lock);
  2853. }
  2854. #ifdef CONFIG_CFG80211_WEXT
  2855. static struct cfg80211_registered_device *
  2856. cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
  2857. {
  2858. struct cfg80211_registered_device *rdev;
  2859. struct net_device *dev;
  2860. ASSERT_RTNL();
  2861. dev = dev_get_by_index(net, ifindex);
  2862. if (!dev)
  2863. return ERR_PTR(-ENODEV);
  2864. if (dev->ieee80211_ptr)
  2865. rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
  2866. else
  2867. rdev = ERR_PTR(-ENODEV);
  2868. dev_put(dev);
  2869. return rdev;
  2870. }
  2871. int cfg80211_wext_siwscan(struct net_device *dev,
  2872. struct iw_request_info *info,
  2873. union iwreq_data *wrqu, char *extra)
  2874. {
  2875. struct cfg80211_registered_device *rdev;
  2876. struct wiphy *wiphy;
  2877. struct iw_scan_req *wreq = NULL;
  2878. struct cfg80211_scan_request *creq;
  2879. int i, err, n_channels = 0;
  2880. enum nl80211_band band;
  2881. if (!netif_running(dev))
  2882. return -ENETDOWN;
  2883. if (wrqu->data.length == sizeof(struct iw_scan_req))
  2884. wreq = (struct iw_scan_req *)extra;
  2885. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  2886. if (IS_ERR(rdev))
  2887. return PTR_ERR(rdev);
  2888. if (rdev->scan_req || rdev->scan_msg)
  2889. return -EBUSY;
  2890. wiphy = &rdev->wiphy;
  2891. /* Determine number of channels, needed to allocate creq */
  2892. if (wreq && wreq->num_channels) {
  2893. /* Passed from userspace so should be checked */
  2894. if (unlikely(wreq->num_channels > IW_MAX_FREQUENCIES))
  2895. return -EINVAL;
  2896. n_channels = wreq->num_channels;
  2897. } else {
  2898. n_channels = ieee80211_get_num_supported_channels(wiphy);
  2899. }
  2900. creq = kzalloc(struct_size(creq, channels, n_channels) +
  2901. sizeof(struct cfg80211_ssid),
  2902. GFP_ATOMIC);
  2903. if (!creq)
  2904. return -ENOMEM;
  2905. creq->wiphy = wiphy;
  2906. creq->wdev = dev->ieee80211_ptr;
  2907. /* SSIDs come after channels */
  2908. creq->ssids = (void *)creq + struct_size(creq, channels, n_channels);
  2909. creq->n_channels = n_channels;
  2910. creq->n_ssids = 1;
  2911. creq->scan_start = jiffies;
  2912. /* translate "Scan on frequencies" request */
  2913. i = 0;
  2914. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  2915. int j;
  2916. if (!wiphy->bands[band])
  2917. continue;
  2918. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  2919. /* ignore disabled channels */
  2920. if (wiphy->bands[band]->channels[j].flags &
  2921. IEEE80211_CHAN_DISABLED)
  2922. continue;
  2923. /* If we have a wireless request structure and the
  2924. * wireless request specifies frequencies, then search
  2925. * for the matching hardware channel.
  2926. */
  2927. if (wreq && wreq->num_channels) {
  2928. int k;
  2929. int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
  2930. for (k = 0; k < wreq->num_channels; k++) {
  2931. struct iw_freq *freq =
  2932. &wreq->channel_list[k];
  2933. int wext_freq =
  2934. cfg80211_wext_freq(freq);
  2935. if (wext_freq == wiphy_freq)
  2936. goto wext_freq_found;
  2937. }
  2938. goto wext_freq_not_found;
  2939. }
  2940. wext_freq_found:
  2941. creq->channels[i] = &wiphy->bands[band]->channels[j];
  2942. i++;
  2943. wext_freq_not_found: ;
  2944. }
  2945. }
  2946. /* No channels found? */
  2947. if (!i) {
  2948. err = -EINVAL;
  2949. goto out;
  2950. }
  2951. /* Set real number of channels specified in creq->channels[] */
  2952. creq->n_channels = i;
  2953. /* translate "Scan for SSID" request */
  2954. if (wreq) {
  2955. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  2956. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
  2957. err = -EINVAL;
  2958. goto out;
  2959. }
  2960. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  2961. creq->ssids[0].ssid_len = wreq->essid_len;
  2962. }
  2963. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE) {
  2964. creq->ssids = NULL;
  2965. creq->n_ssids = 0;
  2966. }
  2967. }
  2968. for (i = 0; i < NUM_NL80211_BANDS; i++)
  2969. if (wiphy->bands[i])
  2970. creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
  2971. eth_broadcast_addr(creq->bssid);
  2972. wiphy_lock(&rdev->wiphy);
  2973. rdev->scan_req = creq;
  2974. err = rdev_scan(rdev, creq);
  2975. if (err) {
  2976. rdev->scan_req = NULL;
  2977. /* creq will be freed below */
  2978. } else {
  2979. nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
  2980. /* creq now owned by driver */
  2981. creq = NULL;
  2982. dev_hold(dev);
  2983. }
  2984. wiphy_unlock(&rdev->wiphy);
  2985. out:
  2986. kfree(creq);
  2987. return err;
  2988. }
  2989. EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
  2990. static char *ieee80211_scan_add_ies(struct iw_request_info *info,
  2991. const struct cfg80211_bss_ies *ies,
  2992. char *current_ev, char *end_buf)
  2993. {
  2994. const u8 *pos, *end, *next;
  2995. struct iw_event iwe;
  2996. if (!ies)
  2997. return current_ev;
  2998. /*
  2999. * If needed, fragment the IEs buffer (at IE boundaries) into short
  3000. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  3001. */
  3002. pos = ies->data;
  3003. end = pos + ies->len;
  3004. while (end - pos > IW_GENERIC_IE_MAX) {
  3005. next = pos + 2 + pos[1];
  3006. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  3007. next = next + 2 + next[1];
  3008. memset(&iwe, 0, sizeof(iwe));
  3009. iwe.cmd = IWEVGENIE;
  3010. iwe.u.data.length = next - pos;
  3011. current_ev = iwe_stream_add_point_check(info, current_ev,
  3012. end_buf, &iwe,
  3013. (void *)pos);
  3014. if (IS_ERR(current_ev))
  3015. return current_ev;
  3016. pos = next;
  3017. }
  3018. if (end > pos) {
  3019. memset(&iwe, 0, sizeof(iwe));
  3020. iwe.cmd = IWEVGENIE;
  3021. iwe.u.data.length = end - pos;
  3022. current_ev = iwe_stream_add_point_check(info, current_ev,
  3023. end_buf, &iwe,
  3024. (void *)pos);
  3025. if (IS_ERR(current_ev))
  3026. return current_ev;
  3027. }
  3028. return current_ev;
  3029. }
  3030. static char *
  3031. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  3032. struct cfg80211_internal_bss *bss, char *current_ev,
  3033. char *end_buf)
  3034. {
  3035. const struct cfg80211_bss_ies *ies;
  3036. struct iw_event iwe;
  3037. const u8 *ie;
  3038. u8 buf[50];
  3039. u8 *cfg, *p, *tmp;
  3040. int rem, i, sig;
  3041. bool ismesh = false;
  3042. memset(&iwe, 0, sizeof(iwe));
  3043. iwe.cmd = SIOCGIWAP;
  3044. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  3045. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  3046. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  3047. IW_EV_ADDR_LEN);
  3048. if (IS_ERR(current_ev))
  3049. return current_ev;
  3050. memset(&iwe, 0, sizeof(iwe));
  3051. iwe.cmd = SIOCGIWFREQ;
  3052. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  3053. iwe.u.freq.e = 0;
  3054. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  3055. IW_EV_FREQ_LEN);
  3056. if (IS_ERR(current_ev))
  3057. return current_ev;
  3058. memset(&iwe, 0, sizeof(iwe));
  3059. iwe.cmd = SIOCGIWFREQ;
  3060. iwe.u.freq.m = bss->pub.channel->center_freq;
  3061. iwe.u.freq.e = 6;
  3062. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  3063. IW_EV_FREQ_LEN);
  3064. if (IS_ERR(current_ev))
  3065. return current_ev;
  3066. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  3067. memset(&iwe, 0, sizeof(iwe));
  3068. iwe.cmd = IWEVQUAL;
  3069. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  3070. IW_QUAL_NOISE_INVALID |
  3071. IW_QUAL_QUAL_UPDATED;
  3072. switch (wiphy->signal_type) {
  3073. case CFG80211_SIGNAL_TYPE_MBM:
  3074. sig = bss->pub.signal / 100;
  3075. iwe.u.qual.level = sig;
  3076. iwe.u.qual.updated |= IW_QUAL_DBM;
  3077. if (sig < -110) /* rather bad */
  3078. sig = -110;
  3079. else if (sig > -40) /* perfect */
  3080. sig = -40;
  3081. /* will give a range of 0 .. 70 */
  3082. iwe.u.qual.qual = sig + 110;
  3083. break;
  3084. case CFG80211_SIGNAL_TYPE_UNSPEC:
  3085. iwe.u.qual.level = bss->pub.signal;
  3086. /* will give range 0 .. 100 */
  3087. iwe.u.qual.qual = bss->pub.signal;
  3088. break;
  3089. default:
  3090. /* not reached */
  3091. break;
  3092. }
  3093. current_ev = iwe_stream_add_event_check(info, current_ev,
  3094. end_buf, &iwe,
  3095. IW_EV_QUAL_LEN);
  3096. if (IS_ERR(current_ev))
  3097. return current_ev;
  3098. }
  3099. memset(&iwe, 0, sizeof(iwe));
  3100. iwe.cmd = SIOCGIWENCODE;
  3101. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  3102. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  3103. else
  3104. iwe.u.data.flags = IW_ENCODE_DISABLED;
  3105. iwe.u.data.length = 0;
  3106. current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
  3107. &iwe, "");
  3108. if (IS_ERR(current_ev))
  3109. return current_ev;
  3110. rcu_read_lock();
  3111. ies = rcu_dereference(bss->pub.ies);
  3112. rem = ies->len;
  3113. ie = ies->data;
  3114. while (rem >= 2) {
  3115. /* invalid data */
  3116. if (ie[1] > rem - 2)
  3117. break;
  3118. switch (ie[0]) {
  3119. case WLAN_EID_SSID:
  3120. memset(&iwe, 0, sizeof(iwe));
  3121. iwe.cmd = SIOCGIWESSID;
  3122. iwe.u.data.length = ie[1];
  3123. iwe.u.data.flags = 1;
  3124. current_ev = iwe_stream_add_point_check(info,
  3125. current_ev,
  3126. end_buf, &iwe,
  3127. (u8 *)ie + 2);
  3128. if (IS_ERR(current_ev))
  3129. goto unlock;
  3130. break;
  3131. case WLAN_EID_MESH_ID:
  3132. memset(&iwe, 0, sizeof(iwe));
  3133. iwe.cmd = SIOCGIWESSID;
  3134. iwe.u.data.length = ie[1];
  3135. iwe.u.data.flags = 1;
  3136. current_ev = iwe_stream_add_point_check(info,
  3137. current_ev,
  3138. end_buf, &iwe,
  3139. (u8 *)ie + 2);
  3140. if (IS_ERR(current_ev))
  3141. goto unlock;
  3142. break;
  3143. case WLAN_EID_MESH_CONFIG:
  3144. ismesh = true;
  3145. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  3146. break;
  3147. cfg = (u8 *)ie + 2;
  3148. memset(&iwe, 0, sizeof(iwe));
  3149. iwe.cmd = IWEVCUSTOM;
  3150. iwe.u.data.length = sprintf(buf,
  3151. "Mesh Network Path Selection Protocol ID: 0x%02X",
  3152. cfg[0]);
  3153. current_ev = iwe_stream_add_point_check(info,
  3154. current_ev,
  3155. end_buf,
  3156. &iwe, buf);
  3157. if (IS_ERR(current_ev))
  3158. goto unlock;
  3159. iwe.u.data.length = sprintf(buf,
  3160. "Path Selection Metric ID: 0x%02X",
  3161. cfg[1]);
  3162. current_ev = iwe_stream_add_point_check(info,
  3163. current_ev,
  3164. end_buf,
  3165. &iwe, buf);
  3166. if (IS_ERR(current_ev))
  3167. goto unlock;
  3168. iwe.u.data.length = sprintf(buf,
  3169. "Congestion Control Mode ID: 0x%02X",
  3170. cfg[2]);
  3171. current_ev = iwe_stream_add_point_check(info,
  3172. current_ev,
  3173. end_buf,
  3174. &iwe, buf);
  3175. if (IS_ERR(current_ev))
  3176. goto unlock;
  3177. iwe.u.data.length = sprintf(buf,
  3178. "Synchronization ID: 0x%02X",
  3179. cfg[3]);
  3180. current_ev = iwe_stream_add_point_check(info,
  3181. current_ev,
  3182. end_buf,
  3183. &iwe, buf);
  3184. if (IS_ERR(current_ev))
  3185. goto unlock;
  3186. iwe.u.data.length = sprintf(buf,
  3187. "Authentication ID: 0x%02X",
  3188. cfg[4]);
  3189. current_ev = iwe_stream_add_point_check(info,
  3190. current_ev,
  3191. end_buf,
  3192. &iwe, buf);
  3193. if (IS_ERR(current_ev))
  3194. goto unlock;
  3195. iwe.u.data.length = sprintf(buf,
  3196. "Formation Info: 0x%02X",
  3197. cfg[5]);
  3198. current_ev = iwe_stream_add_point_check(info,
  3199. current_ev,
  3200. end_buf,
  3201. &iwe, buf);
  3202. if (IS_ERR(current_ev))
  3203. goto unlock;
  3204. iwe.u.data.length = sprintf(buf,
  3205. "Capabilities: 0x%02X",
  3206. cfg[6]);
  3207. current_ev = iwe_stream_add_point_check(info,
  3208. current_ev,
  3209. end_buf,
  3210. &iwe, buf);
  3211. if (IS_ERR(current_ev))
  3212. goto unlock;
  3213. break;
  3214. case WLAN_EID_SUPP_RATES:
  3215. case WLAN_EID_EXT_SUPP_RATES:
  3216. /* display all supported rates in readable format */
  3217. p = current_ev + iwe_stream_lcp_len(info);
  3218. memset(&iwe, 0, sizeof(iwe));
  3219. iwe.cmd = SIOCGIWRATE;
  3220. /* Those two flags are ignored... */
  3221. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  3222. for (i = 0; i < ie[1]; i++) {
  3223. iwe.u.bitrate.value =
  3224. ((ie[i + 2] & 0x7f) * 500000);
  3225. tmp = p;
  3226. p = iwe_stream_add_value(info, current_ev, p,
  3227. end_buf, &iwe,
  3228. IW_EV_PARAM_LEN);
  3229. if (p == tmp) {
  3230. current_ev = ERR_PTR(-E2BIG);
  3231. goto unlock;
  3232. }
  3233. }
  3234. current_ev = p;
  3235. break;
  3236. }
  3237. rem -= ie[1] + 2;
  3238. ie += ie[1] + 2;
  3239. }
  3240. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
  3241. ismesh) {
  3242. memset(&iwe, 0, sizeof(iwe));
  3243. iwe.cmd = SIOCGIWMODE;
  3244. if (ismesh)
  3245. iwe.u.mode = IW_MODE_MESH;
  3246. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  3247. iwe.u.mode = IW_MODE_MASTER;
  3248. else
  3249. iwe.u.mode = IW_MODE_ADHOC;
  3250. current_ev = iwe_stream_add_event_check(info, current_ev,
  3251. end_buf, &iwe,
  3252. IW_EV_UINT_LEN);
  3253. if (IS_ERR(current_ev))
  3254. goto unlock;
  3255. }
  3256. memset(&iwe, 0, sizeof(iwe));
  3257. iwe.cmd = IWEVCUSTOM;
  3258. iwe.u.data.length = sprintf(buf, "tsf=%016llx",
  3259. (unsigned long long)(ies->tsf));
  3260. current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
  3261. &iwe, buf);
  3262. if (IS_ERR(current_ev))
  3263. goto unlock;
  3264. memset(&iwe, 0, sizeof(iwe));
  3265. iwe.cmd = IWEVCUSTOM;
  3266. iwe.u.data.length = sprintf(buf, " Last beacon: %ums ago",
  3267. elapsed_jiffies_msecs(bss->ts));
  3268. current_ev = iwe_stream_add_point_check(info, current_ev,
  3269. end_buf, &iwe, buf);
  3270. if (IS_ERR(current_ev))
  3271. goto unlock;
  3272. current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
  3273. unlock:
  3274. rcu_read_unlock();
  3275. return current_ev;
  3276. }
  3277. static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
  3278. struct iw_request_info *info,
  3279. char *buf, size_t len)
  3280. {
  3281. char *current_ev = buf;
  3282. char *end_buf = buf + len;
  3283. struct cfg80211_internal_bss *bss;
  3284. int err = 0;
  3285. spin_lock_bh(&rdev->bss_lock);
  3286. cfg80211_bss_expire(rdev);
  3287. list_for_each_entry(bss, &rdev->bss_list, list) {
  3288. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  3289. err = -E2BIG;
  3290. break;
  3291. }
  3292. current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
  3293. current_ev, end_buf);
  3294. if (IS_ERR(current_ev)) {
  3295. err = PTR_ERR(current_ev);
  3296. break;
  3297. }
  3298. }
  3299. spin_unlock_bh(&rdev->bss_lock);
  3300. if (err)
  3301. return err;
  3302. return current_ev - buf;
  3303. }
  3304. int cfg80211_wext_giwscan(struct net_device *dev,
  3305. struct iw_request_info *info,
  3306. union iwreq_data *wrqu, char *extra)
  3307. {
  3308. struct iw_point *data = &wrqu->data;
  3309. struct cfg80211_registered_device *rdev;
  3310. int res;
  3311. if (!netif_running(dev))
  3312. return -ENETDOWN;
  3313. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  3314. if (IS_ERR(rdev))
  3315. return PTR_ERR(rdev);
  3316. if (rdev->scan_req || rdev->scan_msg)
  3317. return -EAGAIN;
  3318. res = ieee80211_scan_results(rdev, info, extra, data->length);
  3319. data->length = 0;
  3320. if (res >= 0) {
  3321. data->length = res;
  3322. res = 0;
  3323. }
  3324. return res;
  3325. }
  3326. EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
  3327. #endif