util.c 122 KB

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