util.c 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2002-2005, Instant802 Networks, Inc.
  4. * Copyright 2005-2006, Devicescape Software, Inc.
  5. * Copyright 2006-2007 Jiri Benc <[email protected]>
  6. * Copyright 2007 Johannes Berg <[email protected]>
  7. * Copyright 2013-2014 Intel Mobile Communications GmbH
  8. * Copyright (C) 2015-2017 Intel Deutschland GmbH
  9. * Copyright (C) 2018-2022 Intel Corporation
  10. *
  11. * utilities for mac80211
  12. */
  13. #include <net/mac80211.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/export.h>
  16. #include <linux/types.h>
  17. #include <linux/slab.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/if_arp.h>
  21. #include <linux/bitmap.h>
  22. #include <linux/crc32.h>
  23. #include <net/net_namespace.h>
  24. #include <net/cfg80211.h>
  25. #include <net/rtnetlink.h>
  26. #include "ieee80211_i.h"
  27. #include "driver-ops.h"
  28. #include "rate.h"
  29. #include "mesh.h"
  30. #include "wme.h"
  31. #include "led.h"
  32. #include "wep.h"
  33. /* privid for wiphys to determine whether they belong to us or not */
  34. const void *const mac80211_wiphy_privid = &mac80211_wiphy_privid;
  35. struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
  36. {
  37. struct ieee80211_local *local;
  38. local = wiphy_priv(wiphy);
  39. return &local->hw;
  40. }
  41. EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
  42. u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
  43. enum nl80211_iftype type)
  44. {
  45. __le16 fc = hdr->frame_control;
  46. if (ieee80211_is_data(fc)) {
  47. if (len < 24) /* drop incorrect hdr len (data) */
  48. return NULL;
  49. if (ieee80211_has_a4(fc))
  50. return NULL;
  51. if (ieee80211_has_tods(fc))
  52. return hdr->addr1;
  53. if (ieee80211_has_fromds(fc))
  54. return hdr->addr2;
  55. return hdr->addr3;
  56. }
  57. if (ieee80211_is_s1g_beacon(fc)) {
  58. struct ieee80211_ext *ext = (void *) hdr;
  59. return ext->u.s1g_beacon.sa;
  60. }
  61. if (ieee80211_is_mgmt(fc)) {
  62. if (len < 24) /* drop incorrect hdr len (mgmt) */
  63. return NULL;
  64. return hdr->addr3;
  65. }
  66. if (ieee80211_is_ctl(fc)) {
  67. if (ieee80211_is_pspoll(fc))
  68. return hdr->addr1;
  69. if (ieee80211_is_back_req(fc)) {
  70. switch (type) {
  71. case NL80211_IFTYPE_STATION:
  72. return hdr->addr2;
  73. case NL80211_IFTYPE_AP:
  74. case NL80211_IFTYPE_AP_VLAN:
  75. return hdr->addr1;
  76. default:
  77. break; /* fall through to the return */
  78. }
  79. }
  80. }
  81. return NULL;
  82. }
  83. EXPORT_SYMBOL(ieee80211_get_bssid);
  84. void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
  85. {
  86. struct sk_buff *skb;
  87. struct ieee80211_hdr *hdr;
  88. skb_queue_walk(&tx->skbs, skb) {
  89. hdr = (struct ieee80211_hdr *) skb->data;
  90. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  91. }
  92. }
  93. int ieee80211_frame_duration(enum nl80211_band band, size_t len,
  94. int rate, int erp, int short_preamble,
  95. int shift)
  96. {
  97. int dur;
  98. /* calculate duration (in microseconds, rounded up to next higher
  99. * integer if it includes a fractional microsecond) to send frame of
  100. * len bytes (does not include FCS) at the given rate. Duration will
  101. * also include SIFS.
  102. *
  103. * rate is in 100 kbps, so divident is multiplied by 10 in the
  104. * DIV_ROUND_UP() operations.
  105. *
  106. * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and
  107. * is assumed to be 0 otherwise.
  108. */
  109. if (band == NL80211_BAND_5GHZ || erp) {
  110. /*
  111. * OFDM:
  112. *
  113. * N_DBPS = DATARATE x 4
  114. * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
  115. * (16 = SIGNAL time, 6 = tail bits)
  116. * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
  117. *
  118. * T_SYM = 4 usec
  119. * 802.11a - 18.5.2: aSIFSTime = 16 usec
  120. * 802.11g - 19.8.4: aSIFSTime = 10 usec +
  121. * signal ext = 6 usec
  122. */
  123. dur = 16; /* SIFS + signal ext */
  124. dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */
  125. dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */
  126. /* IEEE 802.11-2012 18.3.2.4: all values above are:
  127. * * times 4 for 5 MHz
  128. * * times 2 for 10 MHz
  129. */
  130. dur *= 1 << shift;
  131. /* rates should already consider the channel bandwidth,
  132. * don't apply divisor again.
  133. */
  134. dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
  135. 4 * rate); /* T_SYM x N_SYM */
  136. } else {
  137. /*
  138. * 802.11b or 802.11g with 802.11b compatibility:
  139. * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
  140. * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
  141. *
  142. * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
  143. * aSIFSTime = 10 usec
  144. * aPreambleLength = 144 usec or 72 usec with short preamble
  145. * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
  146. */
  147. dur = 10; /* aSIFSTime = 10 usec */
  148. dur += short_preamble ? (72 + 24) : (144 + 48);
  149. dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
  150. }
  151. return dur;
  152. }
  153. /* Exported duration function for driver use */
  154. __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
  155. struct ieee80211_vif *vif,
  156. enum nl80211_band band,
  157. size_t frame_len,
  158. struct ieee80211_rate *rate)
  159. {
  160. struct ieee80211_sub_if_data *sdata;
  161. u16 dur;
  162. int erp, shift = 0;
  163. bool short_preamble = false;
  164. erp = 0;
  165. if (vif) {
  166. sdata = vif_to_sdata(vif);
  167. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  168. if (sdata->deflink.operating_11g_mode)
  169. erp = rate->flags & IEEE80211_RATE_ERP_G;
  170. shift = ieee80211_vif_get_shift(vif);
  171. }
  172. dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
  173. short_preamble, shift);
  174. return cpu_to_le16(dur);
  175. }
  176. EXPORT_SYMBOL(ieee80211_generic_frame_duration);
  177. __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
  178. struct ieee80211_vif *vif, size_t frame_len,
  179. const struct ieee80211_tx_info *frame_txctl)
  180. {
  181. struct ieee80211_local *local = hw_to_local(hw);
  182. struct ieee80211_rate *rate;
  183. struct ieee80211_sub_if_data *sdata;
  184. bool short_preamble;
  185. int erp, shift = 0, bitrate;
  186. u16 dur;
  187. struct ieee80211_supported_band *sband;
  188. sband = local->hw.wiphy->bands[frame_txctl->band];
  189. short_preamble = false;
  190. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  191. erp = 0;
  192. if (vif) {
  193. sdata = vif_to_sdata(vif);
  194. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  195. if (sdata->deflink.operating_11g_mode)
  196. erp = rate->flags & IEEE80211_RATE_ERP_G;
  197. shift = ieee80211_vif_get_shift(vif);
  198. }
  199. bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
  200. /* CTS duration */
  201. dur = ieee80211_frame_duration(sband->band, 10, bitrate,
  202. erp, short_preamble, shift);
  203. /* Data frame duration */
  204. dur += ieee80211_frame_duration(sband->band, frame_len, bitrate,
  205. erp, short_preamble, shift);
  206. /* ACK duration */
  207. dur += ieee80211_frame_duration(sband->band, 10, bitrate,
  208. erp, short_preamble, shift);
  209. return cpu_to_le16(dur);
  210. }
  211. EXPORT_SYMBOL(ieee80211_rts_duration);
  212. __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
  213. struct ieee80211_vif *vif,
  214. size_t frame_len,
  215. const struct ieee80211_tx_info *frame_txctl)
  216. {
  217. struct ieee80211_local *local = hw_to_local(hw);
  218. struct ieee80211_rate *rate;
  219. struct ieee80211_sub_if_data *sdata;
  220. bool short_preamble;
  221. int erp, shift = 0, bitrate;
  222. u16 dur;
  223. struct ieee80211_supported_band *sband;
  224. sband = local->hw.wiphy->bands[frame_txctl->band];
  225. short_preamble = false;
  226. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  227. erp = 0;
  228. if (vif) {
  229. sdata = vif_to_sdata(vif);
  230. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  231. if (sdata->deflink.operating_11g_mode)
  232. erp = rate->flags & IEEE80211_RATE_ERP_G;
  233. shift = ieee80211_vif_get_shift(vif);
  234. }
  235. bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
  236. /* Data frame duration */
  237. dur = ieee80211_frame_duration(sband->band, frame_len, bitrate,
  238. erp, short_preamble, shift);
  239. if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
  240. /* ACK duration */
  241. dur += ieee80211_frame_duration(sband->band, 10, bitrate,
  242. erp, short_preamble, shift);
  243. }
  244. return cpu_to_le16(dur);
  245. }
  246. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  247. static void __ieee80211_wake_txqs(struct ieee80211_sub_if_data *sdata, int ac)
  248. {
  249. struct ieee80211_local *local = sdata->local;
  250. struct ieee80211_vif *vif = &sdata->vif;
  251. struct fq *fq = &local->fq;
  252. struct ps_data *ps = NULL;
  253. struct txq_info *txqi;
  254. struct sta_info *sta;
  255. int i;
  256. local_bh_disable();
  257. spin_lock(&fq->lock);
  258. if (!test_bit(SDATA_STATE_RUNNING, &sdata->state))
  259. goto out;
  260. if (sdata->vif.type == NL80211_IFTYPE_AP)
  261. ps = &sdata->bss->ps;
  262. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  263. if (sdata != sta->sdata)
  264. continue;
  265. for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
  266. struct ieee80211_txq *txq = sta->sta.txq[i];
  267. if (!txq)
  268. continue;
  269. txqi = to_txq_info(txq);
  270. if (ac != txq->ac)
  271. continue;
  272. if (!test_and_clear_bit(IEEE80211_TXQ_DIRTY,
  273. &txqi->flags))
  274. continue;
  275. spin_unlock(&fq->lock);
  276. drv_wake_tx_queue(local, txqi);
  277. spin_lock(&fq->lock);
  278. }
  279. }
  280. if (!vif->txq)
  281. goto out;
  282. txqi = to_txq_info(vif->txq);
  283. if (!test_and_clear_bit(IEEE80211_TXQ_DIRTY, &txqi->flags) ||
  284. (ps && atomic_read(&ps->num_sta_ps)) || ac != vif->txq->ac)
  285. goto out;
  286. spin_unlock(&fq->lock);
  287. drv_wake_tx_queue(local, txqi);
  288. local_bh_enable();
  289. return;
  290. out:
  291. spin_unlock(&fq->lock);
  292. local_bh_enable();
  293. }
  294. static void
  295. __releases(&local->queue_stop_reason_lock)
  296. __acquires(&local->queue_stop_reason_lock)
  297. _ieee80211_wake_txqs(struct ieee80211_local *local, unsigned long *flags)
  298. {
  299. struct ieee80211_sub_if_data *sdata;
  300. int n_acs = IEEE80211_NUM_ACS;
  301. int i;
  302. rcu_read_lock();
  303. if (local->hw.queues < IEEE80211_NUM_ACS)
  304. n_acs = 1;
  305. for (i = 0; i < local->hw.queues; i++) {
  306. if (local->queue_stop_reasons[i])
  307. continue;
  308. spin_unlock_irqrestore(&local->queue_stop_reason_lock, *flags);
  309. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  310. int ac;
  311. for (ac = 0; ac < n_acs; ac++) {
  312. int ac_queue = sdata->vif.hw_queue[ac];
  313. if (ac_queue == i ||
  314. sdata->vif.cab_queue == i)
  315. __ieee80211_wake_txqs(sdata, ac);
  316. }
  317. }
  318. spin_lock_irqsave(&local->queue_stop_reason_lock, *flags);
  319. }
  320. rcu_read_unlock();
  321. }
  322. void ieee80211_wake_txqs(struct tasklet_struct *t)
  323. {
  324. struct ieee80211_local *local = from_tasklet(local, t,
  325. wake_txqs_tasklet);
  326. unsigned long flags;
  327. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  328. _ieee80211_wake_txqs(local, &flags);
  329. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  330. }
  331. void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
  332. {
  333. struct ieee80211_sub_if_data *sdata;
  334. int n_acs = IEEE80211_NUM_ACS;
  335. if (local->ops->wake_tx_queue)
  336. return;
  337. if (local->hw.queues < IEEE80211_NUM_ACS)
  338. n_acs = 1;
  339. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  340. int ac;
  341. if (!sdata->dev)
  342. continue;
  343. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
  344. local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
  345. continue;
  346. for (ac = 0; ac < n_acs; ac++) {
  347. int ac_queue = sdata->vif.hw_queue[ac];
  348. if (ac_queue == queue ||
  349. (sdata->vif.cab_queue == queue &&
  350. local->queue_stop_reasons[ac_queue] == 0 &&
  351. skb_queue_empty(&local->pending[ac_queue])))
  352. netif_wake_subqueue(sdata->dev, ac);
  353. }
  354. }
  355. }
  356. static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
  357. enum queue_stop_reason reason,
  358. bool refcounted,
  359. unsigned long *flags)
  360. {
  361. struct ieee80211_local *local = hw_to_local(hw);
  362. trace_wake_queue(local, queue, reason);
  363. if (WARN_ON(queue >= hw->queues))
  364. return;
  365. if (!test_bit(reason, &local->queue_stop_reasons[queue]))
  366. return;
  367. if (!refcounted) {
  368. local->q_stop_reasons[queue][reason] = 0;
  369. } else {
  370. local->q_stop_reasons[queue][reason]--;
  371. if (WARN_ON(local->q_stop_reasons[queue][reason] < 0))
  372. local->q_stop_reasons[queue][reason] = 0;
  373. }
  374. if (local->q_stop_reasons[queue][reason] == 0)
  375. __clear_bit(reason, &local->queue_stop_reasons[queue]);
  376. if (local->queue_stop_reasons[queue] != 0)
  377. /* someone still has this queue stopped */
  378. return;
  379. if (skb_queue_empty(&local->pending[queue])) {
  380. rcu_read_lock();
  381. ieee80211_propagate_queue_wake(local, queue);
  382. rcu_read_unlock();
  383. } else
  384. tasklet_schedule(&local->tx_pending_tasklet);
  385. /*
  386. * Calling _ieee80211_wake_txqs here can be a problem because it may
  387. * release queue_stop_reason_lock which has been taken by
  388. * __ieee80211_wake_queue's caller. It is certainly not very nice to
  389. * release someone's lock, but it is fine because all the callers of
  390. * __ieee80211_wake_queue call it right before releasing the lock.
  391. */
  392. if (local->ops->wake_tx_queue) {
  393. if (reason == IEEE80211_QUEUE_STOP_REASON_DRIVER)
  394. tasklet_schedule(&local->wake_txqs_tasklet);
  395. else
  396. _ieee80211_wake_txqs(local, flags);
  397. }
  398. }
  399. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  400. enum queue_stop_reason reason,
  401. bool refcounted)
  402. {
  403. struct ieee80211_local *local = hw_to_local(hw);
  404. unsigned long flags;
  405. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  406. __ieee80211_wake_queue(hw, queue, reason, refcounted, &flags);
  407. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  408. }
  409. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  410. {
  411. ieee80211_wake_queue_by_reason(hw, queue,
  412. IEEE80211_QUEUE_STOP_REASON_DRIVER,
  413. false);
  414. }
  415. EXPORT_SYMBOL(ieee80211_wake_queue);
  416. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  417. enum queue_stop_reason reason,
  418. bool refcounted)
  419. {
  420. struct ieee80211_local *local = hw_to_local(hw);
  421. struct ieee80211_sub_if_data *sdata;
  422. int n_acs = IEEE80211_NUM_ACS;
  423. trace_stop_queue(local, queue, reason);
  424. if (WARN_ON(queue >= hw->queues))
  425. return;
  426. if (!refcounted)
  427. local->q_stop_reasons[queue][reason] = 1;
  428. else
  429. local->q_stop_reasons[queue][reason]++;
  430. if (__test_and_set_bit(reason, &local->queue_stop_reasons[queue]))
  431. return;
  432. if (local->hw.queues < IEEE80211_NUM_ACS)
  433. n_acs = 1;
  434. rcu_read_lock();
  435. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  436. int ac;
  437. if (!sdata->dev)
  438. continue;
  439. for (ac = 0; ac < n_acs; ac++) {
  440. if (!local->ops->wake_tx_queue &&
  441. (sdata->vif.hw_queue[ac] == queue ||
  442. sdata->vif.cab_queue == queue))
  443. netif_stop_subqueue(sdata->dev, ac);
  444. }
  445. }
  446. rcu_read_unlock();
  447. }
  448. void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
  449. enum queue_stop_reason reason,
  450. bool refcounted)
  451. {
  452. struct ieee80211_local *local = hw_to_local(hw);
  453. unsigned long flags;
  454. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  455. __ieee80211_stop_queue(hw, queue, reason, refcounted);
  456. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  457. }
  458. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  459. {
  460. ieee80211_stop_queue_by_reason(hw, queue,
  461. IEEE80211_QUEUE_STOP_REASON_DRIVER,
  462. false);
  463. }
  464. EXPORT_SYMBOL(ieee80211_stop_queue);
  465. void ieee80211_add_pending_skb(struct ieee80211_local *local,
  466. struct sk_buff *skb)
  467. {
  468. struct ieee80211_hw *hw = &local->hw;
  469. unsigned long flags;
  470. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  471. int queue = info->hw_queue;
  472. if (WARN_ON(!info->control.vif)) {
  473. ieee80211_free_txskb(&local->hw, skb);
  474. return;
  475. }
  476. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  477. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
  478. false);
  479. __skb_queue_tail(&local->pending[queue], skb);
  480. __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
  481. false, &flags);
  482. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  483. }
  484. void ieee80211_add_pending_skbs(struct ieee80211_local *local,
  485. struct sk_buff_head *skbs)
  486. {
  487. struct ieee80211_hw *hw = &local->hw;
  488. struct sk_buff *skb;
  489. unsigned long flags;
  490. int queue, i;
  491. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  492. while ((skb = skb_dequeue(skbs))) {
  493. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  494. if (WARN_ON(!info->control.vif)) {
  495. ieee80211_free_txskb(&local->hw, skb);
  496. continue;
  497. }
  498. queue = info->hw_queue;
  499. __ieee80211_stop_queue(hw, queue,
  500. IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
  501. false);
  502. __skb_queue_tail(&local->pending[queue], skb);
  503. }
  504. for (i = 0; i < hw->queues; i++)
  505. __ieee80211_wake_queue(hw, i,
  506. IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
  507. false, &flags);
  508. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  509. }
  510. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  511. unsigned long queues,
  512. enum queue_stop_reason reason,
  513. bool refcounted)
  514. {
  515. struct ieee80211_local *local = hw_to_local(hw);
  516. unsigned long flags;
  517. int i;
  518. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  519. for_each_set_bit(i, &queues, hw->queues)
  520. __ieee80211_stop_queue(hw, i, reason, refcounted);
  521. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  522. }
  523. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  524. {
  525. ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  526. IEEE80211_QUEUE_STOP_REASON_DRIVER,
  527. false);
  528. }
  529. EXPORT_SYMBOL(ieee80211_stop_queues);
  530. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  531. {
  532. struct ieee80211_local *local = hw_to_local(hw);
  533. unsigned long flags;
  534. int ret;
  535. if (WARN_ON(queue >= hw->queues))
  536. return true;
  537. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  538. ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
  539. &local->queue_stop_reasons[queue]);
  540. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  541. return ret;
  542. }
  543. EXPORT_SYMBOL(ieee80211_queue_stopped);
  544. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  545. unsigned long queues,
  546. enum queue_stop_reason reason,
  547. bool refcounted)
  548. {
  549. struct ieee80211_local *local = hw_to_local(hw);
  550. unsigned long flags;
  551. int i;
  552. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  553. for_each_set_bit(i, &queues, hw->queues)
  554. __ieee80211_wake_queue(hw, i, reason, refcounted, &flags);
  555. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  556. }
  557. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  558. {
  559. ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  560. IEEE80211_QUEUE_STOP_REASON_DRIVER,
  561. false);
  562. }
  563. EXPORT_SYMBOL(ieee80211_wake_queues);
  564. static unsigned int
  565. ieee80211_get_vif_queues(struct ieee80211_local *local,
  566. struct ieee80211_sub_if_data *sdata)
  567. {
  568. unsigned int queues;
  569. if (sdata && ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
  570. int ac;
  571. queues = 0;
  572. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  573. queues |= BIT(sdata->vif.hw_queue[ac]);
  574. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
  575. queues |= BIT(sdata->vif.cab_queue);
  576. } else {
  577. /* all queues */
  578. queues = BIT(local->hw.queues) - 1;
  579. }
  580. return queues;
  581. }
  582. void __ieee80211_flush_queues(struct ieee80211_local *local,
  583. struct ieee80211_sub_if_data *sdata,
  584. unsigned int queues, bool drop)
  585. {
  586. if (!local->ops->flush)
  587. return;
  588. /*
  589. * If no queue was set, or if the HW doesn't support
  590. * IEEE80211_HW_QUEUE_CONTROL - flush all queues
  591. */
  592. if (!queues || !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
  593. queues = ieee80211_get_vif_queues(local, sdata);
  594. ieee80211_stop_queues_by_reason(&local->hw, queues,
  595. IEEE80211_QUEUE_STOP_REASON_FLUSH,
  596. false);
  597. drv_flush(local, sdata, queues, drop);
  598. ieee80211_wake_queues_by_reason(&local->hw, queues,
  599. IEEE80211_QUEUE_STOP_REASON_FLUSH,
  600. false);
  601. }
  602. void ieee80211_flush_queues(struct ieee80211_local *local,
  603. struct ieee80211_sub_if_data *sdata, bool drop)
  604. {
  605. __ieee80211_flush_queues(local, sdata, 0, drop);
  606. }
  607. void ieee80211_stop_vif_queues(struct ieee80211_local *local,
  608. struct ieee80211_sub_if_data *sdata,
  609. enum queue_stop_reason reason)
  610. {
  611. ieee80211_stop_queues_by_reason(&local->hw,
  612. ieee80211_get_vif_queues(local, sdata),
  613. reason, true);
  614. }
  615. void ieee80211_wake_vif_queues(struct ieee80211_local *local,
  616. struct ieee80211_sub_if_data *sdata,
  617. enum queue_stop_reason reason)
  618. {
  619. ieee80211_wake_queues_by_reason(&local->hw,
  620. ieee80211_get_vif_queues(local, sdata),
  621. reason, true);
  622. }
  623. static void __iterate_interfaces(struct ieee80211_local *local,
  624. u32 iter_flags,
  625. void (*iterator)(void *data, u8 *mac,
  626. struct ieee80211_vif *vif),
  627. void *data)
  628. {
  629. struct ieee80211_sub_if_data *sdata;
  630. bool active_only = iter_flags & IEEE80211_IFACE_ITER_ACTIVE;
  631. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  632. switch (sdata->vif.type) {
  633. case NL80211_IFTYPE_MONITOR:
  634. if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
  635. continue;
  636. break;
  637. case NL80211_IFTYPE_AP_VLAN:
  638. continue;
  639. default:
  640. break;
  641. }
  642. if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
  643. active_only && !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  644. continue;
  645. if ((iter_flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) &&
  646. !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  647. continue;
  648. if (ieee80211_sdata_running(sdata) || !active_only)
  649. iterator(data, sdata->vif.addr,
  650. &sdata->vif);
  651. }
  652. sdata = rcu_dereference_check(local->monitor_sdata,
  653. lockdep_is_held(&local->iflist_mtx) ||
  654. lockdep_is_held(&local->hw.wiphy->mtx));
  655. if (sdata &&
  656. (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL || !active_only ||
  657. sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  658. iterator(data, sdata->vif.addr, &sdata->vif);
  659. }
  660. void ieee80211_iterate_interfaces(
  661. struct ieee80211_hw *hw, u32 iter_flags,
  662. void (*iterator)(void *data, u8 *mac,
  663. struct ieee80211_vif *vif),
  664. void *data)
  665. {
  666. struct ieee80211_local *local = hw_to_local(hw);
  667. mutex_lock(&local->iflist_mtx);
  668. __iterate_interfaces(local, iter_flags, iterator, data);
  669. mutex_unlock(&local->iflist_mtx);
  670. }
  671. EXPORT_SYMBOL_GPL(ieee80211_iterate_interfaces);
  672. void ieee80211_iterate_active_interfaces_atomic(
  673. struct ieee80211_hw *hw, u32 iter_flags,
  674. void (*iterator)(void *data, u8 *mac,
  675. struct ieee80211_vif *vif),
  676. void *data)
  677. {
  678. struct ieee80211_local *local = hw_to_local(hw);
  679. rcu_read_lock();
  680. __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
  681. iterator, data);
  682. rcu_read_unlock();
  683. }
  684. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  685. void ieee80211_iterate_active_interfaces_mtx(
  686. struct ieee80211_hw *hw, u32 iter_flags,
  687. void (*iterator)(void *data, u8 *mac,
  688. struct ieee80211_vif *vif),
  689. void *data)
  690. {
  691. struct ieee80211_local *local = hw_to_local(hw);
  692. lockdep_assert_wiphy(hw->wiphy);
  693. __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
  694. iterator, data);
  695. }
  696. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_mtx);
  697. static void __iterate_stations(struct ieee80211_local *local,
  698. void (*iterator)(void *data,
  699. struct ieee80211_sta *sta),
  700. void *data)
  701. {
  702. struct sta_info *sta;
  703. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  704. if (!sta->uploaded)
  705. continue;
  706. iterator(data, &sta->sta);
  707. }
  708. }
  709. void ieee80211_iterate_stations(struct ieee80211_hw *hw,
  710. void (*iterator)(void *data,
  711. struct ieee80211_sta *sta),
  712. void *data)
  713. {
  714. struct ieee80211_local *local = hw_to_local(hw);
  715. mutex_lock(&local->sta_mtx);
  716. __iterate_stations(local, iterator, data);
  717. mutex_unlock(&local->sta_mtx);
  718. }
  719. EXPORT_SYMBOL_GPL(ieee80211_iterate_stations);
  720. void ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
  721. void (*iterator)(void *data,
  722. struct ieee80211_sta *sta),
  723. void *data)
  724. {
  725. struct ieee80211_local *local = hw_to_local(hw);
  726. rcu_read_lock();
  727. __iterate_stations(local, iterator, data);
  728. rcu_read_unlock();
  729. }
  730. EXPORT_SYMBOL_GPL(ieee80211_iterate_stations_atomic);
  731. struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev)
  732. {
  733. struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
  734. if (!ieee80211_sdata_running(sdata) ||
  735. !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  736. return NULL;
  737. return &sdata->vif;
  738. }
  739. EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif);
  740. struct wireless_dev *ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
  741. {
  742. if (!vif)
  743. return NULL;
  744. return &vif_to_sdata(vif)->wdev;
  745. }
  746. EXPORT_SYMBOL_GPL(ieee80211_vif_to_wdev);
  747. /*
  748. * Nothing should have been stuffed into the workqueue during
  749. * the suspend->resume cycle. Since we can't check each caller
  750. * of this function if we are already quiescing / suspended,
  751. * check here and don't WARN since this can actually happen when
  752. * the rx path (for example) is racing against __ieee80211_suspend
  753. * and suspending / quiescing was set after the rx path checked
  754. * them.
  755. */
  756. static bool ieee80211_can_queue_work(struct ieee80211_local *local)
  757. {
  758. if (local->quiescing || (local->suspended && !local->resuming)) {
  759. pr_warn("queueing ieee80211 work while going to suspend\n");
  760. return false;
  761. }
  762. return true;
  763. }
  764. void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
  765. {
  766. struct ieee80211_local *local = hw_to_local(hw);
  767. if (!ieee80211_can_queue_work(local))
  768. return;
  769. queue_work(local->workqueue, work);
  770. }
  771. EXPORT_SYMBOL(ieee80211_queue_work);
  772. void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
  773. struct delayed_work *dwork,
  774. unsigned long delay)
  775. {
  776. struct ieee80211_local *local = hw_to_local(hw);
  777. if (!ieee80211_can_queue_work(local))
  778. return;
  779. queue_delayed_work(local->workqueue, dwork, delay);
  780. }
  781. EXPORT_SYMBOL(ieee80211_queue_delayed_work);
  782. static void
  783. ieee80211_parse_extension_element(u32 *crc,
  784. const struct element *elem,
  785. struct ieee802_11_elems *elems,
  786. struct ieee80211_elems_parse_params *params)
  787. {
  788. const void *data = elem->data + 1;
  789. u8 len;
  790. if (!elem->datalen)
  791. return;
  792. len = elem->datalen - 1;
  793. switch (elem->data[0]) {
  794. case WLAN_EID_EXT_HE_MU_EDCA:
  795. if (len >= sizeof(*elems->mu_edca_param_set)) {
  796. elems->mu_edca_param_set = data;
  797. if (crc)
  798. *crc = crc32_be(*crc, (void *)elem,
  799. elem->datalen + 2);
  800. }
  801. break;
  802. case WLAN_EID_EXT_HE_CAPABILITY:
  803. if (ieee80211_he_capa_size_ok(data, len)) {
  804. elems->he_cap = data;
  805. elems->he_cap_len = len;
  806. }
  807. break;
  808. case WLAN_EID_EXT_HE_OPERATION:
  809. if (len >= sizeof(*elems->he_operation) &&
  810. len >= ieee80211_he_oper_size(data) - 1) {
  811. if (crc)
  812. *crc = crc32_be(*crc, (void *)elem,
  813. elem->datalen + 2);
  814. elems->he_operation = data;
  815. }
  816. break;
  817. case WLAN_EID_EXT_UORA:
  818. if (len >= 1)
  819. elems->uora_element = data;
  820. break;
  821. case WLAN_EID_EXT_MAX_CHANNEL_SWITCH_TIME:
  822. if (len == 3)
  823. elems->max_channel_switch_time = data;
  824. break;
  825. case WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION:
  826. if (len >= sizeof(*elems->mbssid_config_ie))
  827. elems->mbssid_config_ie = data;
  828. break;
  829. case WLAN_EID_EXT_HE_SPR:
  830. if (len >= sizeof(*elems->he_spr) &&
  831. len >= ieee80211_he_spr_size(data))
  832. elems->he_spr = data;
  833. break;
  834. case WLAN_EID_EXT_HE_6GHZ_CAPA:
  835. if (len >= sizeof(*elems->he_6ghz_capa))
  836. elems->he_6ghz_capa = data;
  837. break;
  838. case WLAN_EID_EXT_EHT_CAPABILITY:
  839. if (ieee80211_eht_capa_size_ok(elems->he_cap,
  840. data, len,
  841. params->from_ap)) {
  842. elems->eht_cap = data;
  843. elems->eht_cap_len = len;
  844. }
  845. break;
  846. case WLAN_EID_EXT_EHT_OPERATION:
  847. if (ieee80211_eht_oper_size_ok(data, len))
  848. elems->eht_operation = data;
  849. break;
  850. case WLAN_EID_EXT_EHT_MULTI_LINK:
  851. if (ieee80211_mle_size_ok(data, len))
  852. elems->multi_link = (void *)data;
  853. break;
  854. }
  855. }
  856. static u32
  857. _ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params,
  858. struct ieee802_11_elems *elems,
  859. const struct element *check_inherit)
  860. {
  861. const struct element *elem;
  862. bool calc_crc = params->filter != 0;
  863. DECLARE_BITMAP(seen_elems, 256);
  864. u32 crc = params->crc;
  865. const u8 *ie;
  866. bitmap_zero(seen_elems, 256);
  867. for_each_element(elem, params->start, params->len) {
  868. bool elem_parse_failed;
  869. u8 id = elem->id;
  870. u8 elen = elem->datalen;
  871. const u8 *pos = elem->data;
  872. if (check_inherit &&
  873. !cfg80211_is_element_inherited(elem,
  874. check_inherit))
  875. continue;
  876. switch (id) {
  877. case WLAN_EID_SSID:
  878. case WLAN_EID_SUPP_RATES:
  879. case WLAN_EID_FH_PARAMS:
  880. case WLAN_EID_DS_PARAMS:
  881. case WLAN_EID_CF_PARAMS:
  882. case WLAN_EID_TIM:
  883. case WLAN_EID_IBSS_PARAMS:
  884. case WLAN_EID_CHALLENGE:
  885. case WLAN_EID_RSN:
  886. case WLAN_EID_ERP_INFO:
  887. case WLAN_EID_EXT_SUPP_RATES:
  888. case WLAN_EID_HT_CAPABILITY:
  889. case WLAN_EID_HT_OPERATION:
  890. case WLAN_EID_VHT_CAPABILITY:
  891. case WLAN_EID_VHT_OPERATION:
  892. case WLAN_EID_MESH_ID:
  893. case WLAN_EID_MESH_CONFIG:
  894. case WLAN_EID_PEER_MGMT:
  895. case WLAN_EID_PREQ:
  896. case WLAN_EID_PREP:
  897. case WLAN_EID_PERR:
  898. case WLAN_EID_RANN:
  899. case WLAN_EID_CHANNEL_SWITCH:
  900. case WLAN_EID_EXT_CHANSWITCH_ANN:
  901. case WLAN_EID_COUNTRY:
  902. case WLAN_EID_PWR_CONSTRAINT:
  903. case WLAN_EID_TIMEOUT_INTERVAL:
  904. case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
  905. case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
  906. case WLAN_EID_CHAN_SWITCH_PARAM:
  907. case WLAN_EID_EXT_CAPABILITY:
  908. case WLAN_EID_CHAN_SWITCH_TIMING:
  909. case WLAN_EID_LINK_ID:
  910. case WLAN_EID_BSS_MAX_IDLE_PERIOD:
  911. case WLAN_EID_RSNX:
  912. case WLAN_EID_S1G_BCN_COMPAT:
  913. case WLAN_EID_S1G_CAPABILITIES:
  914. case WLAN_EID_S1G_OPERATION:
  915. case WLAN_EID_AID_RESPONSE:
  916. case WLAN_EID_S1G_SHORT_BCN_INTERVAL:
  917. /*
  918. * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
  919. * that if the content gets bigger it might be needed more than once
  920. */
  921. if (test_bit(id, seen_elems)) {
  922. elems->parse_error = true;
  923. continue;
  924. }
  925. break;
  926. }
  927. if (calc_crc && id < 64 && (params->filter & (1ULL << id)))
  928. crc = crc32_be(crc, pos - 2, elen + 2);
  929. elem_parse_failed = false;
  930. switch (id) {
  931. case WLAN_EID_LINK_ID:
  932. if (elen + 2 < sizeof(struct ieee80211_tdls_lnkie)) {
  933. elem_parse_failed = true;
  934. break;
  935. }
  936. elems->lnk_id = (void *)(pos - 2);
  937. break;
  938. case WLAN_EID_CHAN_SWITCH_TIMING:
  939. if (elen < sizeof(struct ieee80211_ch_switch_timing)) {
  940. elem_parse_failed = true;
  941. break;
  942. }
  943. elems->ch_sw_timing = (void *)pos;
  944. break;
  945. case WLAN_EID_EXT_CAPABILITY:
  946. elems->ext_capab = pos;
  947. elems->ext_capab_len = elen;
  948. break;
  949. case WLAN_EID_SSID:
  950. elems->ssid = pos;
  951. elems->ssid_len = elen;
  952. break;
  953. case WLAN_EID_SUPP_RATES:
  954. elems->supp_rates = pos;
  955. elems->supp_rates_len = elen;
  956. break;
  957. case WLAN_EID_DS_PARAMS:
  958. if (elen >= 1)
  959. elems->ds_params = pos;
  960. else
  961. elem_parse_failed = true;
  962. break;
  963. case WLAN_EID_TIM:
  964. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  965. elems->tim = (void *)pos;
  966. elems->tim_len = elen;
  967. } else
  968. elem_parse_failed = true;
  969. break;
  970. case WLAN_EID_VENDOR_SPECIFIC:
  971. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  972. pos[2] == 0xf2) {
  973. /* Microsoft OUI (00:50:F2) */
  974. if (calc_crc)
  975. crc = crc32_be(crc, pos - 2, elen + 2);
  976. if (elen >= 5 && pos[3] == 2) {
  977. /* OUI Type 2 - WMM IE */
  978. if (pos[4] == 0) {
  979. elems->wmm_info = pos;
  980. elems->wmm_info_len = elen;
  981. } else if (pos[4] == 1) {
  982. elems->wmm_param = pos;
  983. elems->wmm_param_len = elen;
  984. }
  985. }
  986. }
  987. break;
  988. case WLAN_EID_RSN:
  989. elems->rsn = pos;
  990. elems->rsn_len = elen;
  991. break;
  992. case WLAN_EID_ERP_INFO:
  993. if (elen >= 1)
  994. elems->erp_info = pos;
  995. else
  996. elem_parse_failed = true;
  997. break;
  998. case WLAN_EID_EXT_SUPP_RATES:
  999. elems->ext_supp_rates = pos;
  1000. elems->ext_supp_rates_len = elen;
  1001. break;
  1002. case WLAN_EID_HT_CAPABILITY:
  1003. if (elen >= sizeof(struct ieee80211_ht_cap))
  1004. elems->ht_cap_elem = (void *)pos;
  1005. else
  1006. elem_parse_failed = true;
  1007. break;
  1008. case WLAN_EID_HT_OPERATION:
  1009. if (elen >= sizeof(struct ieee80211_ht_operation))
  1010. elems->ht_operation = (void *)pos;
  1011. else
  1012. elem_parse_failed = true;
  1013. break;
  1014. case WLAN_EID_VHT_CAPABILITY:
  1015. if (elen >= sizeof(struct ieee80211_vht_cap))
  1016. elems->vht_cap_elem = (void *)pos;
  1017. else
  1018. elem_parse_failed = true;
  1019. break;
  1020. case WLAN_EID_VHT_OPERATION:
  1021. if (elen >= sizeof(struct ieee80211_vht_operation)) {
  1022. elems->vht_operation = (void *)pos;
  1023. if (calc_crc)
  1024. crc = crc32_be(crc, pos - 2, elen + 2);
  1025. break;
  1026. }
  1027. elem_parse_failed = true;
  1028. break;
  1029. case WLAN_EID_OPMODE_NOTIF:
  1030. if (elen > 0) {
  1031. elems->opmode_notif = pos;
  1032. if (calc_crc)
  1033. crc = crc32_be(crc, pos - 2, elen + 2);
  1034. break;
  1035. }
  1036. elem_parse_failed = true;
  1037. break;
  1038. case WLAN_EID_MESH_ID:
  1039. elems->mesh_id = pos;
  1040. elems->mesh_id_len = elen;
  1041. break;
  1042. case WLAN_EID_MESH_CONFIG:
  1043. if (elen >= sizeof(struct ieee80211_meshconf_ie))
  1044. elems->mesh_config = (void *)pos;
  1045. else
  1046. elem_parse_failed = true;
  1047. break;
  1048. case WLAN_EID_PEER_MGMT:
  1049. elems->peering = pos;
  1050. elems->peering_len = elen;
  1051. break;
  1052. case WLAN_EID_MESH_AWAKE_WINDOW:
  1053. if (elen >= 2)
  1054. elems->awake_window = (void *)pos;
  1055. break;
  1056. case WLAN_EID_PREQ:
  1057. elems->preq = pos;
  1058. elems->preq_len = elen;
  1059. break;
  1060. case WLAN_EID_PREP:
  1061. elems->prep = pos;
  1062. elems->prep_len = elen;
  1063. break;
  1064. case WLAN_EID_PERR:
  1065. elems->perr = pos;
  1066. elems->perr_len = elen;
  1067. break;
  1068. case WLAN_EID_RANN:
  1069. if (elen >= sizeof(struct ieee80211_rann_ie))
  1070. elems->rann = (void *)pos;
  1071. else
  1072. elem_parse_failed = true;
  1073. break;
  1074. case WLAN_EID_CHANNEL_SWITCH:
  1075. if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
  1076. elem_parse_failed = true;
  1077. break;
  1078. }
  1079. elems->ch_switch_ie = (void *)pos;
  1080. break;
  1081. case WLAN_EID_EXT_CHANSWITCH_ANN:
  1082. if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
  1083. elem_parse_failed = true;
  1084. break;
  1085. }
  1086. elems->ext_chansw_ie = (void *)pos;
  1087. break;
  1088. case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
  1089. if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
  1090. elem_parse_failed = true;
  1091. break;
  1092. }
  1093. elems->sec_chan_offs = (void *)pos;
  1094. break;
  1095. case WLAN_EID_CHAN_SWITCH_PARAM:
  1096. if (elen <
  1097. sizeof(*elems->mesh_chansw_params_ie)) {
  1098. elem_parse_failed = true;
  1099. break;
  1100. }
  1101. elems->mesh_chansw_params_ie = (void *)pos;
  1102. break;
  1103. case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
  1104. if (!params->action ||
  1105. elen < sizeof(*elems->wide_bw_chansw_ie)) {
  1106. elem_parse_failed = true;
  1107. break;
  1108. }
  1109. elems->wide_bw_chansw_ie = (void *)pos;
  1110. break;
  1111. case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
  1112. if (params->action) {
  1113. elem_parse_failed = true;
  1114. break;
  1115. }
  1116. /*
  1117. * This is a bit tricky, but as we only care about
  1118. * the wide bandwidth channel switch element, so
  1119. * just parse it out manually.
  1120. */
  1121. ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
  1122. pos, elen);
  1123. if (ie) {
  1124. if (ie[1] >= sizeof(*elems->wide_bw_chansw_ie))
  1125. elems->wide_bw_chansw_ie =
  1126. (void *)(ie + 2);
  1127. else
  1128. elem_parse_failed = true;
  1129. }
  1130. break;
  1131. case WLAN_EID_COUNTRY:
  1132. elems->country_elem = pos;
  1133. elems->country_elem_len = elen;
  1134. break;
  1135. case WLAN_EID_PWR_CONSTRAINT:
  1136. if (elen != 1) {
  1137. elem_parse_failed = true;
  1138. break;
  1139. }
  1140. elems->pwr_constr_elem = pos;
  1141. break;
  1142. case WLAN_EID_CISCO_VENDOR_SPECIFIC:
  1143. /* Lots of different options exist, but we only care
  1144. * about the Dynamic Transmit Power Control element.
  1145. * First check for the Cisco OUI, then for the DTPC
  1146. * tag (0x00).
  1147. */
  1148. if (elen < 4) {
  1149. elem_parse_failed = true;
  1150. break;
  1151. }
  1152. if (pos[0] != 0x00 || pos[1] != 0x40 ||
  1153. pos[2] != 0x96 || pos[3] != 0x00)
  1154. break;
  1155. if (elen != 6) {
  1156. elem_parse_failed = true;
  1157. break;
  1158. }
  1159. if (calc_crc)
  1160. crc = crc32_be(crc, pos - 2, elen + 2);
  1161. elems->cisco_dtpc_elem = pos;
  1162. break;
  1163. case WLAN_EID_ADDBA_EXT:
  1164. if (elen < sizeof(struct ieee80211_addba_ext_ie)) {
  1165. elem_parse_failed = true;
  1166. break;
  1167. }
  1168. elems->addba_ext_ie = (void *)pos;
  1169. break;
  1170. case WLAN_EID_TIMEOUT_INTERVAL:
  1171. if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
  1172. elems->timeout_int = (void *)pos;
  1173. else
  1174. elem_parse_failed = true;
  1175. break;
  1176. case WLAN_EID_BSS_MAX_IDLE_PERIOD:
  1177. if (elen >= sizeof(*elems->max_idle_period_ie))
  1178. elems->max_idle_period_ie = (void *)pos;
  1179. break;
  1180. case WLAN_EID_RSNX:
  1181. elems->rsnx = pos;
  1182. elems->rsnx_len = elen;
  1183. break;
  1184. case WLAN_EID_TX_POWER_ENVELOPE:
  1185. if (elen < 1 ||
  1186. elen > sizeof(struct ieee80211_tx_pwr_env))
  1187. break;
  1188. if (elems->tx_pwr_env_num >= ARRAY_SIZE(elems->tx_pwr_env))
  1189. break;
  1190. elems->tx_pwr_env[elems->tx_pwr_env_num] = (void *)pos;
  1191. elems->tx_pwr_env_len[elems->tx_pwr_env_num] = elen;
  1192. elems->tx_pwr_env_num++;
  1193. break;
  1194. case WLAN_EID_EXTENSION:
  1195. ieee80211_parse_extension_element(calc_crc ?
  1196. &crc : NULL,
  1197. elem, elems, params);
  1198. break;
  1199. case WLAN_EID_S1G_CAPABILITIES:
  1200. if (elen >= sizeof(*elems->s1g_capab))
  1201. elems->s1g_capab = (void *)pos;
  1202. else
  1203. elem_parse_failed = true;
  1204. break;
  1205. case WLAN_EID_S1G_OPERATION:
  1206. if (elen == sizeof(*elems->s1g_oper))
  1207. elems->s1g_oper = (void *)pos;
  1208. else
  1209. elem_parse_failed = true;
  1210. break;
  1211. case WLAN_EID_S1G_BCN_COMPAT:
  1212. if (elen == sizeof(*elems->s1g_bcn_compat))
  1213. elems->s1g_bcn_compat = (void *)pos;
  1214. else
  1215. elem_parse_failed = true;
  1216. break;
  1217. case WLAN_EID_AID_RESPONSE:
  1218. if (elen == sizeof(struct ieee80211_aid_response_ie))
  1219. elems->aid_resp = (void *)pos;
  1220. else
  1221. elem_parse_failed = true;
  1222. break;
  1223. default:
  1224. break;
  1225. }
  1226. if (elem_parse_failed)
  1227. elems->parse_error = true;
  1228. else
  1229. __set_bit(id, seen_elems);
  1230. }
  1231. if (!for_each_element_completed(elem, params->start, params->len))
  1232. elems->parse_error = true;
  1233. return crc;
  1234. }
  1235. static size_t ieee802_11_find_bssid_profile(const u8 *start, size_t len,
  1236. struct ieee802_11_elems *elems,
  1237. struct cfg80211_bss *bss,
  1238. u8 *nontransmitted_profile)
  1239. {
  1240. const struct element *elem, *sub;
  1241. size_t profile_len = 0;
  1242. bool found = false;
  1243. if (!bss || !bss->transmitted_bss)
  1244. return profile_len;
  1245. for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, len) {
  1246. if (elem->datalen < 2)
  1247. continue;
  1248. if (elem->data[0] < 1 || elem->data[0] > 8)
  1249. continue;
  1250. for_each_element(sub, elem->data + 1, elem->datalen - 1) {
  1251. u8 new_bssid[ETH_ALEN];
  1252. const u8 *index;
  1253. if (sub->id != 0 || sub->datalen < 4) {
  1254. /* not a valid BSS profile */
  1255. continue;
  1256. }
  1257. if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
  1258. sub->data[1] != 2) {
  1259. /* The first element of the
  1260. * Nontransmitted BSSID Profile is not
  1261. * the Nontransmitted BSSID Capability
  1262. * element.
  1263. */
  1264. continue;
  1265. }
  1266. memset(nontransmitted_profile, 0, len);
  1267. profile_len = cfg80211_merge_profile(start, len,
  1268. elem,
  1269. sub,
  1270. nontransmitted_profile,
  1271. len);
  1272. /* found a Nontransmitted BSSID Profile */
  1273. index = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX,
  1274. nontransmitted_profile,
  1275. profile_len);
  1276. if (!index || index[1] < 1 || index[2] == 0) {
  1277. /* Invalid MBSSID Index element */
  1278. continue;
  1279. }
  1280. cfg80211_gen_new_bssid(bss->transmitted_bss->bssid,
  1281. elem->data[0],
  1282. index[2],
  1283. new_bssid);
  1284. if (ether_addr_equal(new_bssid, bss->bssid)) {
  1285. found = true;
  1286. elems->bssid_index_len = index[1];
  1287. elems->bssid_index = (void *)&index[2];
  1288. break;
  1289. }
  1290. }
  1291. }
  1292. return found ? profile_len : 0;
  1293. }
  1294. struct ieee802_11_elems *
  1295. ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params)
  1296. {
  1297. struct ieee802_11_elems *elems;
  1298. const struct element *non_inherit = NULL;
  1299. u8 *nontransmitted_profile;
  1300. int nontransmitted_profile_len = 0;
  1301. size_t scratch_len = params->len;
  1302. elems = kzalloc(sizeof(*elems) + scratch_len, GFP_ATOMIC);
  1303. if (!elems)
  1304. return NULL;
  1305. elems->ie_start = params->start;
  1306. elems->total_len = params->len;
  1307. elems->scratch_len = scratch_len;
  1308. elems->scratch_pos = elems->scratch;
  1309. nontransmitted_profile = elems->scratch_pos;
  1310. nontransmitted_profile_len =
  1311. ieee802_11_find_bssid_profile(params->start, params->len,
  1312. elems, params->bss,
  1313. nontransmitted_profile);
  1314. elems->scratch_pos += nontransmitted_profile_len;
  1315. elems->scratch_len -= nontransmitted_profile_len;
  1316. non_inherit = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
  1317. nontransmitted_profile,
  1318. nontransmitted_profile_len);
  1319. elems->crc = _ieee802_11_parse_elems_full(params, elems, non_inherit);
  1320. /* Override with nontransmitted profile, if found */
  1321. if (nontransmitted_profile_len) {
  1322. struct ieee80211_elems_parse_params sub = {
  1323. .start = nontransmitted_profile,
  1324. .len = nontransmitted_profile_len,
  1325. .action = params->action,
  1326. .link_id = params->link_id,
  1327. };
  1328. _ieee802_11_parse_elems_full(&sub, elems, NULL);
  1329. }
  1330. if (elems->tim && !elems->parse_error) {
  1331. const struct ieee80211_tim_ie *tim_ie = elems->tim;
  1332. elems->dtim_period = tim_ie->dtim_period;
  1333. elems->dtim_count = tim_ie->dtim_count;
  1334. }
  1335. /* Override DTIM period and count if needed */
  1336. if (elems->bssid_index &&
  1337. elems->bssid_index_len >=
  1338. offsetofend(struct ieee80211_bssid_index, dtim_period))
  1339. elems->dtim_period = elems->bssid_index->dtim_period;
  1340. if (elems->bssid_index &&
  1341. elems->bssid_index_len >=
  1342. offsetofend(struct ieee80211_bssid_index, dtim_count))
  1343. elems->dtim_count = elems->bssid_index->dtim_count;
  1344. return elems;
  1345. }
  1346. void ieee80211_regulatory_limit_wmm_params(struct ieee80211_sub_if_data *sdata,
  1347. struct ieee80211_tx_queue_params
  1348. *qparam, int ac)
  1349. {
  1350. struct ieee80211_chanctx_conf *chanctx_conf;
  1351. const struct ieee80211_reg_rule *rrule;
  1352. const struct ieee80211_wmm_ac *wmm_ac;
  1353. u16 center_freq = 0;
  1354. if (sdata->vif.type != NL80211_IFTYPE_AP &&
  1355. sdata->vif.type != NL80211_IFTYPE_STATION)
  1356. return;
  1357. rcu_read_lock();
  1358. chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
  1359. if (chanctx_conf)
  1360. center_freq = chanctx_conf->def.chan->center_freq;
  1361. if (!center_freq) {
  1362. rcu_read_unlock();
  1363. return;
  1364. }
  1365. rrule = freq_reg_info(sdata->wdev.wiphy, MHZ_TO_KHZ(center_freq));
  1366. if (IS_ERR_OR_NULL(rrule) || !rrule->has_wmm) {
  1367. rcu_read_unlock();
  1368. return;
  1369. }
  1370. if (sdata->vif.type == NL80211_IFTYPE_AP)
  1371. wmm_ac = &rrule->wmm_rule.ap[ac];
  1372. else
  1373. wmm_ac = &rrule->wmm_rule.client[ac];
  1374. qparam->cw_min = max_t(u16, qparam->cw_min, wmm_ac->cw_min);
  1375. qparam->cw_max = max_t(u16, qparam->cw_max, wmm_ac->cw_max);
  1376. qparam->aifs = max_t(u8, qparam->aifs, wmm_ac->aifsn);
  1377. qparam->txop = min_t(u16, qparam->txop, wmm_ac->cot / 32);
  1378. rcu_read_unlock();
  1379. }
  1380. void ieee80211_set_wmm_default(struct ieee80211_link_data *link,
  1381. bool bss_notify, bool enable_qos)
  1382. {
  1383. struct ieee80211_sub_if_data *sdata = link->sdata;
  1384. struct ieee80211_local *local = sdata->local;
  1385. struct ieee80211_tx_queue_params qparam;
  1386. struct ieee80211_chanctx_conf *chanctx_conf;
  1387. int ac;
  1388. bool use_11b;
  1389. bool is_ocb; /* Use another EDCA parameters if dot11OCBActivated=true */
  1390. int aCWmin, aCWmax;
  1391. if (!local->ops->conf_tx)
  1392. return;
  1393. if (local->hw.queues < IEEE80211_NUM_ACS)
  1394. return;
  1395. memset(&qparam, 0, sizeof(qparam));
  1396. rcu_read_lock();
  1397. chanctx_conf = rcu_dereference(link->conf->chanctx_conf);
  1398. use_11b = (chanctx_conf &&
  1399. chanctx_conf->def.chan->band == NL80211_BAND_2GHZ) &&
  1400. !link->operating_11g_mode;
  1401. rcu_read_unlock();
  1402. is_ocb = (sdata->vif.type == NL80211_IFTYPE_OCB);
  1403. /* Set defaults according to 802.11-2007 Table 7-37 */
  1404. aCWmax = 1023;
  1405. if (use_11b)
  1406. aCWmin = 31;
  1407. else
  1408. aCWmin = 15;
  1409. /* Confiure old 802.11b/g medium access rules. */
  1410. qparam.cw_max = aCWmax;
  1411. qparam.cw_min = aCWmin;
  1412. qparam.txop = 0;
  1413. qparam.aifs = 2;
  1414. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  1415. /* Update if QoS is enabled. */
  1416. if (enable_qos) {
  1417. switch (ac) {
  1418. case IEEE80211_AC_BK:
  1419. qparam.cw_max = aCWmax;
  1420. qparam.cw_min = aCWmin;
  1421. qparam.txop = 0;
  1422. if (is_ocb)
  1423. qparam.aifs = 9;
  1424. else
  1425. qparam.aifs = 7;
  1426. break;
  1427. /* never happens but let's not leave undefined */
  1428. default:
  1429. case IEEE80211_AC_BE:
  1430. qparam.cw_max = aCWmax;
  1431. qparam.cw_min = aCWmin;
  1432. qparam.txop = 0;
  1433. if (is_ocb)
  1434. qparam.aifs = 6;
  1435. else
  1436. qparam.aifs = 3;
  1437. break;
  1438. case IEEE80211_AC_VI:
  1439. qparam.cw_max = aCWmin;
  1440. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  1441. if (is_ocb)
  1442. qparam.txop = 0;
  1443. else if (use_11b)
  1444. qparam.txop = 6016/32;
  1445. else
  1446. qparam.txop = 3008/32;
  1447. if (is_ocb)
  1448. qparam.aifs = 3;
  1449. else
  1450. qparam.aifs = 2;
  1451. break;
  1452. case IEEE80211_AC_VO:
  1453. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  1454. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  1455. if (is_ocb)
  1456. qparam.txop = 0;
  1457. else if (use_11b)
  1458. qparam.txop = 3264/32;
  1459. else
  1460. qparam.txop = 1504/32;
  1461. qparam.aifs = 2;
  1462. break;
  1463. }
  1464. }
  1465. ieee80211_regulatory_limit_wmm_params(sdata, &qparam, ac);
  1466. qparam.uapsd = false;
  1467. link->tx_conf[ac] = qparam;
  1468. drv_conf_tx(local, link, ac, &qparam);
  1469. }
  1470. if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1471. sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
  1472. sdata->vif.type != NL80211_IFTYPE_NAN) {
  1473. link->conf->qos = enable_qos;
  1474. if (bss_notify)
  1475. ieee80211_link_info_change_notify(sdata, link,
  1476. BSS_CHANGED_QOS);
  1477. }
  1478. }
  1479. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  1480. u16 transaction, u16 auth_alg, u16 status,
  1481. const u8 *extra, size_t extra_len, const u8 *da,
  1482. const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
  1483. u32 tx_flags)
  1484. {
  1485. struct ieee80211_local *local = sdata->local;
  1486. struct sk_buff *skb;
  1487. struct ieee80211_mgmt *mgmt;
  1488. bool multi_link = sdata->vif.valid_links;
  1489. struct {
  1490. u8 id;
  1491. u8 len;
  1492. u8 ext_id;
  1493. struct ieee80211_multi_link_elem ml;
  1494. struct ieee80211_mle_basic_common_info basic;
  1495. } __packed mle = {
  1496. .id = WLAN_EID_EXTENSION,
  1497. .len = sizeof(mle) - 2,
  1498. .ext_id = WLAN_EID_EXT_EHT_MULTI_LINK,
  1499. .ml.control = cpu_to_le16(IEEE80211_ML_CONTROL_TYPE_BASIC),
  1500. .basic.len = sizeof(mle.basic),
  1501. };
  1502. int err;
  1503. memcpy(mle.basic.mld_mac_addr, sdata->vif.addr, ETH_ALEN);
  1504. /* 24 + 6 = header + auth_algo + auth_transaction + status_code */
  1505. skb = dev_alloc_skb(local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN +
  1506. 24 + 6 + extra_len + IEEE80211_WEP_ICV_LEN +
  1507. multi_link * sizeof(mle));
  1508. if (!skb)
  1509. return;
  1510. skb_reserve(skb, local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN);
  1511. mgmt = skb_put_zero(skb, 24 + 6);
  1512. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1513. IEEE80211_STYPE_AUTH);
  1514. memcpy(mgmt->da, da, ETH_ALEN);
  1515. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  1516. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  1517. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  1518. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  1519. mgmt->u.auth.status_code = cpu_to_le16(status);
  1520. if (extra)
  1521. skb_put_data(skb, extra, extra_len);
  1522. if (multi_link)
  1523. skb_put_data(skb, &mle, sizeof(mle));
  1524. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  1525. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  1526. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  1527. if (WARN_ON(err)) {
  1528. kfree_skb(skb);
  1529. return;
  1530. }
  1531. }
  1532. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  1533. tx_flags;
  1534. ieee80211_tx_skb(sdata, skb);
  1535. }
  1536. void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  1537. const u8 *da, const u8 *bssid,
  1538. u16 stype, u16 reason,
  1539. bool send_frame, u8 *frame_buf)
  1540. {
  1541. struct ieee80211_local *local = sdata->local;
  1542. struct sk_buff *skb;
  1543. struct ieee80211_mgmt *mgmt = (void *)frame_buf;
  1544. /* build frame */
  1545. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  1546. mgmt->duration = 0; /* initialize only */
  1547. mgmt->seq_ctrl = 0; /* initialize only */
  1548. memcpy(mgmt->da, da, ETH_ALEN);
  1549. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  1550. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  1551. /* u.deauth.reason_code == u.disassoc.reason_code */
  1552. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  1553. if (send_frame) {
  1554. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  1555. IEEE80211_DEAUTH_FRAME_LEN);
  1556. if (!skb)
  1557. return;
  1558. skb_reserve(skb, local->hw.extra_tx_headroom);
  1559. /* copy in frame */
  1560. skb_put_data(skb, mgmt, IEEE80211_DEAUTH_FRAME_LEN);
  1561. if (sdata->vif.type != NL80211_IFTYPE_STATION ||
  1562. !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
  1563. IEEE80211_SKB_CB(skb)->flags |=
  1564. IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1565. ieee80211_tx_skb(sdata, skb);
  1566. }
  1567. }
  1568. static u8 *ieee80211_write_he_6ghz_cap(u8 *pos, __le16 cap, u8 *end)
  1569. {
  1570. if ((end - pos) < 5)
  1571. return pos;
  1572. *pos++ = WLAN_EID_EXTENSION;
  1573. *pos++ = 1 + sizeof(cap);
  1574. *pos++ = WLAN_EID_EXT_HE_6GHZ_CAPA;
  1575. memcpy(pos, &cap, sizeof(cap));
  1576. return pos + 2;
  1577. }
  1578. static int ieee80211_build_preq_ies_band(struct ieee80211_sub_if_data *sdata,
  1579. u8 *buffer, size_t buffer_len,
  1580. const u8 *ie, size_t ie_len,
  1581. enum nl80211_band band,
  1582. u32 rate_mask,
  1583. struct cfg80211_chan_def *chandef,
  1584. size_t *offset, u32 flags)
  1585. {
  1586. struct ieee80211_local *local = sdata->local;
  1587. struct ieee80211_supported_band *sband;
  1588. const struct ieee80211_sta_he_cap *he_cap;
  1589. const struct ieee80211_sta_eht_cap *eht_cap;
  1590. u8 *pos = buffer, *end = buffer + buffer_len;
  1591. size_t noffset;
  1592. int supp_rates_len, i;
  1593. u8 rates[32];
  1594. int num_rates;
  1595. int ext_rates_len;
  1596. int shift;
  1597. u32 rate_flags;
  1598. bool have_80mhz = false;
  1599. *offset = 0;
  1600. sband = local->hw.wiphy->bands[band];
  1601. if (WARN_ON_ONCE(!sband))
  1602. return 0;
  1603. rate_flags = ieee80211_chandef_rate_flags(chandef);
  1604. shift = ieee80211_chandef_get_shift(chandef);
  1605. num_rates = 0;
  1606. for (i = 0; i < sband->n_bitrates; i++) {
  1607. if ((BIT(i) & rate_mask) == 0)
  1608. continue; /* skip rate */
  1609. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1610. continue;
  1611. rates[num_rates++] =
  1612. (u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
  1613. (1 << shift) * 5);
  1614. }
  1615. supp_rates_len = min_t(int, num_rates, 8);
  1616. if (end - pos < 2 + supp_rates_len)
  1617. goto out_err;
  1618. *pos++ = WLAN_EID_SUPP_RATES;
  1619. *pos++ = supp_rates_len;
  1620. memcpy(pos, rates, supp_rates_len);
  1621. pos += supp_rates_len;
  1622. /* insert "request information" if in custom IEs */
  1623. if (ie && ie_len) {
  1624. static const u8 before_extrates[] = {
  1625. WLAN_EID_SSID,
  1626. WLAN_EID_SUPP_RATES,
  1627. WLAN_EID_REQUEST,
  1628. };
  1629. noffset = ieee80211_ie_split(ie, ie_len,
  1630. before_extrates,
  1631. ARRAY_SIZE(before_extrates),
  1632. *offset);
  1633. if (end - pos < noffset - *offset)
  1634. goto out_err;
  1635. memcpy(pos, ie + *offset, noffset - *offset);
  1636. pos += noffset - *offset;
  1637. *offset = noffset;
  1638. }
  1639. ext_rates_len = num_rates - supp_rates_len;
  1640. if (ext_rates_len > 0) {
  1641. if (end - pos < 2 + ext_rates_len)
  1642. goto out_err;
  1643. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1644. *pos++ = ext_rates_len;
  1645. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  1646. pos += ext_rates_len;
  1647. }
  1648. if (chandef->chan && sband->band == NL80211_BAND_2GHZ) {
  1649. if (end - pos < 3)
  1650. goto out_err;
  1651. *pos++ = WLAN_EID_DS_PARAMS;
  1652. *pos++ = 1;
  1653. *pos++ = ieee80211_frequency_to_channel(
  1654. chandef->chan->center_freq);
  1655. }
  1656. if (flags & IEEE80211_PROBE_FLAG_MIN_CONTENT)
  1657. goto done;
  1658. /* insert custom IEs that go before HT */
  1659. if (ie && ie_len) {
  1660. static const u8 before_ht[] = {
  1661. /*
  1662. * no need to list the ones split off already
  1663. * (or generated here)
  1664. */
  1665. WLAN_EID_DS_PARAMS,
  1666. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  1667. };
  1668. noffset = ieee80211_ie_split(ie, ie_len,
  1669. before_ht, ARRAY_SIZE(before_ht),
  1670. *offset);
  1671. if (end - pos < noffset - *offset)
  1672. goto out_err;
  1673. memcpy(pos, ie + *offset, noffset - *offset);
  1674. pos += noffset - *offset;
  1675. *offset = noffset;
  1676. }
  1677. if (sband->ht_cap.ht_supported) {
  1678. if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
  1679. goto out_err;
  1680. pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
  1681. sband->ht_cap.cap);
  1682. }
  1683. /* insert custom IEs that go before VHT */
  1684. if (ie && ie_len) {
  1685. static const u8 before_vht[] = {
  1686. /*
  1687. * no need to list the ones split off already
  1688. * (or generated here)
  1689. */
  1690. WLAN_EID_BSS_COEX_2040,
  1691. WLAN_EID_EXT_CAPABILITY,
  1692. WLAN_EID_SSID_LIST,
  1693. WLAN_EID_CHANNEL_USAGE,
  1694. WLAN_EID_INTERWORKING,
  1695. WLAN_EID_MESH_ID,
  1696. /* 60 GHz (Multi-band, DMG, MMS) can't happen */
  1697. };
  1698. noffset = ieee80211_ie_split(ie, ie_len,
  1699. before_vht, ARRAY_SIZE(before_vht),
  1700. *offset);
  1701. if (end - pos < noffset - *offset)
  1702. goto out_err;
  1703. memcpy(pos, ie + *offset, noffset - *offset);
  1704. pos += noffset - *offset;
  1705. *offset = noffset;
  1706. }
  1707. /* Check if any channel in this sband supports at least 80 MHz */
  1708. for (i = 0; i < sband->n_channels; i++) {
  1709. if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
  1710. IEEE80211_CHAN_NO_80MHZ))
  1711. continue;
  1712. have_80mhz = true;
  1713. break;
  1714. }
  1715. if (sband->vht_cap.vht_supported && have_80mhz) {
  1716. if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
  1717. goto out_err;
  1718. pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
  1719. sband->vht_cap.cap);
  1720. }
  1721. /* insert custom IEs that go before HE */
  1722. if (ie && ie_len) {
  1723. static const u8 before_he[] = {
  1724. /*
  1725. * no need to list the ones split off before VHT
  1726. * or generated here
  1727. */
  1728. WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_REQ_PARAMS,
  1729. WLAN_EID_AP_CSN,
  1730. /* TODO: add 11ah/11aj/11ak elements */
  1731. };
  1732. noffset = ieee80211_ie_split(ie, ie_len,
  1733. before_he, ARRAY_SIZE(before_he),
  1734. *offset);
  1735. if (end - pos < noffset - *offset)
  1736. goto out_err;
  1737. memcpy(pos, ie + *offset, noffset - *offset);
  1738. pos += noffset - *offset;
  1739. *offset = noffset;
  1740. }
  1741. he_cap = ieee80211_get_he_iftype_cap(sband,
  1742. ieee80211_vif_type_p2p(&sdata->vif));
  1743. if (he_cap &&
  1744. cfg80211_any_usable_channels(local->hw.wiphy, BIT(sband->band),
  1745. IEEE80211_CHAN_NO_HE)) {
  1746. pos = ieee80211_ie_build_he_cap(0, pos, he_cap, end);
  1747. if (!pos)
  1748. goto out_err;
  1749. }
  1750. eht_cap = ieee80211_get_eht_iftype_cap(sband,
  1751. ieee80211_vif_type_p2p(&sdata->vif));
  1752. if (eht_cap &&
  1753. cfg80211_any_usable_channels(local->hw.wiphy, BIT(sband->band),
  1754. IEEE80211_CHAN_NO_HE |
  1755. IEEE80211_CHAN_NO_EHT)) {
  1756. pos = ieee80211_ie_build_eht_cap(pos, he_cap, eht_cap, end,
  1757. sdata->vif.type == NL80211_IFTYPE_AP);
  1758. if (!pos)
  1759. goto out_err;
  1760. }
  1761. if (cfg80211_any_usable_channels(local->hw.wiphy,
  1762. BIT(NL80211_BAND_6GHZ),
  1763. IEEE80211_CHAN_NO_HE)) {
  1764. struct ieee80211_supported_band *sband6;
  1765. sband6 = local->hw.wiphy->bands[NL80211_BAND_6GHZ];
  1766. he_cap = ieee80211_get_he_iftype_cap(sband6,
  1767. ieee80211_vif_type_p2p(&sdata->vif));
  1768. if (he_cap) {
  1769. enum nl80211_iftype iftype =
  1770. ieee80211_vif_type_p2p(&sdata->vif);
  1771. __le16 cap = ieee80211_get_he_6ghz_capa(sband6, iftype);
  1772. pos = ieee80211_write_he_6ghz_cap(pos, cap, end);
  1773. }
  1774. }
  1775. /*
  1776. * If adding more here, adjust code in main.c
  1777. * that calculates local->scan_ies_len.
  1778. */
  1779. return pos - buffer;
  1780. out_err:
  1781. WARN_ONCE(1, "not enough space for preq IEs\n");
  1782. done:
  1783. return pos - buffer;
  1784. }
  1785. int ieee80211_build_preq_ies(struct ieee80211_sub_if_data *sdata, u8 *buffer,
  1786. size_t buffer_len,
  1787. struct ieee80211_scan_ies *ie_desc,
  1788. const u8 *ie, size_t ie_len,
  1789. u8 bands_used, u32 *rate_masks,
  1790. struct cfg80211_chan_def *chandef,
  1791. u32 flags)
  1792. {
  1793. size_t pos = 0, old_pos = 0, custom_ie_offset = 0;
  1794. int i;
  1795. memset(ie_desc, 0, sizeof(*ie_desc));
  1796. for (i = 0; i < NUM_NL80211_BANDS; i++) {
  1797. if (bands_used & BIT(i)) {
  1798. pos += ieee80211_build_preq_ies_band(sdata,
  1799. buffer + pos,
  1800. buffer_len - pos,
  1801. ie, ie_len, i,
  1802. rate_masks[i],
  1803. chandef,
  1804. &custom_ie_offset,
  1805. flags);
  1806. ie_desc->ies[i] = buffer + old_pos;
  1807. ie_desc->len[i] = pos - old_pos;
  1808. old_pos = pos;
  1809. }
  1810. }
  1811. /* add any remaining custom IEs */
  1812. if (ie && ie_len) {
  1813. if (WARN_ONCE(buffer_len - pos < ie_len - custom_ie_offset,
  1814. "not enough space for preq custom IEs\n"))
  1815. return pos;
  1816. memcpy(buffer + pos, ie + custom_ie_offset,
  1817. ie_len - custom_ie_offset);
  1818. ie_desc->common_ies = buffer + pos;
  1819. ie_desc->common_ie_len = ie_len - custom_ie_offset;
  1820. pos += ie_len - custom_ie_offset;
  1821. }
  1822. return pos;
  1823. };
  1824. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  1825. const u8 *src, const u8 *dst,
  1826. u32 ratemask,
  1827. struct ieee80211_channel *chan,
  1828. const u8 *ssid, size_t ssid_len,
  1829. const u8 *ie, size_t ie_len,
  1830. u32 flags)
  1831. {
  1832. struct ieee80211_local *local = sdata->local;
  1833. struct cfg80211_chan_def chandef;
  1834. struct sk_buff *skb;
  1835. struct ieee80211_mgmt *mgmt;
  1836. int ies_len;
  1837. u32 rate_masks[NUM_NL80211_BANDS] = {};
  1838. struct ieee80211_scan_ies dummy_ie_desc;
  1839. /*
  1840. * Do not send DS Channel parameter for directed probe requests
  1841. * in order to maximize the chance that we get a response. Some
  1842. * badly-behaved APs don't respond when this parameter is included.
  1843. */
  1844. chandef.width = sdata->vif.bss_conf.chandef.width;
  1845. if (flags & IEEE80211_PROBE_FLAG_DIRECTED)
  1846. chandef.chan = NULL;
  1847. else
  1848. chandef.chan = chan;
  1849. skb = ieee80211_probereq_get(&local->hw, src, ssid, ssid_len,
  1850. local->scan_ies_len + ie_len);
  1851. if (!skb)
  1852. return NULL;
  1853. rate_masks[chan->band] = ratemask;
  1854. ies_len = ieee80211_build_preq_ies(sdata, skb_tail_pointer(skb),
  1855. skb_tailroom(skb), &dummy_ie_desc,
  1856. ie, ie_len, BIT(chan->band),
  1857. rate_masks, &chandef, flags);
  1858. skb_put(skb, ies_len);
  1859. if (dst) {
  1860. mgmt = (struct ieee80211_mgmt *) skb->data;
  1861. memcpy(mgmt->da, dst, ETH_ALEN);
  1862. memcpy(mgmt->bssid, dst, ETH_ALEN);
  1863. }
  1864. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1865. return skb;
  1866. }
  1867. u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
  1868. struct ieee802_11_elems *elems,
  1869. enum nl80211_band band, u32 *basic_rates)
  1870. {
  1871. struct ieee80211_supported_band *sband;
  1872. size_t num_rates;
  1873. u32 supp_rates, rate_flags;
  1874. int i, j, shift;
  1875. sband = sdata->local->hw.wiphy->bands[band];
  1876. if (WARN_ON(!sband))
  1877. return 1;
  1878. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  1879. shift = ieee80211_vif_get_shift(&sdata->vif);
  1880. num_rates = sband->n_bitrates;
  1881. supp_rates = 0;
  1882. for (i = 0; i < elems->supp_rates_len +
  1883. elems->ext_supp_rates_len; i++) {
  1884. u8 rate = 0;
  1885. int own_rate;
  1886. bool is_basic;
  1887. if (i < elems->supp_rates_len)
  1888. rate = elems->supp_rates[i];
  1889. else if (elems->ext_supp_rates)
  1890. rate = elems->ext_supp_rates
  1891. [i - elems->supp_rates_len];
  1892. own_rate = 5 * (rate & 0x7f);
  1893. is_basic = !!(rate & 0x80);
  1894. if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  1895. continue;
  1896. for (j = 0; j < num_rates; j++) {
  1897. int brate;
  1898. if ((rate_flags & sband->bitrates[j].flags)
  1899. != rate_flags)
  1900. continue;
  1901. brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
  1902. 1 << shift);
  1903. if (brate == own_rate) {
  1904. supp_rates |= BIT(j);
  1905. if (basic_rates && is_basic)
  1906. *basic_rates |= BIT(j);
  1907. }
  1908. }
  1909. }
  1910. return supp_rates;
  1911. }
  1912. void ieee80211_stop_device(struct ieee80211_local *local)
  1913. {
  1914. ieee80211_led_radio(local, false);
  1915. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  1916. cancel_work_sync(&local->reconfig_filter);
  1917. flush_workqueue(local->workqueue);
  1918. drv_stop(local);
  1919. }
  1920. static void ieee80211_flush_completed_scan(struct ieee80211_local *local,
  1921. bool aborted)
  1922. {
  1923. /* It's possible that we don't handle the scan completion in
  1924. * time during suspend, so if it's still marked as completed
  1925. * here, queue the work and flush it to clean things up.
  1926. * Instead of calling the worker function directly here, we
  1927. * really queue it to avoid potential races with other flows
  1928. * scheduling the same work.
  1929. */
  1930. if (test_bit(SCAN_COMPLETED, &local->scanning)) {
  1931. /* If coming from reconfiguration failure, abort the scan so
  1932. * we don't attempt to continue a partial HW scan - which is
  1933. * possible otherwise if (e.g.) the 2.4 GHz portion was the
  1934. * completed scan, and a 5 GHz portion is still pending.
  1935. */
  1936. if (aborted)
  1937. set_bit(SCAN_ABORTED, &local->scanning);
  1938. wiphy_delayed_work_queue(local->hw.wiphy, &local->scan_work, 0);
  1939. wiphy_delayed_work_flush(local->hw.wiphy, &local->scan_work);
  1940. }
  1941. }
  1942. static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
  1943. {
  1944. struct ieee80211_sub_if_data *sdata;
  1945. struct ieee80211_chanctx *ctx;
  1946. /*
  1947. * We get here if during resume the device can't be restarted properly.
  1948. * We might also get here if this happens during HW reset, which is a
  1949. * slightly different situation and we need to drop all connections in
  1950. * the latter case.
  1951. *
  1952. * Ask cfg80211 to turn off all interfaces, this will result in more
  1953. * warnings but at least we'll then get into a clean stopped state.
  1954. */
  1955. local->resuming = false;
  1956. local->suspended = false;
  1957. local->in_reconfig = false;
  1958. ieee80211_flush_completed_scan(local, true);
  1959. /* scheduled scan clearly can't be running any more, but tell
  1960. * cfg80211 and clear local state
  1961. */
  1962. ieee80211_sched_scan_end(local);
  1963. list_for_each_entry(sdata, &local->interfaces, list)
  1964. sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
  1965. /* Mark channel contexts as not being in the driver any more to avoid
  1966. * removing them from the driver during the shutdown process...
  1967. */
  1968. mutex_lock(&local->chanctx_mtx);
  1969. list_for_each_entry(ctx, &local->chanctx_list, list)
  1970. ctx->driver_present = false;
  1971. mutex_unlock(&local->chanctx_mtx);
  1972. }
  1973. static void ieee80211_assign_chanctx(struct ieee80211_local *local,
  1974. struct ieee80211_sub_if_data *sdata,
  1975. struct ieee80211_link_data *link)
  1976. {
  1977. struct ieee80211_chanctx_conf *conf;
  1978. struct ieee80211_chanctx *ctx;
  1979. if (!local->use_chanctx)
  1980. return;
  1981. mutex_lock(&local->chanctx_mtx);
  1982. conf = rcu_dereference_protected(link->conf->chanctx_conf,
  1983. lockdep_is_held(&local->chanctx_mtx));
  1984. if (conf) {
  1985. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  1986. drv_assign_vif_chanctx(local, sdata, link->conf, ctx);
  1987. }
  1988. mutex_unlock(&local->chanctx_mtx);
  1989. }
  1990. static void ieee80211_reconfig_stations(struct ieee80211_sub_if_data *sdata)
  1991. {
  1992. struct ieee80211_local *local = sdata->local;
  1993. struct sta_info *sta;
  1994. /* add STAs back */
  1995. mutex_lock(&local->sta_mtx);
  1996. list_for_each_entry(sta, &local->sta_list, list) {
  1997. enum ieee80211_sta_state state;
  1998. if (!sta->uploaded || sta->sdata != sdata)
  1999. continue;
  2000. for (state = IEEE80211_STA_NOTEXIST;
  2001. state < sta->sta_state; state++)
  2002. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  2003. state + 1));
  2004. }
  2005. mutex_unlock(&local->sta_mtx);
  2006. }
  2007. static int ieee80211_reconfig_nan(struct ieee80211_sub_if_data *sdata)
  2008. {
  2009. struct cfg80211_nan_func *func, **funcs;
  2010. int res, id, i = 0;
  2011. res = drv_start_nan(sdata->local, sdata,
  2012. &sdata->u.nan.conf);
  2013. if (WARN_ON(res))
  2014. return res;
  2015. funcs = kcalloc(sdata->local->hw.max_nan_de_entries + 1,
  2016. sizeof(*funcs),
  2017. GFP_KERNEL);
  2018. if (!funcs)
  2019. return -ENOMEM;
  2020. /* Add all the functions:
  2021. * This is a little bit ugly. We need to call a potentially sleeping
  2022. * callback for each NAN function, so we can't hold the spinlock.
  2023. */
  2024. spin_lock_bh(&sdata->u.nan.func_lock);
  2025. idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, id)
  2026. funcs[i++] = func;
  2027. spin_unlock_bh(&sdata->u.nan.func_lock);
  2028. for (i = 0; funcs[i]; i++) {
  2029. res = drv_add_nan_func(sdata->local, sdata, funcs[i]);
  2030. if (WARN_ON(res))
  2031. ieee80211_nan_func_terminated(&sdata->vif,
  2032. funcs[i]->instance_id,
  2033. NL80211_NAN_FUNC_TERM_REASON_ERROR,
  2034. GFP_KERNEL);
  2035. }
  2036. kfree(funcs);
  2037. return 0;
  2038. }
  2039. int ieee80211_reconfig(struct ieee80211_local *local)
  2040. {
  2041. struct ieee80211_hw *hw = &local->hw;
  2042. struct ieee80211_sub_if_data *sdata;
  2043. struct ieee80211_chanctx *ctx;
  2044. struct sta_info *sta;
  2045. int res, i;
  2046. bool reconfig_due_to_wowlan = false;
  2047. struct ieee80211_sub_if_data *sched_scan_sdata;
  2048. struct cfg80211_sched_scan_request *sched_scan_req;
  2049. bool sched_scan_stopped = false;
  2050. bool suspended = local->suspended;
  2051. bool in_reconfig = false;
  2052. /* nothing to do if HW shouldn't run */
  2053. if (!local->open_count)
  2054. goto wake_up;
  2055. #ifdef CONFIG_PM
  2056. if (suspended)
  2057. local->resuming = true;
  2058. if (local->wowlan) {
  2059. /*
  2060. * In the wowlan case, both mac80211 and the device
  2061. * are functional when the resume op is called, so
  2062. * clear local->suspended so the device could operate
  2063. * normally (e.g. pass rx frames).
  2064. */
  2065. local->suspended = false;
  2066. res = drv_resume(local);
  2067. local->wowlan = false;
  2068. if (res < 0) {
  2069. local->resuming = false;
  2070. return res;
  2071. }
  2072. if (res == 0)
  2073. goto wake_up;
  2074. WARN_ON(res > 1);
  2075. /*
  2076. * res is 1, which means the driver requested
  2077. * to go through a regular reset on wakeup.
  2078. * restore local->suspended in this case.
  2079. */
  2080. reconfig_due_to_wowlan = true;
  2081. local->suspended = true;
  2082. }
  2083. #endif
  2084. /*
  2085. * In case of hw_restart during suspend (without wowlan),
  2086. * cancel restart work, as we are reconfiguring the device
  2087. * anyway.
  2088. * Note that restart_work is scheduled on a frozen workqueue,
  2089. * so we can't deadlock in this case.
  2090. */
  2091. if (suspended && local->in_reconfig && !reconfig_due_to_wowlan)
  2092. cancel_work_sync(&local->restart_work);
  2093. local->started = false;
  2094. /*
  2095. * Upon resume hardware can sometimes be goofy due to
  2096. * various platform / driver / bus issues, so restarting
  2097. * the device may at times not work immediately. Propagate
  2098. * the error.
  2099. */
  2100. res = drv_start(local);
  2101. if (res) {
  2102. if (suspended)
  2103. WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
  2104. else
  2105. WARN(1, "Hardware became unavailable during restart.\n");
  2106. ieee80211_handle_reconfig_failure(local);
  2107. return res;
  2108. }
  2109. /* setup fragmentation threshold */
  2110. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  2111. /* setup RTS threshold */
  2112. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  2113. /* reset coverage class */
  2114. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  2115. ieee80211_led_radio(local, true);
  2116. ieee80211_mod_tpt_led_trig(local,
  2117. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  2118. /* add interfaces */
  2119. sdata = wiphy_dereference(local->hw.wiphy, local->monitor_sdata);
  2120. if (sdata) {
  2121. /* in HW restart it exists already */
  2122. WARN_ON(local->resuming);
  2123. res = drv_add_interface(local, sdata);
  2124. if (WARN_ON(res)) {
  2125. RCU_INIT_POINTER(local->monitor_sdata, NULL);
  2126. synchronize_net();
  2127. kfree(sdata);
  2128. }
  2129. }
  2130. list_for_each_entry(sdata, &local->interfaces, list) {
  2131. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  2132. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  2133. ieee80211_sdata_running(sdata)) {
  2134. res = drv_add_interface(local, sdata);
  2135. if (WARN_ON(res))
  2136. break;
  2137. }
  2138. }
  2139. /* If adding any of the interfaces failed above, roll back and
  2140. * report failure.
  2141. */
  2142. if (res) {
  2143. list_for_each_entry_continue_reverse(sdata, &local->interfaces,
  2144. list)
  2145. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  2146. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  2147. ieee80211_sdata_running(sdata))
  2148. drv_remove_interface(local, sdata);
  2149. ieee80211_handle_reconfig_failure(local);
  2150. return res;
  2151. }
  2152. /* add channel contexts */
  2153. if (local->use_chanctx) {
  2154. mutex_lock(&local->chanctx_mtx);
  2155. list_for_each_entry(ctx, &local->chanctx_list, list)
  2156. if (ctx->replace_state !=
  2157. IEEE80211_CHANCTX_REPLACES_OTHER)
  2158. WARN_ON(drv_add_chanctx(local, ctx));
  2159. mutex_unlock(&local->chanctx_mtx);
  2160. sdata = wiphy_dereference(local->hw.wiphy,
  2161. local->monitor_sdata);
  2162. if (sdata && ieee80211_sdata_running(sdata))
  2163. ieee80211_assign_chanctx(local, sdata, &sdata->deflink);
  2164. }
  2165. /* reconfigure hardware */
  2166. ieee80211_hw_config(local, ~0);
  2167. ieee80211_configure_filter(local);
  2168. /* Finally also reconfigure all the BSS information */
  2169. list_for_each_entry(sdata, &local->interfaces, list) {
  2170. unsigned int link_id;
  2171. u32 changed;
  2172. if (!ieee80211_sdata_running(sdata))
  2173. continue;
  2174. sdata_lock(sdata);
  2175. for (link_id = 0;
  2176. link_id < ARRAY_SIZE(sdata->vif.link_conf);
  2177. link_id++) {
  2178. struct ieee80211_link_data *link;
  2179. link = sdata_dereference(sdata->link[link_id], sdata);
  2180. if (link)
  2181. ieee80211_assign_chanctx(local, sdata, link);
  2182. }
  2183. switch (sdata->vif.type) {
  2184. case NL80211_IFTYPE_AP_VLAN:
  2185. case NL80211_IFTYPE_MONITOR:
  2186. break;
  2187. case NL80211_IFTYPE_ADHOC:
  2188. if (sdata->vif.cfg.ibss_joined)
  2189. WARN_ON(drv_join_ibss(local, sdata));
  2190. fallthrough;
  2191. default:
  2192. ieee80211_reconfig_stations(sdata);
  2193. fallthrough;
  2194. case NL80211_IFTYPE_AP: /* AP stations are handled later */
  2195. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  2196. drv_conf_tx(local, &sdata->deflink, i,
  2197. &sdata->deflink.tx_conf[i]);
  2198. break;
  2199. }
  2200. sdata_unlock(sdata);
  2201. /* common change flags for all interface types */
  2202. changed = BSS_CHANGED_ERP_CTS_PROT |
  2203. BSS_CHANGED_ERP_PREAMBLE |
  2204. BSS_CHANGED_ERP_SLOT |
  2205. BSS_CHANGED_HT |
  2206. BSS_CHANGED_BASIC_RATES |
  2207. BSS_CHANGED_BEACON_INT |
  2208. BSS_CHANGED_BSSID |
  2209. BSS_CHANGED_CQM |
  2210. BSS_CHANGED_QOS |
  2211. BSS_CHANGED_IDLE |
  2212. BSS_CHANGED_TXPOWER |
  2213. BSS_CHANGED_MCAST_RATE;
  2214. if (sdata->vif.bss_conf.mu_mimo_owner)
  2215. changed |= BSS_CHANGED_MU_GROUPS;
  2216. switch (sdata->vif.type) {
  2217. case NL80211_IFTYPE_STATION:
  2218. changed |= BSS_CHANGED_ASSOC |
  2219. BSS_CHANGED_ARP_FILTER |
  2220. BSS_CHANGED_PS;
  2221. /* Re-send beacon info report to the driver */
  2222. if (sdata->deflink.u.mgd.have_beacon)
  2223. changed |= BSS_CHANGED_BEACON_INFO;
  2224. if (sdata->vif.bss_conf.max_idle_period ||
  2225. sdata->vif.bss_conf.protected_keep_alive)
  2226. changed |= BSS_CHANGED_KEEP_ALIVE;
  2227. sdata_lock(sdata);
  2228. ieee80211_bss_info_change_notify(sdata, changed);
  2229. sdata_unlock(sdata);
  2230. break;
  2231. case NL80211_IFTYPE_OCB:
  2232. changed |= BSS_CHANGED_OCB;
  2233. ieee80211_bss_info_change_notify(sdata, changed);
  2234. break;
  2235. case NL80211_IFTYPE_ADHOC:
  2236. changed |= BSS_CHANGED_IBSS;
  2237. fallthrough;
  2238. case NL80211_IFTYPE_AP:
  2239. changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
  2240. if (sdata->vif.bss_conf.ftm_responder == 1 &&
  2241. wiphy_ext_feature_isset(sdata->local->hw.wiphy,
  2242. NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
  2243. changed |= BSS_CHANGED_FTM_RESPONDER;
  2244. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  2245. changed |= BSS_CHANGED_AP_PROBE_RESP;
  2246. if (rcu_access_pointer(sdata->deflink.u.ap.beacon))
  2247. drv_start_ap(local, sdata,
  2248. sdata->deflink.conf);
  2249. }
  2250. fallthrough;
  2251. case NL80211_IFTYPE_MESH_POINT:
  2252. if (sdata->vif.bss_conf.enable_beacon) {
  2253. changed |= BSS_CHANGED_BEACON |
  2254. BSS_CHANGED_BEACON_ENABLED;
  2255. ieee80211_bss_info_change_notify(sdata, changed);
  2256. }
  2257. break;
  2258. case NL80211_IFTYPE_NAN:
  2259. res = ieee80211_reconfig_nan(sdata);
  2260. if (res < 0) {
  2261. ieee80211_handle_reconfig_failure(local);
  2262. return res;
  2263. }
  2264. break;
  2265. case NL80211_IFTYPE_AP_VLAN:
  2266. case NL80211_IFTYPE_MONITOR:
  2267. case NL80211_IFTYPE_P2P_DEVICE:
  2268. /* nothing to do */
  2269. break;
  2270. case NL80211_IFTYPE_UNSPECIFIED:
  2271. case NUM_NL80211_IFTYPES:
  2272. case NL80211_IFTYPE_P2P_CLIENT:
  2273. case NL80211_IFTYPE_P2P_GO:
  2274. case NL80211_IFTYPE_WDS:
  2275. WARN_ON(1);
  2276. break;
  2277. }
  2278. }
  2279. ieee80211_recalc_ps(local);
  2280. /*
  2281. * The sta might be in psm against the ap (e.g. because
  2282. * this was the state before a hw restart), so we
  2283. * explicitly send a null packet in order to make sure
  2284. * it'll sync against the ap (and get out of psm).
  2285. */
  2286. if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
  2287. list_for_each_entry(sdata, &local->interfaces, list) {
  2288. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2289. continue;
  2290. if (!sdata->u.mgd.associated)
  2291. continue;
  2292. ieee80211_send_nullfunc(local, sdata, false);
  2293. }
  2294. }
  2295. /* APs are now beaconing, add back stations */
  2296. list_for_each_entry(sdata, &local->interfaces, list) {
  2297. if (!ieee80211_sdata_running(sdata))
  2298. continue;
  2299. sdata_lock(sdata);
  2300. switch (sdata->vif.type) {
  2301. case NL80211_IFTYPE_AP_VLAN:
  2302. case NL80211_IFTYPE_AP:
  2303. ieee80211_reconfig_stations(sdata);
  2304. break;
  2305. default:
  2306. break;
  2307. }
  2308. sdata_unlock(sdata);
  2309. }
  2310. /* add back keys */
  2311. list_for_each_entry(sdata, &local->interfaces, list)
  2312. ieee80211_reenable_keys(sdata);
  2313. /* Reconfigure sched scan if it was interrupted by FW restart */
  2314. mutex_lock(&local->mtx);
  2315. sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
  2316. lockdep_is_held(&local->mtx));
  2317. sched_scan_req = rcu_dereference_protected(local->sched_scan_req,
  2318. lockdep_is_held(&local->mtx));
  2319. if (sched_scan_sdata && sched_scan_req)
  2320. /*
  2321. * Sched scan stopped, but we don't want to report it. Instead,
  2322. * we're trying to reschedule. However, if more than one scan
  2323. * plan was set, we cannot reschedule since we don't know which
  2324. * scan plan was currently running (and some scan plans may have
  2325. * already finished).
  2326. */
  2327. if (sched_scan_req->n_scan_plans > 1 ||
  2328. __ieee80211_request_sched_scan_start(sched_scan_sdata,
  2329. sched_scan_req)) {
  2330. RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
  2331. RCU_INIT_POINTER(local->sched_scan_req, NULL);
  2332. sched_scan_stopped = true;
  2333. }
  2334. mutex_unlock(&local->mtx);
  2335. if (sched_scan_stopped)
  2336. cfg80211_sched_scan_stopped_locked(local->hw.wiphy, 0);
  2337. wake_up:
  2338. if (local->monitors == local->open_count && local->monitors > 0)
  2339. ieee80211_add_virtual_monitor(local);
  2340. /*
  2341. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  2342. * sessions can be established after a resume.
  2343. *
  2344. * Also tear down aggregation sessions since reconfiguring
  2345. * them in a hardware restart scenario is not easily done
  2346. * right now, and the hardware will have lost information
  2347. * about the sessions, but we and the AP still think they
  2348. * are active. This is really a workaround though.
  2349. */
  2350. if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
  2351. mutex_lock(&local->sta_mtx);
  2352. list_for_each_entry(sta, &local->sta_list, list) {
  2353. if (!local->resuming)
  2354. ieee80211_sta_tear_down_BA_sessions(
  2355. sta, AGG_STOP_LOCAL_REQUEST);
  2356. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  2357. }
  2358. mutex_unlock(&local->sta_mtx);
  2359. }
  2360. /*
  2361. * If this is for hw restart things are still running.
  2362. * We may want to change that later, however.
  2363. */
  2364. if (local->open_count && (!suspended || reconfig_due_to_wowlan))
  2365. drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_RESTART);
  2366. if (local->in_reconfig) {
  2367. in_reconfig = local->in_reconfig;
  2368. local->in_reconfig = false;
  2369. barrier();
  2370. /* Restart deferred ROCs */
  2371. mutex_lock(&local->mtx);
  2372. ieee80211_start_next_roc(local);
  2373. mutex_unlock(&local->mtx);
  2374. /* Requeue all works */
  2375. list_for_each_entry(sdata, &local->interfaces, list)
  2376. ieee80211_queue_work(&local->hw, &sdata->work);
  2377. }
  2378. ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  2379. IEEE80211_QUEUE_STOP_REASON_SUSPEND,
  2380. false);
  2381. if (in_reconfig) {
  2382. list_for_each_entry(sdata, &local->interfaces, list) {
  2383. if (!ieee80211_sdata_running(sdata))
  2384. continue;
  2385. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  2386. ieee80211_sta_restart(sdata);
  2387. }
  2388. }
  2389. if (!suspended)
  2390. return 0;
  2391. #ifdef CONFIG_PM
  2392. /* first set suspended false, then resuming */
  2393. local->suspended = false;
  2394. mb();
  2395. local->resuming = false;
  2396. ieee80211_flush_completed_scan(local, false);
  2397. if (local->open_count && !reconfig_due_to_wowlan)
  2398. drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_SUSPEND);
  2399. list_for_each_entry(sdata, &local->interfaces, list) {
  2400. if (!ieee80211_sdata_running(sdata))
  2401. continue;
  2402. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  2403. ieee80211_sta_restart(sdata);
  2404. }
  2405. mod_timer(&local->sta_cleanup, jiffies + 1);
  2406. #else
  2407. WARN_ON(1);
  2408. #endif
  2409. return 0;
  2410. }
  2411. static void ieee80211_reconfig_disconnect(struct ieee80211_vif *vif, u8 flag)
  2412. {
  2413. struct ieee80211_sub_if_data *sdata;
  2414. struct ieee80211_local *local;
  2415. struct ieee80211_key *key;
  2416. if (WARN_ON(!vif))
  2417. return;
  2418. sdata = vif_to_sdata(vif);
  2419. local = sdata->local;
  2420. if (WARN_ON(flag & IEEE80211_SDATA_DISCONNECT_RESUME &&
  2421. !local->resuming))
  2422. return;
  2423. if (WARN_ON(flag & IEEE80211_SDATA_DISCONNECT_HW_RESTART &&
  2424. !local->in_reconfig))
  2425. return;
  2426. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  2427. return;
  2428. sdata->flags |= flag;
  2429. mutex_lock(&local->key_mtx);
  2430. list_for_each_entry(key, &sdata->key_list, list)
  2431. key->flags |= KEY_FLAG_TAINTED;
  2432. mutex_unlock(&local->key_mtx);
  2433. }
  2434. void ieee80211_hw_restart_disconnect(struct ieee80211_vif *vif)
  2435. {
  2436. ieee80211_reconfig_disconnect(vif, IEEE80211_SDATA_DISCONNECT_HW_RESTART);
  2437. }
  2438. EXPORT_SYMBOL_GPL(ieee80211_hw_restart_disconnect);
  2439. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  2440. {
  2441. ieee80211_reconfig_disconnect(vif, IEEE80211_SDATA_DISCONNECT_RESUME);
  2442. }
  2443. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  2444. void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata,
  2445. struct ieee80211_link_data *link)
  2446. {
  2447. struct ieee80211_local *local = sdata->local;
  2448. struct ieee80211_chanctx_conf *chanctx_conf;
  2449. struct ieee80211_chanctx *chanctx;
  2450. mutex_lock(&local->chanctx_mtx);
  2451. chanctx_conf = rcu_dereference_protected(link->conf->chanctx_conf,
  2452. lockdep_is_held(&local->chanctx_mtx));
  2453. /*
  2454. * This function can be called from a work, thus it may be possible
  2455. * that the chanctx_conf is removed (due to a disconnection, for
  2456. * example).
  2457. * So nothing should be done in such case.
  2458. */
  2459. if (!chanctx_conf)
  2460. goto unlock;
  2461. chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
  2462. ieee80211_recalc_smps_chanctx(local, chanctx);
  2463. unlock:
  2464. mutex_unlock(&local->chanctx_mtx);
  2465. }
  2466. void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata,
  2467. int link_id)
  2468. {
  2469. struct ieee80211_local *local = sdata->local;
  2470. struct ieee80211_chanctx_conf *chanctx_conf;
  2471. struct ieee80211_chanctx *chanctx;
  2472. int i;
  2473. mutex_lock(&local->chanctx_mtx);
  2474. for (i = 0; i < ARRAY_SIZE(sdata->vif.link_conf); i++) {
  2475. struct ieee80211_bss_conf *bss_conf;
  2476. if (link_id >= 0 && link_id != i)
  2477. continue;
  2478. rcu_read_lock();
  2479. bss_conf = rcu_dereference(sdata->vif.link_conf[i]);
  2480. if (!bss_conf) {
  2481. rcu_read_unlock();
  2482. continue;
  2483. }
  2484. chanctx_conf = rcu_dereference_protected(bss_conf->chanctx_conf,
  2485. lockdep_is_held(&local->chanctx_mtx));
  2486. /*
  2487. * Since we hold the chanctx_mtx (checked above)
  2488. * we can take the chanctx_conf pointer out of the
  2489. * RCU critical section, it cannot go away without
  2490. * the mutex. Just the way we reached it could - in
  2491. * theory - go away, but we don't really care and
  2492. * it really shouldn't happen anyway.
  2493. */
  2494. rcu_read_unlock();
  2495. if (!chanctx_conf)
  2496. goto unlock;
  2497. chanctx = container_of(chanctx_conf, struct ieee80211_chanctx,
  2498. conf);
  2499. ieee80211_recalc_chanctx_min_def(local, chanctx, NULL);
  2500. }
  2501. unlock:
  2502. mutex_unlock(&local->chanctx_mtx);
  2503. }
  2504. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  2505. {
  2506. size_t pos = offset;
  2507. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  2508. pos += 2 + ies[pos + 1];
  2509. return pos;
  2510. }
  2511. u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  2512. u16 cap)
  2513. {
  2514. __le16 tmp;
  2515. *pos++ = WLAN_EID_HT_CAPABILITY;
  2516. *pos++ = sizeof(struct ieee80211_ht_cap);
  2517. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  2518. /* capability flags */
  2519. tmp = cpu_to_le16(cap);
  2520. memcpy(pos, &tmp, sizeof(u16));
  2521. pos += sizeof(u16);
  2522. /* AMPDU parameters */
  2523. *pos++ = ht_cap->ampdu_factor |
  2524. (ht_cap->ampdu_density <<
  2525. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  2526. /* MCS set */
  2527. memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
  2528. pos += sizeof(ht_cap->mcs);
  2529. /* extended capabilities */
  2530. pos += sizeof(__le16);
  2531. /* BF capabilities */
  2532. pos += sizeof(__le32);
  2533. /* antenna selection */
  2534. pos += sizeof(u8);
  2535. return pos;
  2536. }
  2537. u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
  2538. u32 cap)
  2539. {
  2540. __le32 tmp;
  2541. *pos++ = WLAN_EID_VHT_CAPABILITY;
  2542. *pos++ = sizeof(struct ieee80211_vht_cap);
  2543. memset(pos, 0, sizeof(struct ieee80211_vht_cap));
  2544. /* capability flags */
  2545. tmp = cpu_to_le32(cap);
  2546. memcpy(pos, &tmp, sizeof(u32));
  2547. pos += sizeof(u32);
  2548. /* VHT MCS set */
  2549. memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
  2550. pos += sizeof(vht_cap->vht_mcs);
  2551. return pos;
  2552. }
  2553. u8 ieee80211_ie_len_he_cap(struct ieee80211_sub_if_data *sdata, u8 iftype)
  2554. {
  2555. const struct ieee80211_sta_he_cap *he_cap;
  2556. struct ieee80211_supported_band *sband;
  2557. u8 n;
  2558. sband = ieee80211_get_sband(sdata);
  2559. if (!sband)
  2560. return 0;
  2561. he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
  2562. if (!he_cap)
  2563. return 0;
  2564. n = ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem);
  2565. return 2 + 1 +
  2566. sizeof(he_cap->he_cap_elem) + n +
  2567. ieee80211_he_ppe_size(he_cap->ppe_thres[0],
  2568. he_cap->he_cap_elem.phy_cap_info);
  2569. }
  2570. u8 *ieee80211_ie_build_he_cap(ieee80211_conn_flags_t disable_flags, u8 *pos,
  2571. const struct ieee80211_sta_he_cap *he_cap,
  2572. u8 *end)
  2573. {
  2574. struct ieee80211_he_cap_elem elem;
  2575. u8 n;
  2576. u8 ie_len;
  2577. u8 *orig_pos = pos;
  2578. /* Make sure we have place for the IE */
  2579. /*
  2580. * TODO: the 1 added is because this temporarily is under the EXTENSION
  2581. * IE. Get rid of it when it moves.
  2582. */
  2583. if (!he_cap)
  2584. return orig_pos;
  2585. /* modify on stack first to calculate 'n' and 'ie_len' correctly */
  2586. elem = he_cap->he_cap_elem;
  2587. if (disable_flags & IEEE80211_CONN_DISABLE_40MHZ)
  2588. elem.phy_cap_info[0] &=
  2589. ~(IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
  2590. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G);
  2591. if (disable_flags & IEEE80211_CONN_DISABLE_160MHZ)
  2592. elem.phy_cap_info[0] &=
  2593. ~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
  2594. if (disable_flags & IEEE80211_CONN_DISABLE_80P80MHZ)
  2595. elem.phy_cap_info[0] &=
  2596. ~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
  2597. n = ieee80211_he_mcs_nss_size(&elem);
  2598. ie_len = 2 + 1 +
  2599. sizeof(he_cap->he_cap_elem) + n +
  2600. ieee80211_he_ppe_size(he_cap->ppe_thres[0],
  2601. he_cap->he_cap_elem.phy_cap_info);
  2602. if ((end - pos) < ie_len)
  2603. return orig_pos;
  2604. *pos++ = WLAN_EID_EXTENSION;
  2605. pos++; /* We'll set the size later below */
  2606. *pos++ = WLAN_EID_EXT_HE_CAPABILITY;
  2607. /* Fixed data */
  2608. memcpy(pos, &elem, sizeof(elem));
  2609. pos += sizeof(elem);
  2610. memcpy(pos, &he_cap->he_mcs_nss_supp, n);
  2611. pos += n;
  2612. /* Check if PPE Threshold should be present */
  2613. if ((he_cap->he_cap_elem.phy_cap_info[6] &
  2614. IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) == 0)
  2615. goto end;
  2616. /*
  2617. * Calculate how many PPET16/PPET8 pairs are to come. Algorithm:
  2618. * (NSS_M1 + 1) x (num of 1 bits in RU_INDEX_BITMASK)
  2619. */
  2620. n = hweight8(he_cap->ppe_thres[0] &
  2621. IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
  2622. n *= (1 + ((he_cap->ppe_thres[0] & IEEE80211_PPE_THRES_NSS_MASK) >>
  2623. IEEE80211_PPE_THRES_NSS_POS));
  2624. /*
  2625. * Each pair is 6 bits, and we need to add the 7 "header" bits to the
  2626. * total size.
  2627. */
  2628. n = (n * IEEE80211_PPE_THRES_INFO_PPET_SIZE * 2) + 7;
  2629. n = DIV_ROUND_UP(n, 8);
  2630. /* Copy PPE Thresholds */
  2631. memcpy(pos, &he_cap->ppe_thres, n);
  2632. pos += n;
  2633. end:
  2634. orig_pos[1] = (pos - orig_pos) - 2;
  2635. return pos;
  2636. }
  2637. void ieee80211_ie_build_he_6ghz_cap(struct ieee80211_sub_if_data *sdata,
  2638. enum ieee80211_smps_mode smps_mode,
  2639. struct sk_buff *skb)
  2640. {
  2641. struct ieee80211_supported_band *sband;
  2642. const struct ieee80211_sband_iftype_data *iftd;
  2643. enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
  2644. u8 *pos;
  2645. u16 cap;
  2646. if (!cfg80211_any_usable_channels(sdata->local->hw.wiphy,
  2647. BIT(NL80211_BAND_6GHZ),
  2648. IEEE80211_CHAN_NO_HE))
  2649. return;
  2650. sband = sdata->local->hw.wiphy->bands[NL80211_BAND_6GHZ];
  2651. iftd = ieee80211_get_sband_iftype_data(sband, iftype);
  2652. if (!iftd)
  2653. return;
  2654. /* Check for device HE 6 GHz capability before adding element */
  2655. if (!iftd->he_6ghz_capa.capa)
  2656. return;
  2657. cap = le16_to_cpu(iftd->he_6ghz_capa.capa);
  2658. cap &= ~IEEE80211_HE_6GHZ_CAP_SM_PS;
  2659. switch (smps_mode) {
  2660. case IEEE80211_SMPS_AUTOMATIC:
  2661. case IEEE80211_SMPS_NUM_MODES:
  2662. WARN_ON(1);
  2663. fallthrough;
  2664. case IEEE80211_SMPS_OFF:
  2665. cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
  2666. IEEE80211_HE_6GHZ_CAP_SM_PS);
  2667. break;
  2668. case IEEE80211_SMPS_STATIC:
  2669. cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_STATIC,
  2670. IEEE80211_HE_6GHZ_CAP_SM_PS);
  2671. break;
  2672. case IEEE80211_SMPS_DYNAMIC:
  2673. cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
  2674. IEEE80211_HE_6GHZ_CAP_SM_PS);
  2675. break;
  2676. }
  2677. pos = skb_put(skb, 2 + 1 + sizeof(cap));
  2678. ieee80211_write_he_6ghz_cap(pos, cpu_to_le16(cap),
  2679. pos + 2 + 1 + sizeof(cap));
  2680. }
  2681. u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  2682. const struct cfg80211_chan_def *chandef,
  2683. u16 prot_mode, bool rifs_mode)
  2684. {
  2685. struct ieee80211_ht_operation *ht_oper;
  2686. /* Build HT Information */
  2687. *pos++ = WLAN_EID_HT_OPERATION;
  2688. *pos++ = sizeof(struct ieee80211_ht_operation);
  2689. ht_oper = (struct ieee80211_ht_operation *)pos;
  2690. ht_oper->primary_chan = ieee80211_frequency_to_channel(
  2691. chandef->chan->center_freq);
  2692. switch (chandef->width) {
  2693. case NL80211_CHAN_WIDTH_160:
  2694. case NL80211_CHAN_WIDTH_80P80:
  2695. case NL80211_CHAN_WIDTH_80:
  2696. case NL80211_CHAN_WIDTH_40:
  2697. if (chandef->center_freq1 > chandef->chan->center_freq)
  2698. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  2699. else
  2700. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  2701. break;
  2702. case NL80211_CHAN_WIDTH_320:
  2703. /* HT information element should not be included on 6GHz */
  2704. WARN_ON(1);
  2705. return pos;
  2706. default:
  2707. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  2708. break;
  2709. }
  2710. if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
  2711. chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
  2712. chandef->width != NL80211_CHAN_WIDTH_20)
  2713. ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
  2714. if (rifs_mode)
  2715. ht_oper->ht_param |= IEEE80211_HT_PARAM_RIFS_MODE;
  2716. ht_oper->operation_mode = cpu_to_le16(prot_mode);
  2717. ht_oper->stbc_param = 0x0000;
  2718. /* It seems that Basic MCS set and Supported MCS set
  2719. are identical for the first 10 bytes */
  2720. memset(&ht_oper->basic_set, 0, 16);
  2721. memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
  2722. return pos + sizeof(struct ieee80211_ht_operation);
  2723. }
  2724. void ieee80211_ie_build_wide_bw_cs(u8 *pos,
  2725. const struct cfg80211_chan_def *chandef)
  2726. {
  2727. *pos++ = WLAN_EID_WIDE_BW_CHANNEL_SWITCH; /* EID */
  2728. *pos++ = 3; /* IE length */
  2729. /* New channel width */
  2730. switch (chandef->width) {
  2731. case NL80211_CHAN_WIDTH_80:
  2732. *pos++ = IEEE80211_VHT_CHANWIDTH_80MHZ;
  2733. break;
  2734. case NL80211_CHAN_WIDTH_160:
  2735. *pos++ = IEEE80211_VHT_CHANWIDTH_160MHZ;
  2736. break;
  2737. case NL80211_CHAN_WIDTH_80P80:
  2738. *pos++ = IEEE80211_VHT_CHANWIDTH_80P80MHZ;
  2739. break;
  2740. case NL80211_CHAN_WIDTH_320:
  2741. /* The behavior is not defined for 320 MHz channels */
  2742. WARN_ON(1);
  2743. fallthrough;
  2744. default:
  2745. *pos++ = IEEE80211_VHT_CHANWIDTH_USE_HT;
  2746. }
  2747. /* new center frequency segment 0 */
  2748. *pos++ = ieee80211_frequency_to_channel(chandef->center_freq1);
  2749. /* new center frequency segment 1 */
  2750. if (chandef->center_freq2)
  2751. *pos++ = ieee80211_frequency_to_channel(chandef->center_freq2);
  2752. else
  2753. *pos++ = 0;
  2754. }
  2755. u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
  2756. const struct cfg80211_chan_def *chandef)
  2757. {
  2758. struct ieee80211_vht_operation *vht_oper;
  2759. *pos++ = WLAN_EID_VHT_OPERATION;
  2760. *pos++ = sizeof(struct ieee80211_vht_operation);
  2761. vht_oper = (struct ieee80211_vht_operation *)pos;
  2762. vht_oper->center_freq_seg0_idx = ieee80211_frequency_to_channel(
  2763. chandef->center_freq1);
  2764. if (chandef->center_freq2)
  2765. vht_oper->center_freq_seg1_idx =
  2766. ieee80211_frequency_to_channel(chandef->center_freq2);
  2767. else
  2768. vht_oper->center_freq_seg1_idx = 0x00;
  2769. switch (chandef->width) {
  2770. case NL80211_CHAN_WIDTH_160:
  2771. /*
  2772. * Convert 160 MHz channel width to new style as interop
  2773. * workaround.
  2774. */
  2775. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
  2776. vht_oper->center_freq_seg1_idx = vht_oper->center_freq_seg0_idx;
  2777. if (chandef->chan->center_freq < chandef->center_freq1)
  2778. vht_oper->center_freq_seg0_idx -= 8;
  2779. else
  2780. vht_oper->center_freq_seg0_idx += 8;
  2781. break;
  2782. case NL80211_CHAN_WIDTH_80P80:
  2783. /*
  2784. * Convert 80+80 MHz channel width to new style as interop
  2785. * workaround.
  2786. */
  2787. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
  2788. break;
  2789. case NL80211_CHAN_WIDTH_80:
  2790. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
  2791. break;
  2792. case NL80211_CHAN_WIDTH_320:
  2793. /* VHT information element should not be included on 6GHz */
  2794. WARN_ON(1);
  2795. return pos;
  2796. default:
  2797. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
  2798. break;
  2799. }
  2800. /* don't require special VHT peer rates */
  2801. vht_oper->basic_mcs_set = cpu_to_le16(0xffff);
  2802. return pos + sizeof(struct ieee80211_vht_operation);
  2803. }
  2804. u8 *ieee80211_ie_build_he_oper(u8 *pos, struct cfg80211_chan_def *chandef)
  2805. {
  2806. struct ieee80211_he_operation *he_oper;
  2807. struct ieee80211_he_6ghz_oper *he_6ghz_op;
  2808. u32 he_oper_params;
  2809. u8 ie_len = 1 + sizeof(struct ieee80211_he_operation);
  2810. if (chandef->chan->band == NL80211_BAND_6GHZ)
  2811. ie_len += sizeof(struct ieee80211_he_6ghz_oper);
  2812. *pos++ = WLAN_EID_EXTENSION;
  2813. *pos++ = ie_len;
  2814. *pos++ = WLAN_EID_EXT_HE_OPERATION;
  2815. he_oper_params = 0;
  2816. he_oper_params |= u32_encode_bits(1023, /* disabled */
  2817. IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
  2818. he_oper_params |= u32_encode_bits(1,
  2819. IEEE80211_HE_OPERATION_ER_SU_DISABLE);
  2820. he_oper_params |= u32_encode_bits(1,
  2821. IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
  2822. if (chandef->chan->band == NL80211_BAND_6GHZ)
  2823. he_oper_params |= u32_encode_bits(1,
  2824. IEEE80211_HE_OPERATION_6GHZ_OP_INFO);
  2825. he_oper = (struct ieee80211_he_operation *)pos;
  2826. he_oper->he_oper_params = cpu_to_le32(he_oper_params);
  2827. /* don't require special HE peer rates */
  2828. he_oper->he_mcs_nss_set = cpu_to_le16(0xffff);
  2829. pos += sizeof(struct ieee80211_he_operation);
  2830. if (chandef->chan->band != NL80211_BAND_6GHZ)
  2831. goto out;
  2832. /* TODO add VHT operational */
  2833. he_6ghz_op = (struct ieee80211_he_6ghz_oper *)pos;
  2834. he_6ghz_op->minrate = 6; /* 6 Mbps */
  2835. he_6ghz_op->primary =
  2836. ieee80211_frequency_to_channel(chandef->chan->center_freq);
  2837. he_6ghz_op->ccfs0 =
  2838. ieee80211_frequency_to_channel(chandef->center_freq1);
  2839. if (chandef->center_freq2)
  2840. he_6ghz_op->ccfs1 =
  2841. ieee80211_frequency_to_channel(chandef->center_freq2);
  2842. else
  2843. he_6ghz_op->ccfs1 = 0;
  2844. switch (chandef->width) {
  2845. case NL80211_CHAN_WIDTH_320:
  2846. /*
  2847. * TODO: mesh operation is not defined over 6GHz 320 MHz
  2848. * channels.
  2849. */
  2850. WARN_ON(1);
  2851. break;
  2852. case NL80211_CHAN_WIDTH_160:
  2853. /* Convert 160 MHz channel width to new style as interop
  2854. * workaround.
  2855. */
  2856. he_6ghz_op->control =
  2857. IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ;
  2858. he_6ghz_op->ccfs1 = he_6ghz_op->ccfs0;
  2859. if (chandef->chan->center_freq < chandef->center_freq1)
  2860. he_6ghz_op->ccfs0 -= 8;
  2861. else
  2862. he_6ghz_op->ccfs0 += 8;
  2863. fallthrough;
  2864. case NL80211_CHAN_WIDTH_80P80:
  2865. he_6ghz_op->control =
  2866. IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ;
  2867. break;
  2868. case NL80211_CHAN_WIDTH_80:
  2869. he_6ghz_op->control =
  2870. IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ;
  2871. break;
  2872. case NL80211_CHAN_WIDTH_40:
  2873. he_6ghz_op->control =
  2874. IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ;
  2875. break;
  2876. default:
  2877. he_6ghz_op->control =
  2878. IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ;
  2879. break;
  2880. }
  2881. pos += sizeof(struct ieee80211_he_6ghz_oper);
  2882. out:
  2883. return pos;
  2884. }
  2885. bool ieee80211_chandef_ht_oper(const struct ieee80211_ht_operation *ht_oper,
  2886. struct cfg80211_chan_def *chandef)
  2887. {
  2888. enum nl80211_channel_type channel_type;
  2889. if (!ht_oper)
  2890. return false;
  2891. switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  2892. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  2893. channel_type = NL80211_CHAN_HT20;
  2894. break;
  2895. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  2896. channel_type = NL80211_CHAN_HT40PLUS;
  2897. break;
  2898. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  2899. channel_type = NL80211_CHAN_HT40MINUS;
  2900. break;
  2901. default:
  2902. return false;
  2903. }
  2904. cfg80211_chandef_create(chandef, chandef->chan, channel_type);
  2905. return true;
  2906. }
  2907. bool ieee80211_chandef_vht_oper(struct ieee80211_hw *hw, u32 vht_cap_info,
  2908. const struct ieee80211_vht_operation *oper,
  2909. const struct ieee80211_ht_operation *htop,
  2910. struct cfg80211_chan_def *chandef)
  2911. {
  2912. struct cfg80211_chan_def new = *chandef;
  2913. int cf0, cf1;
  2914. int ccfs0, ccfs1, ccfs2;
  2915. int ccf0, ccf1;
  2916. u32 vht_cap;
  2917. bool support_80_80 = false;
  2918. bool support_160 = false;
  2919. u8 ext_nss_bw_supp = u32_get_bits(vht_cap_info,
  2920. IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
  2921. u8 supp_chwidth = u32_get_bits(vht_cap_info,
  2922. IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
  2923. if (!oper || !htop)
  2924. return false;
  2925. vht_cap = hw->wiphy->bands[chandef->chan->band]->vht_cap.cap;
  2926. support_160 = (vht_cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK |
  2927. IEEE80211_VHT_CAP_EXT_NSS_BW_MASK));
  2928. support_80_80 = ((vht_cap &
  2929. IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) ||
  2930. (vht_cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
  2931. vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) ||
  2932. ((vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) >>
  2933. IEEE80211_VHT_CAP_EXT_NSS_BW_SHIFT > 1));
  2934. ccfs0 = oper->center_freq_seg0_idx;
  2935. ccfs1 = oper->center_freq_seg1_idx;
  2936. ccfs2 = (le16_to_cpu(htop->operation_mode) &
  2937. IEEE80211_HT_OP_MODE_CCFS2_MASK)
  2938. >> IEEE80211_HT_OP_MODE_CCFS2_SHIFT;
  2939. ccf0 = ccfs0;
  2940. /* if not supported, parse as though we didn't understand it */
  2941. if (!ieee80211_hw_check(hw, SUPPORTS_VHT_EXT_NSS_BW))
  2942. ext_nss_bw_supp = 0;
  2943. /*
  2944. * Cf. IEEE 802.11 Table 9-250
  2945. *
  2946. * We really just consider that because it's inefficient to connect
  2947. * at a higher bandwidth than we'll actually be able to use.
  2948. */
  2949. switch ((supp_chwidth << 4) | ext_nss_bw_supp) {
  2950. default:
  2951. case 0x00:
  2952. ccf1 = 0;
  2953. support_160 = false;
  2954. support_80_80 = false;
  2955. break;
  2956. case 0x01:
  2957. support_80_80 = false;
  2958. fallthrough;
  2959. case 0x02:
  2960. case 0x03:
  2961. ccf1 = ccfs2;
  2962. break;
  2963. case 0x10:
  2964. ccf1 = ccfs1;
  2965. break;
  2966. case 0x11:
  2967. case 0x12:
  2968. if (!ccfs1)
  2969. ccf1 = ccfs2;
  2970. else
  2971. ccf1 = ccfs1;
  2972. break;
  2973. case 0x13:
  2974. case 0x20:
  2975. case 0x23:
  2976. ccf1 = ccfs1;
  2977. break;
  2978. }
  2979. cf0 = ieee80211_channel_to_frequency(ccf0, chandef->chan->band);
  2980. cf1 = ieee80211_channel_to_frequency(ccf1, chandef->chan->band);
  2981. switch (oper->chan_width) {
  2982. case IEEE80211_VHT_CHANWIDTH_USE_HT:
  2983. /* just use HT information directly */
  2984. break;
  2985. case IEEE80211_VHT_CHANWIDTH_80MHZ:
  2986. new.width = NL80211_CHAN_WIDTH_80;
  2987. new.center_freq1 = cf0;
  2988. /* If needed, adjust based on the newer interop workaround. */
  2989. if (ccf1) {
  2990. unsigned int diff;
  2991. diff = abs(ccf1 - ccf0);
  2992. if ((diff == 8) && support_160) {
  2993. new.width = NL80211_CHAN_WIDTH_160;
  2994. new.center_freq1 = cf1;
  2995. } else if ((diff > 8) && support_80_80) {
  2996. new.width = NL80211_CHAN_WIDTH_80P80;
  2997. new.center_freq2 = cf1;
  2998. }
  2999. }
  3000. break;
  3001. case IEEE80211_VHT_CHANWIDTH_160MHZ:
  3002. /* deprecated encoding */
  3003. new.width = NL80211_CHAN_WIDTH_160;
  3004. new.center_freq1 = cf0;
  3005. break;
  3006. case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
  3007. /* deprecated encoding */
  3008. new.width = NL80211_CHAN_WIDTH_80P80;
  3009. new.center_freq1 = cf0;
  3010. new.center_freq2 = cf1;
  3011. break;
  3012. default:
  3013. return false;
  3014. }
  3015. if (!cfg80211_chandef_valid(&new))
  3016. return false;
  3017. *chandef = new;
  3018. return true;
  3019. }
  3020. void ieee80211_chandef_eht_oper(const struct ieee80211_eht_operation *eht_oper,
  3021. bool support_160, bool support_320,
  3022. struct cfg80211_chan_def *chandef)
  3023. {
  3024. struct ieee80211_eht_operation_info *info = (void *)eht_oper->optional;
  3025. chandef->center_freq1 =
  3026. ieee80211_channel_to_frequency(info->ccfs0,
  3027. chandef->chan->band);
  3028. switch (u8_get_bits(info->control,
  3029. IEEE80211_EHT_OPER_CHAN_WIDTH)) {
  3030. case IEEE80211_EHT_OPER_CHAN_WIDTH_20MHZ:
  3031. chandef->width = NL80211_CHAN_WIDTH_20;
  3032. break;
  3033. case IEEE80211_EHT_OPER_CHAN_WIDTH_40MHZ:
  3034. chandef->width = NL80211_CHAN_WIDTH_40;
  3035. break;
  3036. case IEEE80211_EHT_OPER_CHAN_WIDTH_80MHZ:
  3037. chandef->width = NL80211_CHAN_WIDTH_80;
  3038. break;
  3039. case IEEE80211_EHT_OPER_CHAN_WIDTH_160MHZ:
  3040. if (support_160) {
  3041. chandef->width = NL80211_CHAN_WIDTH_160;
  3042. chandef->center_freq1 =
  3043. ieee80211_channel_to_frequency(info->ccfs1,
  3044. chandef->chan->band);
  3045. } else {
  3046. chandef->width = NL80211_CHAN_WIDTH_80;
  3047. }
  3048. break;
  3049. case IEEE80211_EHT_OPER_CHAN_WIDTH_320MHZ:
  3050. if (support_320) {
  3051. chandef->width = NL80211_CHAN_WIDTH_320;
  3052. chandef->center_freq1 =
  3053. ieee80211_channel_to_frequency(info->ccfs1,
  3054. chandef->chan->band);
  3055. } else if (support_160) {
  3056. chandef->width = NL80211_CHAN_WIDTH_160;
  3057. } else {
  3058. chandef->width = NL80211_CHAN_WIDTH_80;
  3059. if (chandef->center_freq1 > chandef->chan->center_freq)
  3060. chandef->center_freq1 -= 40;
  3061. else
  3062. chandef->center_freq1 += 40;
  3063. }
  3064. break;
  3065. }
  3066. }
  3067. bool ieee80211_chandef_he_6ghz_oper(struct ieee80211_sub_if_data *sdata,
  3068. const struct ieee80211_he_operation *he_oper,
  3069. const struct ieee80211_eht_operation *eht_oper,
  3070. struct cfg80211_chan_def *chandef)
  3071. {
  3072. struct ieee80211_local *local = sdata->local;
  3073. struct ieee80211_supported_band *sband;
  3074. enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
  3075. const struct ieee80211_sta_he_cap *he_cap;
  3076. const struct ieee80211_sta_eht_cap *eht_cap;
  3077. struct cfg80211_chan_def he_chandef = *chandef;
  3078. const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
  3079. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  3080. bool support_80_80, support_160, support_320;
  3081. u8 he_phy_cap, eht_phy_cap;
  3082. u32 freq;
  3083. if (chandef->chan->band != NL80211_BAND_6GHZ)
  3084. return true;
  3085. sband = local->hw.wiphy->bands[NL80211_BAND_6GHZ];
  3086. he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
  3087. if (!he_cap) {
  3088. sdata_info(sdata, "Missing iftype sband data/HE cap");
  3089. return false;
  3090. }
  3091. he_phy_cap = he_cap->he_cap_elem.phy_cap_info[0];
  3092. support_160 =
  3093. he_phy_cap &
  3094. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
  3095. support_80_80 =
  3096. he_phy_cap &
  3097. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
  3098. if (!he_oper) {
  3099. sdata_info(sdata,
  3100. "HE is not advertised on (on %d MHz), expect issues\n",
  3101. chandef->chan->center_freq);
  3102. return false;
  3103. }
  3104. eht_cap = ieee80211_get_eht_iftype_cap(sband, iftype);
  3105. if (!eht_cap)
  3106. eht_oper = NULL;
  3107. he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
  3108. if (!he_6ghz_oper) {
  3109. sdata_info(sdata,
  3110. "HE 6GHz operation missing (on %d MHz), expect issues\n",
  3111. chandef->chan->center_freq);
  3112. return false;
  3113. }
  3114. /*
  3115. * The EHT operation IE does not contain the primary channel so the
  3116. * primary channel frequency should be taken from the 6 GHz operation
  3117. * information.
  3118. */
  3119. freq = ieee80211_channel_to_frequency(he_6ghz_oper->primary,
  3120. NL80211_BAND_6GHZ);
  3121. he_chandef.chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
  3122. switch (u8_get_bits(he_6ghz_oper->control,
  3123. IEEE80211_HE_6GHZ_OPER_CTRL_REG_INFO)) {
  3124. case IEEE80211_6GHZ_CTRL_REG_LPI_AP:
  3125. bss_conf->power_type = IEEE80211_REG_LPI_AP;
  3126. break;
  3127. case IEEE80211_6GHZ_CTRL_REG_SP_AP:
  3128. bss_conf->power_type = IEEE80211_REG_SP_AP;
  3129. break;
  3130. default:
  3131. bss_conf->power_type = IEEE80211_REG_UNSET_AP;
  3132. break;
  3133. }
  3134. if (!eht_oper ||
  3135. !(eht_oper->params & IEEE80211_EHT_OPER_INFO_PRESENT)) {
  3136. switch (u8_get_bits(he_6ghz_oper->control,
  3137. IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH)) {
  3138. case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ:
  3139. he_chandef.width = NL80211_CHAN_WIDTH_20;
  3140. break;
  3141. case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ:
  3142. he_chandef.width = NL80211_CHAN_WIDTH_40;
  3143. break;
  3144. case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ:
  3145. he_chandef.width = NL80211_CHAN_WIDTH_80;
  3146. break;
  3147. case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ:
  3148. he_chandef.width = NL80211_CHAN_WIDTH_80;
  3149. if (!he_6ghz_oper->ccfs1)
  3150. break;
  3151. if (abs(he_6ghz_oper->ccfs1 - he_6ghz_oper->ccfs0) == 8) {
  3152. if (support_160)
  3153. he_chandef.width = NL80211_CHAN_WIDTH_160;
  3154. } else {
  3155. if (support_80_80)
  3156. he_chandef.width = NL80211_CHAN_WIDTH_80P80;
  3157. }
  3158. break;
  3159. }
  3160. if (he_chandef.width == NL80211_CHAN_WIDTH_160) {
  3161. he_chandef.center_freq1 =
  3162. ieee80211_channel_to_frequency(he_6ghz_oper->ccfs1,
  3163. NL80211_BAND_6GHZ);
  3164. } else {
  3165. he_chandef.center_freq1 =
  3166. ieee80211_channel_to_frequency(he_6ghz_oper->ccfs0,
  3167. NL80211_BAND_6GHZ);
  3168. if (support_80_80 || support_160)
  3169. he_chandef.center_freq2 =
  3170. ieee80211_channel_to_frequency(he_6ghz_oper->ccfs1,
  3171. NL80211_BAND_6GHZ);
  3172. }
  3173. } else {
  3174. eht_phy_cap = eht_cap->eht_cap_elem.phy_cap_info[0];
  3175. support_320 =
  3176. eht_phy_cap & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ;
  3177. ieee80211_chandef_eht_oper(eht_oper, support_160,
  3178. support_320, &he_chandef);
  3179. }
  3180. if (!cfg80211_chandef_valid(&he_chandef)) {
  3181. sdata_info(sdata,
  3182. "HE 6GHz operation resulted in invalid chandef: %d MHz/%d/%d MHz/%d MHz\n",
  3183. he_chandef.chan ? he_chandef.chan->center_freq : 0,
  3184. he_chandef.width,
  3185. he_chandef.center_freq1,
  3186. he_chandef.center_freq2);
  3187. return false;
  3188. }
  3189. *chandef = he_chandef;
  3190. return true;
  3191. }
  3192. bool ieee80211_chandef_s1g_oper(const struct ieee80211_s1g_oper_ie *oper,
  3193. struct cfg80211_chan_def *chandef)
  3194. {
  3195. u32 oper_freq;
  3196. if (!oper)
  3197. return false;
  3198. switch (FIELD_GET(S1G_OPER_CH_WIDTH_OPER, oper->ch_width)) {
  3199. case IEEE80211_S1G_CHANWIDTH_1MHZ:
  3200. chandef->width = NL80211_CHAN_WIDTH_1;
  3201. break;
  3202. case IEEE80211_S1G_CHANWIDTH_2MHZ:
  3203. chandef->width = NL80211_CHAN_WIDTH_2;
  3204. break;
  3205. case IEEE80211_S1G_CHANWIDTH_4MHZ:
  3206. chandef->width = NL80211_CHAN_WIDTH_4;
  3207. break;
  3208. case IEEE80211_S1G_CHANWIDTH_8MHZ:
  3209. chandef->width = NL80211_CHAN_WIDTH_8;
  3210. break;
  3211. case IEEE80211_S1G_CHANWIDTH_16MHZ:
  3212. chandef->width = NL80211_CHAN_WIDTH_16;
  3213. break;
  3214. default:
  3215. return false;
  3216. }
  3217. oper_freq = ieee80211_channel_to_freq_khz(oper->oper_ch,
  3218. NL80211_BAND_S1GHZ);
  3219. chandef->center_freq1 = KHZ_TO_MHZ(oper_freq);
  3220. chandef->freq1_offset = oper_freq % 1000;
  3221. return true;
  3222. }
  3223. int ieee80211_parse_bitrates(enum nl80211_chan_width width,
  3224. const struct ieee80211_supported_band *sband,
  3225. const u8 *srates, int srates_len, u32 *rates)
  3226. {
  3227. u32 rate_flags = ieee80211_chanwidth_rate_flags(width);
  3228. int shift = ieee80211_chanwidth_get_shift(width);
  3229. struct ieee80211_rate *br;
  3230. int brate, rate, i, j, count = 0;
  3231. *rates = 0;
  3232. for (i = 0; i < srates_len; i++) {
  3233. rate = srates[i] & 0x7f;
  3234. for (j = 0; j < sband->n_bitrates; j++) {
  3235. br = &sband->bitrates[j];
  3236. if ((rate_flags & br->flags) != rate_flags)
  3237. continue;
  3238. brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
  3239. if (brate == rate) {
  3240. *rates |= BIT(j);
  3241. count++;
  3242. break;
  3243. }
  3244. }
  3245. }
  3246. return count;
  3247. }
  3248. int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
  3249. struct sk_buff *skb, bool need_basic,
  3250. enum nl80211_band band)
  3251. {
  3252. struct ieee80211_local *local = sdata->local;
  3253. struct ieee80211_supported_band *sband;
  3254. int rate, shift;
  3255. u8 i, rates, *pos;
  3256. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  3257. u32 rate_flags;
  3258. shift = ieee80211_vif_get_shift(&sdata->vif);
  3259. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  3260. sband = local->hw.wiphy->bands[band];
  3261. rates = 0;
  3262. for (i = 0; i < sband->n_bitrates; i++) {
  3263. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  3264. continue;
  3265. rates++;
  3266. }
  3267. if (rates > 8)
  3268. rates = 8;
  3269. if (skb_tailroom(skb) < rates + 2)
  3270. return -ENOMEM;
  3271. pos = skb_put(skb, rates + 2);
  3272. *pos++ = WLAN_EID_SUPP_RATES;
  3273. *pos++ = rates;
  3274. for (i = 0; i < rates; i++) {
  3275. u8 basic = 0;
  3276. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  3277. continue;
  3278. if (need_basic && basic_rates & BIT(i))
  3279. basic = 0x80;
  3280. rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  3281. 5 * (1 << shift));
  3282. *pos++ = basic | (u8) rate;
  3283. }
  3284. return 0;
  3285. }
  3286. int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
  3287. struct sk_buff *skb, bool need_basic,
  3288. enum nl80211_band band)
  3289. {
  3290. struct ieee80211_local *local = sdata->local;
  3291. struct ieee80211_supported_band *sband;
  3292. int rate, shift;
  3293. u8 i, exrates, *pos;
  3294. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  3295. u32 rate_flags;
  3296. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  3297. shift = ieee80211_vif_get_shift(&sdata->vif);
  3298. sband = local->hw.wiphy->bands[band];
  3299. exrates = 0;
  3300. for (i = 0; i < sband->n_bitrates; i++) {
  3301. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  3302. continue;
  3303. exrates++;
  3304. }
  3305. if (exrates > 8)
  3306. exrates -= 8;
  3307. else
  3308. exrates = 0;
  3309. if (skb_tailroom(skb) < exrates + 2)
  3310. return -ENOMEM;
  3311. if (exrates) {
  3312. pos = skb_put(skb, exrates + 2);
  3313. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  3314. *pos++ = exrates;
  3315. for (i = 8; i < sband->n_bitrates; i++) {
  3316. u8 basic = 0;
  3317. if ((rate_flags & sband->bitrates[i].flags)
  3318. != rate_flags)
  3319. continue;
  3320. if (need_basic && basic_rates & BIT(i))
  3321. basic = 0x80;
  3322. rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  3323. 5 * (1 << shift));
  3324. *pos++ = basic | (u8) rate;
  3325. }
  3326. }
  3327. return 0;
  3328. }
  3329. int ieee80211_ave_rssi(struct ieee80211_vif *vif)
  3330. {
  3331. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  3332. if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION))
  3333. return 0;
  3334. return -ewma_beacon_signal_read(&sdata->deflink.u.mgd.ave_beacon_signal);
  3335. }
  3336. EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
  3337. u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
  3338. {
  3339. if (!mcs)
  3340. return 1;
  3341. /* TODO: consider rx_highest */
  3342. if (mcs->rx_mask[3])
  3343. return 4;
  3344. if (mcs->rx_mask[2])
  3345. return 3;
  3346. if (mcs->rx_mask[1])
  3347. return 2;
  3348. return 1;
  3349. }
  3350. /**
  3351. * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
  3352. * @local: mac80211 hw info struct
  3353. * @status: RX status
  3354. * @mpdu_len: total MPDU length (including FCS)
  3355. * @mpdu_offset: offset into MPDU to calculate timestamp at
  3356. *
  3357. * This function calculates the RX timestamp at the given MPDU offset, taking
  3358. * into account what the RX timestamp was. An offset of 0 will just normalize
  3359. * the timestamp to TSF at beginning of MPDU reception.
  3360. */
  3361. u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
  3362. struct ieee80211_rx_status *status,
  3363. unsigned int mpdu_len,
  3364. unsigned int mpdu_offset)
  3365. {
  3366. u64 ts = status->mactime;
  3367. struct rate_info ri;
  3368. u16 rate;
  3369. u8 n_ltf;
  3370. if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
  3371. return 0;
  3372. memset(&ri, 0, sizeof(ri));
  3373. ri.bw = status->bw;
  3374. /* Fill cfg80211 rate info */
  3375. switch (status->encoding) {
  3376. case RX_ENC_HE:
  3377. ri.flags |= RATE_INFO_FLAGS_HE_MCS;
  3378. ri.mcs = status->rate_idx;
  3379. ri.nss = status->nss;
  3380. ri.he_ru_alloc = status->he_ru;
  3381. if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
  3382. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  3383. /*
  3384. * See P802.11ax_D6.0, section 27.3.4 for
  3385. * VHT PPDU format.
  3386. */
  3387. if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
  3388. mpdu_offset += 2;
  3389. ts += 36;
  3390. /*
  3391. * TODO:
  3392. * For HE MU PPDU, add the HE-SIG-B.
  3393. * For HE ER PPDU, add 8us for the HE-SIG-A.
  3394. * For HE TB PPDU, add 4us for the HE-STF.
  3395. * Add the HE-LTF durations - variable.
  3396. */
  3397. }
  3398. break;
  3399. case RX_ENC_HT:
  3400. ri.mcs = status->rate_idx;
  3401. ri.flags |= RATE_INFO_FLAGS_MCS;
  3402. if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
  3403. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  3404. /*
  3405. * See P802.11REVmd_D3.0, section 19.3.2 for
  3406. * HT PPDU format.
  3407. */
  3408. if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
  3409. mpdu_offset += 2;
  3410. if (status->enc_flags & RX_ENC_FLAG_HT_GF)
  3411. ts += 24;
  3412. else
  3413. ts += 32;
  3414. /*
  3415. * Add Data HT-LTFs per streams
  3416. * TODO: add Extension HT-LTFs, 4us per LTF
  3417. */
  3418. n_ltf = ((ri.mcs >> 3) & 3) + 1;
  3419. n_ltf = n_ltf == 3 ? 4 : n_ltf;
  3420. ts += n_ltf * 4;
  3421. }
  3422. break;
  3423. case RX_ENC_VHT:
  3424. ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
  3425. ri.mcs = status->rate_idx;
  3426. ri.nss = status->nss;
  3427. if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
  3428. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  3429. /*
  3430. * See P802.11REVmd_D3.0, section 21.3.2 for
  3431. * VHT PPDU format.
  3432. */
  3433. if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
  3434. mpdu_offset += 2;
  3435. ts += 36;
  3436. /*
  3437. * Add VHT-LTFs per streams
  3438. */
  3439. n_ltf = (ri.nss != 1) && (ri.nss % 2) ?
  3440. ri.nss + 1 : ri.nss;
  3441. ts += 4 * n_ltf;
  3442. }
  3443. break;
  3444. default:
  3445. WARN_ON(1);
  3446. fallthrough;
  3447. case RX_ENC_LEGACY: {
  3448. struct ieee80211_supported_band *sband;
  3449. int shift = 0;
  3450. int bitrate;
  3451. switch (status->bw) {
  3452. case RATE_INFO_BW_10:
  3453. shift = 1;
  3454. break;
  3455. case RATE_INFO_BW_5:
  3456. shift = 2;
  3457. break;
  3458. }
  3459. sband = local->hw.wiphy->bands[status->band];
  3460. bitrate = sband->bitrates[status->rate_idx].bitrate;
  3461. ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
  3462. if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
  3463. if (status->band == NL80211_BAND_5GHZ) {
  3464. ts += 20 << shift;
  3465. mpdu_offset += 2;
  3466. } else if (status->enc_flags & RX_ENC_FLAG_SHORTPRE) {
  3467. ts += 96;
  3468. } else {
  3469. ts += 192;
  3470. }
  3471. }
  3472. break;
  3473. }
  3474. }
  3475. rate = cfg80211_calculate_bitrate(&ri);
  3476. if (WARN_ONCE(!rate,
  3477. "Invalid bitrate: flags=0x%llx, idx=%d, vht_nss=%d\n",
  3478. (unsigned long long)status->flag, status->rate_idx,
  3479. status->nss))
  3480. return 0;
  3481. /* rewind from end of MPDU */
  3482. if (status->flag & RX_FLAG_MACTIME_END)
  3483. ts -= mpdu_len * 8 * 10 / rate;
  3484. ts += mpdu_offset * 8 * 10 / rate;
  3485. return ts;
  3486. }
  3487. void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
  3488. {
  3489. struct ieee80211_sub_if_data *sdata;
  3490. struct cfg80211_chan_def chandef;
  3491. /* for interface list, to avoid linking iflist_mtx and chanctx_mtx */
  3492. lockdep_assert_wiphy(local->hw.wiphy);
  3493. mutex_lock(&local->mtx);
  3494. list_for_each_entry(sdata, &local->interfaces, list) {
  3495. /* it might be waiting for the local->mtx, but then
  3496. * by the time it gets it, sdata->wdev.cac_started
  3497. * will no longer be true
  3498. */
  3499. cancel_delayed_work(&sdata->deflink.dfs_cac_timer_work);
  3500. if (sdata->wdev.cac_started) {
  3501. chandef = sdata->vif.bss_conf.chandef;
  3502. ieee80211_link_release_channel(&sdata->deflink);
  3503. cfg80211_cac_event(sdata->dev,
  3504. &chandef,
  3505. NL80211_RADAR_CAC_ABORTED,
  3506. GFP_KERNEL);
  3507. }
  3508. }
  3509. mutex_unlock(&local->mtx);
  3510. }
  3511. void ieee80211_dfs_radar_detected_work(struct wiphy *wiphy,
  3512. struct wiphy_work *work)
  3513. {
  3514. struct ieee80211_local *local =
  3515. container_of(work, struct ieee80211_local, radar_detected_work);
  3516. struct cfg80211_chan_def chandef = local->hw.conf.chandef;
  3517. struct ieee80211_chanctx *ctx;
  3518. int num_chanctx = 0;
  3519. mutex_lock(&local->chanctx_mtx);
  3520. list_for_each_entry(ctx, &local->chanctx_list, list) {
  3521. if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER)
  3522. continue;
  3523. num_chanctx++;
  3524. chandef = ctx->conf.def;
  3525. }
  3526. mutex_unlock(&local->chanctx_mtx);
  3527. ieee80211_dfs_cac_cancel(local);
  3528. if (num_chanctx > 1)
  3529. /* XXX: multi-channel is not supported yet */
  3530. WARN_ON(1);
  3531. else
  3532. cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
  3533. }
  3534. void ieee80211_radar_detected(struct ieee80211_hw *hw)
  3535. {
  3536. struct ieee80211_local *local = hw_to_local(hw);
  3537. trace_api_radar_detected(local);
  3538. wiphy_work_queue(hw->wiphy, &local->radar_detected_work);
  3539. }
  3540. EXPORT_SYMBOL(ieee80211_radar_detected);
  3541. ieee80211_conn_flags_t ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
  3542. {
  3543. ieee80211_conn_flags_t ret;
  3544. int tmp;
  3545. switch (c->width) {
  3546. case NL80211_CHAN_WIDTH_20:
  3547. c->width = NL80211_CHAN_WIDTH_20_NOHT;
  3548. ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
  3549. break;
  3550. case NL80211_CHAN_WIDTH_40:
  3551. c->width = NL80211_CHAN_WIDTH_20;
  3552. c->center_freq1 = c->chan->center_freq;
  3553. ret = IEEE80211_CONN_DISABLE_40MHZ |
  3554. IEEE80211_CONN_DISABLE_VHT;
  3555. break;
  3556. case NL80211_CHAN_WIDTH_80:
  3557. tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
  3558. /* n_P40 */
  3559. tmp /= 2;
  3560. /* freq_P40 */
  3561. c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
  3562. c->width = NL80211_CHAN_WIDTH_40;
  3563. ret = IEEE80211_CONN_DISABLE_VHT;
  3564. break;
  3565. case NL80211_CHAN_WIDTH_80P80:
  3566. c->center_freq2 = 0;
  3567. c->width = NL80211_CHAN_WIDTH_80;
  3568. ret = IEEE80211_CONN_DISABLE_80P80MHZ |
  3569. IEEE80211_CONN_DISABLE_160MHZ;
  3570. break;
  3571. case NL80211_CHAN_WIDTH_160:
  3572. /* n_P20 */
  3573. tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
  3574. /* n_P80 */
  3575. tmp /= 4;
  3576. c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
  3577. c->width = NL80211_CHAN_WIDTH_80;
  3578. ret = IEEE80211_CONN_DISABLE_80P80MHZ |
  3579. IEEE80211_CONN_DISABLE_160MHZ;
  3580. break;
  3581. case NL80211_CHAN_WIDTH_320:
  3582. /* n_P20 */
  3583. tmp = (150 + c->chan->center_freq - c->center_freq1) / 20;
  3584. /* n_P160 */
  3585. tmp /= 8;
  3586. c->center_freq1 = c->center_freq1 - 80 + 160 * tmp;
  3587. c->width = NL80211_CHAN_WIDTH_160;
  3588. ret = IEEE80211_CONN_DISABLE_320MHZ;
  3589. break;
  3590. default:
  3591. case NL80211_CHAN_WIDTH_20_NOHT:
  3592. WARN_ON_ONCE(1);
  3593. c->width = NL80211_CHAN_WIDTH_20_NOHT;
  3594. ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
  3595. break;
  3596. case NL80211_CHAN_WIDTH_1:
  3597. case NL80211_CHAN_WIDTH_2:
  3598. case NL80211_CHAN_WIDTH_4:
  3599. case NL80211_CHAN_WIDTH_8:
  3600. case NL80211_CHAN_WIDTH_16:
  3601. case NL80211_CHAN_WIDTH_5:
  3602. case NL80211_CHAN_WIDTH_10:
  3603. WARN_ON_ONCE(1);
  3604. /* keep c->width */
  3605. ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
  3606. break;
  3607. }
  3608. WARN_ON_ONCE(!cfg80211_chandef_valid(c));
  3609. return ret;
  3610. }
  3611. /*
  3612. * Returns true if smps_mode_new is strictly more restrictive than
  3613. * smps_mode_old.
  3614. */
  3615. bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
  3616. enum ieee80211_smps_mode smps_mode_new)
  3617. {
  3618. if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
  3619. smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
  3620. return false;
  3621. switch (smps_mode_old) {
  3622. case IEEE80211_SMPS_STATIC:
  3623. return false;
  3624. case IEEE80211_SMPS_DYNAMIC:
  3625. return smps_mode_new == IEEE80211_SMPS_STATIC;
  3626. case IEEE80211_SMPS_OFF:
  3627. return smps_mode_new != IEEE80211_SMPS_OFF;
  3628. default:
  3629. WARN_ON(1);
  3630. }
  3631. return false;
  3632. }
  3633. int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
  3634. struct cfg80211_csa_settings *csa_settings)
  3635. {
  3636. struct sk_buff *skb;
  3637. struct ieee80211_mgmt *mgmt;
  3638. struct ieee80211_local *local = sdata->local;
  3639. int freq;
  3640. int hdr_len = offsetofend(struct ieee80211_mgmt,
  3641. u.action.u.chan_switch);
  3642. u8 *pos;
  3643. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  3644. sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  3645. return -EOPNOTSUPP;
  3646. skb = dev_alloc_skb(local->tx_headroom + hdr_len +
  3647. 5 + /* channel switch announcement element */
  3648. 3 + /* secondary channel offset element */
  3649. 5 + /* wide bandwidth channel switch announcement */
  3650. 8); /* mesh channel switch parameters element */
  3651. if (!skb)
  3652. return -ENOMEM;
  3653. skb_reserve(skb, local->tx_headroom);
  3654. mgmt = skb_put_zero(skb, hdr_len);
  3655. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  3656. IEEE80211_STYPE_ACTION);
  3657. eth_broadcast_addr(mgmt->da);
  3658. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  3659. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  3660. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  3661. } else {
  3662. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  3663. memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
  3664. }
  3665. mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
  3666. mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
  3667. pos = skb_put(skb, 5);
  3668. *pos++ = WLAN_EID_CHANNEL_SWITCH; /* EID */
  3669. *pos++ = 3; /* IE length */
  3670. *pos++ = csa_settings->block_tx ? 1 : 0; /* CSA mode */
  3671. freq = csa_settings->chandef.chan->center_freq;
  3672. *pos++ = ieee80211_frequency_to_channel(freq); /* channel */
  3673. *pos++ = csa_settings->count; /* count */
  3674. if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
  3675. enum nl80211_channel_type ch_type;
  3676. skb_put(skb, 3);
  3677. *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
  3678. *pos++ = 1; /* IE length */
  3679. ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
  3680. if (ch_type == NL80211_CHAN_HT40PLUS)
  3681. *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  3682. else
  3683. *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  3684. }
  3685. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  3686. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  3687. skb_put(skb, 8);
  3688. *pos++ = WLAN_EID_CHAN_SWITCH_PARAM; /* EID */
  3689. *pos++ = 6; /* IE length */
  3690. *pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL; /* Mesh TTL */
  3691. *pos = 0x00; /* Mesh Flag: Tx Restrict, Initiator, Reason */
  3692. *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
  3693. *pos++ |= csa_settings->block_tx ?
  3694. WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
  3695. put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
  3696. pos += 2;
  3697. put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
  3698. pos += 2;
  3699. }
  3700. if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_80 ||
  3701. csa_settings->chandef.width == NL80211_CHAN_WIDTH_80P80 ||
  3702. csa_settings->chandef.width == NL80211_CHAN_WIDTH_160) {
  3703. skb_put(skb, 5);
  3704. ieee80211_ie_build_wide_bw_cs(pos, &csa_settings->chandef);
  3705. }
  3706. ieee80211_tx_skb(sdata, skb);
  3707. return 0;
  3708. }
  3709. static bool
  3710. ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
  3711. {
  3712. s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
  3713. int skip;
  3714. if (end > 0)
  3715. return false;
  3716. /* One shot NOA */
  3717. if (data->count[i] == 1)
  3718. return false;
  3719. if (data->desc[i].interval == 0)
  3720. return false;
  3721. /* End time is in the past, check for repetitions */
  3722. skip = DIV_ROUND_UP(-end, data->desc[i].interval);
  3723. if (data->count[i] < 255) {
  3724. if (data->count[i] <= skip) {
  3725. data->count[i] = 0;
  3726. return false;
  3727. }
  3728. data->count[i] -= skip;
  3729. }
  3730. data->desc[i].start += skip * data->desc[i].interval;
  3731. return true;
  3732. }
  3733. static bool
  3734. ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
  3735. s32 *offset)
  3736. {
  3737. bool ret = false;
  3738. int i;
  3739. for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
  3740. s32 cur;
  3741. if (!data->count[i])
  3742. continue;
  3743. if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
  3744. ret = true;
  3745. cur = data->desc[i].start - tsf;
  3746. if (cur > *offset)
  3747. continue;
  3748. cur = data->desc[i].start + data->desc[i].duration - tsf;
  3749. if (cur > *offset)
  3750. *offset = cur;
  3751. }
  3752. return ret;
  3753. }
  3754. static u32
  3755. ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
  3756. {
  3757. s32 offset = 0;
  3758. int tries = 0;
  3759. /*
  3760. * arbitrary limit, used to avoid infinite loops when combined NoA
  3761. * descriptors cover the full time period.
  3762. */
  3763. int max_tries = 5;
  3764. ieee80211_extend_absent_time(data, tsf, &offset);
  3765. do {
  3766. if (!ieee80211_extend_absent_time(data, tsf, &offset))
  3767. break;
  3768. tries++;
  3769. } while (tries < max_tries);
  3770. return offset;
  3771. }
  3772. void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
  3773. {
  3774. u32 next_offset = BIT(31) - 1;
  3775. int i;
  3776. data->absent = 0;
  3777. data->has_next_tsf = false;
  3778. for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
  3779. s32 start;
  3780. if (!data->count[i])
  3781. continue;
  3782. ieee80211_extend_noa_desc(data, tsf, i);
  3783. start = data->desc[i].start - tsf;
  3784. if (start <= 0)
  3785. data->absent |= BIT(i);
  3786. if (next_offset > start)
  3787. next_offset = start;
  3788. data->has_next_tsf = true;
  3789. }
  3790. if (data->absent)
  3791. next_offset = ieee80211_get_noa_absent_time(data, tsf);
  3792. data->next_tsf = tsf + next_offset;
  3793. }
  3794. EXPORT_SYMBOL(ieee80211_update_p2p_noa);
  3795. int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
  3796. struct ieee80211_noa_data *data, u32 tsf)
  3797. {
  3798. int ret = 0;
  3799. int i;
  3800. memset(data, 0, sizeof(*data));
  3801. for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
  3802. const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
  3803. if (!desc->count || !desc->duration)
  3804. continue;
  3805. data->count[i] = desc->count;
  3806. data->desc[i].start = le32_to_cpu(desc->start_time);
  3807. data->desc[i].duration = le32_to_cpu(desc->duration);
  3808. data->desc[i].interval = le32_to_cpu(desc->interval);
  3809. if (data->count[i] > 1 &&
  3810. data->desc[i].interval < data->desc[i].duration)
  3811. continue;
  3812. ieee80211_extend_noa_desc(data, tsf, i);
  3813. ret++;
  3814. }
  3815. if (ret)
  3816. ieee80211_update_p2p_noa(data, tsf);
  3817. return ret;
  3818. }
  3819. EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
  3820. void ieee80211_recalc_dtim(struct ieee80211_local *local,
  3821. struct ieee80211_sub_if_data *sdata)
  3822. {
  3823. u64 tsf = drv_get_tsf(local, sdata);
  3824. u64 dtim_count = 0;
  3825. u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
  3826. u8 dtim_period = sdata->vif.bss_conf.dtim_period;
  3827. struct ps_data *ps;
  3828. u8 bcns_from_dtim;
  3829. if (tsf == -1ULL || !beacon_int || !dtim_period)
  3830. return;
  3831. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  3832. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  3833. if (!sdata->bss)
  3834. return;
  3835. ps = &sdata->bss->ps;
  3836. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  3837. ps = &sdata->u.mesh.ps;
  3838. } else {
  3839. return;
  3840. }
  3841. /*
  3842. * actually finds last dtim_count, mac80211 will update in
  3843. * __beacon_add_tim().
  3844. * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
  3845. */
  3846. do_div(tsf, beacon_int);
  3847. bcns_from_dtim = do_div(tsf, dtim_period);
  3848. /* just had a DTIM */
  3849. if (!bcns_from_dtim)
  3850. dtim_count = 0;
  3851. else
  3852. dtim_count = dtim_period - bcns_from_dtim;
  3853. ps->dtim_count = dtim_count;
  3854. }
  3855. static u8 ieee80211_chanctx_radar_detect(struct ieee80211_local *local,
  3856. struct ieee80211_chanctx *ctx)
  3857. {
  3858. struct ieee80211_link_data *link;
  3859. u8 radar_detect = 0;
  3860. lockdep_assert_held(&local->chanctx_mtx);
  3861. if (WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED))
  3862. return 0;
  3863. list_for_each_entry(link, &ctx->reserved_links, reserved_chanctx_list)
  3864. if (link->reserved_radar_required)
  3865. radar_detect |= BIT(link->reserved_chandef.width);
  3866. /*
  3867. * An in-place reservation context should not have any assigned vifs
  3868. * until it replaces the other context.
  3869. */
  3870. WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
  3871. !list_empty(&ctx->assigned_links));
  3872. list_for_each_entry(link, &ctx->assigned_links, assigned_chanctx_list) {
  3873. if (!link->radar_required)
  3874. continue;
  3875. radar_detect |=
  3876. BIT(link->conf->chandef.width);
  3877. }
  3878. return radar_detect;
  3879. }
  3880. int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
  3881. const struct cfg80211_chan_def *chandef,
  3882. enum ieee80211_chanctx_mode chanmode,
  3883. u8 radar_detect)
  3884. {
  3885. struct ieee80211_local *local = sdata->local;
  3886. struct ieee80211_sub_if_data *sdata_iter;
  3887. enum nl80211_iftype iftype = sdata->wdev.iftype;
  3888. struct ieee80211_chanctx *ctx;
  3889. int total = 1;
  3890. struct iface_combination_params params = {
  3891. .radar_detect = radar_detect,
  3892. };
  3893. lockdep_assert_held(&local->chanctx_mtx);
  3894. if (WARN_ON(hweight32(radar_detect) > 1))
  3895. return -EINVAL;
  3896. if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
  3897. !chandef->chan))
  3898. return -EINVAL;
  3899. if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
  3900. return -EINVAL;
  3901. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  3902. sdata->vif.type == NL80211_IFTYPE_MESH_POINT) {
  3903. /*
  3904. * always passing this is harmless, since it'll be the
  3905. * same value that cfg80211 finds if it finds the same
  3906. * interface ... and that's always allowed
  3907. */
  3908. params.new_beacon_int = sdata->vif.bss_conf.beacon_int;
  3909. }
  3910. /* Always allow software iftypes */
  3911. if (cfg80211_iftype_allowed(local->hw.wiphy, iftype, 0, 1)) {
  3912. if (radar_detect)
  3913. return -EINVAL;
  3914. return 0;
  3915. }
  3916. if (chandef)
  3917. params.num_different_channels = 1;
  3918. if (iftype != NL80211_IFTYPE_UNSPECIFIED)
  3919. params.iftype_num[iftype] = 1;
  3920. list_for_each_entry(ctx, &local->chanctx_list, list) {
  3921. if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
  3922. continue;
  3923. params.radar_detect |=
  3924. ieee80211_chanctx_radar_detect(local, ctx);
  3925. if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
  3926. params.num_different_channels++;
  3927. continue;
  3928. }
  3929. if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
  3930. cfg80211_chandef_compatible(chandef,
  3931. &ctx->conf.def))
  3932. continue;
  3933. params.num_different_channels++;
  3934. }
  3935. list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
  3936. struct wireless_dev *wdev_iter;
  3937. wdev_iter = &sdata_iter->wdev;
  3938. if (sdata_iter == sdata ||
  3939. !ieee80211_sdata_running(sdata_iter) ||
  3940. cfg80211_iftype_allowed(local->hw.wiphy,
  3941. wdev_iter->iftype, 0, 1))
  3942. continue;
  3943. params.iftype_num[wdev_iter->iftype]++;
  3944. total++;
  3945. }
  3946. if (total == 1 && !params.radar_detect)
  3947. return 0;
  3948. return cfg80211_check_combinations(local->hw.wiphy, &params);
  3949. }
  3950. static void
  3951. ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
  3952. void *data)
  3953. {
  3954. u32 *max_num_different_channels = data;
  3955. *max_num_different_channels = max(*max_num_different_channels,
  3956. c->num_different_channels);
  3957. }
  3958. int ieee80211_max_num_channels(struct ieee80211_local *local)
  3959. {
  3960. struct ieee80211_sub_if_data *sdata;
  3961. struct ieee80211_chanctx *ctx;
  3962. u32 max_num_different_channels = 1;
  3963. int err;
  3964. struct iface_combination_params params = {0};
  3965. lockdep_assert_held(&local->chanctx_mtx);
  3966. list_for_each_entry(ctx, &local->chanctx_list, list) {
  3967. if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
  3968. continue;
  3969. params.num_different_channels++;
  3970. params.radar_detect |=
  3971. ieee80211_chanctx_radar_detect(local, ctx);
  3972. }
  3973. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  3974. params.iftype_num[sdata->wdev.iftype]++;
  3975. err = cfg80211_iter_combinations(local->hw.wiphy, &params,
  3976. ieee80211_iter_max_chans,
  3977. &max_num_different_channels);
  3978. if (err < 0)
  3979. return err;
  3980. return max_num_different_channels;
  3981. }
  3982. void ieee80211_add_s1g_capab_ie(struct ieee80211_sub_if_data *sdata,
  3983. struct ieee80211_sta_s1g_cap *caps,
  3984. struct sk_buff *skb)
  3985. {
  3986. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3987. struct ieee80211_s1g_cap s1g_capab;
  3988. u8 *pos;
  3989. int i;
  3990. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  3991. return;
  3992. if (!caps->s1g)
  3993. return;
  3994. memcpy(s1g_capab.capab_info, caps->cap, sizeof(caps->cap));
  3995. memcpy(s1g_capab.supp_mcs_nss, caps->nss_mcs, sizeof(caps->nss_mcs));
  3996. /* override the capability info */
  3997. for (i = 0; i < sizeof(ifmgd->s1g_capa.capab_info); i++) {
  3998. u8 mask = ifmgd->s1g_capa_mask.capab_info[i];
  3999. s1g_capab.capab_info[i] &= ~mask;
  4000. s1g_capab.capab_info[i] |= ifmgd->s1g_capa.capab_info[i] & mask;
  4001. }
  4002. /* then MCS and NSS set */
  4003. for (i = 0; i < sizeof(ifmgd->s1g_capa.supp_mcs_nss); i++) {
  4004. u8 mask = ifmgd->s1g_capa_mask.supp_mcs_nss[i];
  4005. s1g_capab.supp_mcs_nss[i] &= ~mask;
  4006. s1g_capab.supp_mcs_nss[i] |=
  4007. ifmgd->s1g_capa.supp_mcs_nss[i] & mask;
  4008. }
  4009. pos = skb_put(skb, 2 + sizeof(s1g_capab));
  4010. *pos++ = WLAN_EID_S1G_CAPABILITIES;
  4011. *pos++ = sizeof(s1g_capab);
  4012. memcpy(pos, &s1g_capab, sizeof(s1g_capab));
  4013. }
  4014. void ieee80211_add_aid_request_ie(struct ieee80211_sub_if_data *sdata,
  4015. struct sk_buff *skb)
  4016. {
  4017. u8 *pos = skb_put(skb, 3);
  4018. *pos++ = WLAN_EID_AID_REQUEST;
  4019. *pos++ = 1;
  4020. *pos++ = 0;
  4021. }
  4022. u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
  4023. {
  4024. *buf++ = WLAN_EID_VENDOR_SPECIFIC;
  4025. *buf++ = 7; /* len */
  4026. *buf++ = 0x00; /* Microsoft OUI 00:50:F2 */
  4027. *buf++ = 0x50;
  4028. *buf++ = 0xf2;
  4029. *buf++ = 2; /* WME */
  4030. *buf++ = 0; /* WME info */
  4031. *buf++ = 1; /* WME ver */
  4032. *buf++ = qosinfo; /* U-APSD no in use */
  4033. return buf;
  4034. }
  4035. void ieee80211_txq_get_depth(struct ieee80211_txq *txq,
  4036. unsigned long *frame_cnt,
  4037. unsigned long *byte_cnt)
  4038. {
  4039. struct txq_info *txqi = to_txq_info(txq);
  4040. u32 frag_cnt = 0, frag_bytes = 0;
  4041. struct sk_buff *skb;
  4042. skb_queue_walk(&txqi->frags, skb) {
  4043. frag_cnt++;
  4044. frag_bytes += skb->len;
  4045. }
  4046. if (frame_cnt)
  4047. *frame_cnt = txqi->tin.backlog_packets + frag_cnt;
  4048. if (byte_cnt)
  4049. *byte_cnt = txqi->tin.backlog_bytes + frag_bytes;
  4050. }
  4051. EXPORT_SYMBOL(ieee80211_txq_get_depth);
  4052. const u8 ieee80211_ac_to_qos_mask[IEEE80211_NUM_ACS] = {
  4053. IEEE80211_WMM_IE_STA_QOSINFO_AC_VO,
  4054. IEEE80211_WMM_IE_STA_QOSINFO_AC_VI,
  4055. IEEE80211_WMM_IE_STA_QOSINFO_AC_BE,
  4056. IEEE80211_WMM_IE_STA_QOSINFO_AC_BK
  4057. };
  4058. u16 ieee80211_encode_usf(int listen_interval)
  4059. {
  4060. static const int listen_int_usf[] = { 1, 10, 1000, 10000 };
  4061. u16 ui, usf = 0;
  4062. /* find greatest USF */
  4063. while (usf < IEEE80211_MAX_USF) {
  4064. if (listen_interval % listen_int_usf[usf + 1])
  4065. break;
  4066. usf += 1;
  4067. }
  4068. ui = listen_interval / listen_int_usf[usf];
  4069. /* error if there is a remainder. Should've been checked by user */
  4070. WARN_ON_ONCE(ui > IEEE80211_MAX_UI);
  4071. listen_interval = FIELD_PREP(LISTEN_INT_USF, usf) |
  4072. FIELD_PREP(LISTEN_INT_UI, ui);
  4073. return (u16) listen_interval;
  4074. }
  4075. u8 ieee80211_ie_len_eht_cap(struct ieee80211_sub_if_data *sdata, u8 iftype)
  4076. {
  4077. const struct ieee80211_sta_he_cap *he_cap;
  4078. const struct ieee80211_sta_eht_cap *eht_cap;
  4079. struct ieee80211_supported_band *sband;
  4080. bool is_ap;
  4081. u8 n;
  4082. sband = ieee80211_get_sband(sdata);
  4083. if (!sband)
  4084. return 0;
  4085. he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
  4086. eht_cap = ieee80211_get_eht_iftype_cap(sband, iftype);
  4087. if (!he_cap || !eht_cap)
  4088. return 0;
  4089. is_ap = iftype == NL80211_IFTYPE_AP ||
  4090. iftype == NL80211_IFTYPE_P2P_GO;
  4091. n = ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
  4092. &eht_cap->eht_cap_elem,
  4093. is_ap);
  4094. return 2 + 1 +
  4095. sizeof(eht_cap->eht_cap_elem) + n +
  4096. ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0],
  4097. eht_cap->eht_cap_elem.phy_cap_info);
  4098. return 0;
  4099. }
  4100. u8 *ieee80211_ie_build_eht_cap(u8 *pos,
  4101. const struct ieee80211_sta_he_cap *he_cap,
  4102. const struct ieee80211_sta_eht_cap *eht_cap,
  4103. u8 *end,
  4104. bool for_ap)
  4105. {
  4106. u8 mcs_nss_len, ppet_len;
  4107. u8 ie_len;
  4108. u8 *orig_pos = pos;
  4109. /* Make sure we have place for the IE */
  4110. if (!he_cap || !eht_cap)
  4111. return orig_pos;
  4112. mcs_nss_len = ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
  4113. &eht_cap->eht_cap_elem,
  4114. for_ap);
  4115. ppet_len = ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0],
  4116. eht_cap->eht_cap_elem.phy_cap_info);
  4117. ie_len = 2 + 1 + sizeof(eht_cap->eht_cap_elem) + mcs_nss_len + ppet_len;
  4118. if ((end - pos) < ie_len)
  4119. return orig_pos;
  4120. *pos++ = WLAN_EID_EXTENSION;
  4121. *pos++ = ie_len - 2;
  4122. *pos++ = WLAN_EID_EXT_EHT_CAPABILITY;
  4123. /* Fixed data */
  4124. memcpy(pos, &eht_cap->eht_cap_elem, sizeof(eht_cap->eht_cap_elem));
  4125. pos += sizeof(eht_cap->eht_cap_elem);
  4126. memcpy(pos, &eht_cap->eht_mcs_nss_supp, mcs_nss_len);
  4127. pos += mcs_nss_len;
  4128. if (ppet_len) {
  4129. memcpy(pos, &eht_cap->eht_ppe_thres, ppet_len);
  4130. pos += ppet_len;
  4131. }
  4132. return pos;
  4133. }
  4134. void ieee80211_fragment_element(struct sk_buff *skb, u8 *len_pos)
  4135. {
  4136. unsigned int elem_len;
  4137. if (!len_pos)
  4138. return;
  4139. elem_len = skb->data + skb->len - len_pos - 1;
  4140. while (elem_len > 255) {
  4141. /* this one is 255 */
  4142. *len_pos = 255;
  4143. /* remaining data gets smaller */
  4144. elem_len -= 255;
  4145. /* make space for the fragment ID/len in SKB */
  4146. skb_put(skb, 2);
  4147. /* shift back the remaining data to place fragment ID/len */
  4148. memmove(len_pos + 255 + 3, len_pos + 255 + 1, elem_len);
  4149. /* place the fragment ID */
  4150. len_pos += 255 + 1;
  4151. *len_pos = WLAN_EID_FRAGMENT;
  4152. /* and point to fragment length to update later */
  4153. len_pos++;
  4154. }
  4155. *len_pos = elem_len;
  4156. }