tx.c 167 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278
  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) 2018-2024 Intel Corporation
  9. *
  10. * Transmit and frame generation functions.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/if_vlan.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/bitmap.h>
  18. #include <linux/rcupdate.h>
  19. #include <linux/export.h>
  20. #include <net/net_namespace.h>
  21. #include <net/ieee80211_radiotap.h>
  22. #include <net/cfg80211.h>
  23. #include <net/mac80211.h>
  24. #include <net/codel.h>
  25. #include <net/codel_impl.h>
  26. #include <linux/unaligned.h>
  27. #include <net/fq_impl.h>
  28. #include <net/gso.h>
  29. #include "ieee80211_i.h"
  30. #include "driver-ops.h"
  31. #include "led.h"
  32. #include "mesh.h"
  33. #include "wep.h"
  34. #include "wpa.h"
  35. #include "wme.h"
  36. #include "rate.h"
  37. /* misc utils */
  38. static __le16 ieee80211_duration(struct ieee80211_tx_data *tx,
  39. struct sk_buff *skb, int group_addr,
  40. int next_frag_len)
  41. {
  42. int rate, mrate, erp, dur, i;
  43. struct ieee80211_rate *txrate;
  44. struct ieee80211_local *local = tx->local;
  45. struct ieee80211_supported_band *sband;
  46. struct ieee80211_hdr *hdr;
  47. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  48. struct ieee80211_chanctx_conf *chanctx_conf;
  49. u32 rate_flags = 0;
  50. /* assume HW handles this */
  51. if (tx->rate.flags & (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))
  52. return 0;
  53. rcu_read_lock();
  54. chanctx_conf = rcu_dereference(tx->sdata->vif.bss_conf.chanctx_conf);
  55. if (chanctx_conf)
  56. rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
  57. rcu_read_unlock();
  58. /* uh huh? */
  59. if (WARN_ON_ONCE(tx->rate.idx < 0))
  60. return 0;
  61. sband = local->hw.wiphy->bands[info->band];
  62. txrate = &sband->bitrates[tx->rate.idx];
  63. erp = txrate->flags & IEEE80211_RATE_ERP_G;
  64. /* device is expected to do this */
  65. if (sband->band == NL80211_BAND_S1GHZ)
  66. return 0;
  67. /*
  68. * data and mgmt (except PS Poll):
  69. * - during CFP: 32768
  70. * - during contention period:
  71. * if addr1 is group address: 0
  72. * if more fragments = 0 and addr1 is individual address: time to
  73. * transmit one ACK plus SIFS
  74. * if more fragments = 1 and addr1 is individual address: time to
  75. * transmit next fragment plus 2 x ACK plus 3 x SIFS
  76. *
  77. * IEEE 802.11, 9.6:
  78. * - control response frame (CTS or ACK) shall be transmitted using the
  79. * same rate as the immediately previous frame in the frame exchange
  80. * sequence, if this rate belongs to the PHY mandatory rates, or else
  81. * at the highest possible rate belonging to the PHY rates in the
  82. * BSSBasicRateSet
  83. */
  84. hdr = (struct ieee80211_hdr *)skb->data;
  85. if (ieee80211_is_ctl(hdr->frame_control)) {
  86. /* TODO: These control frames are not currently sent by
  87. * mac80211, but should they be implemented, this function
  88. * needs to be updated to support duration field calculation.
  89. *
  90. * RTS: time needed to transmit pending data/mgmt frame plus
  91. * one CTS frame plus one ACK frame plus 3 x SIFS
  92. * CTS: duration of immediately previous RTS minus time
  93. * required to transmit CTS and its SIFS
  94. * ACK: 0 if immediately previous directed data/mgmt had
  95. * more=0, with more=1 duration in ACK frame is duration
  96. * from previous frame minus time needed to transmit ACK
  97. * and its SIFS
  98. * PS Poll: BIT(15) | BIT(14) | aid
  99. */
  100. return 0;
  101. }
  102. /* data/mgmt */
  103. if (0 /* FIX: data/mgmt during CFP */)
  104. return cpu_to_le16(32768);
  105. if (group_addr) /* Group address as the destination - no ACK */
  106. return 0;
  107. /* Individual destination address:
  108. * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
  109. * CTS and ACK frames shall be transmitted using the highest rate in
  110. * basic rate set that is less than or equal to the rate of the
  111. * immediately previous frame and that is using the same modulation
  112. * (CCK or OFDM). If no basic rate set matches with these requirements,
  113. * the highest mandatory rate of the PHY that is less than or equal to
  114. * the rate of the previous frame is used.
  115. * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
  116. */
  117. rate = -1;
  118. /* use lowest available if everything fails */
  119. mrate = sband->bitrates[0].bitrate;
  120. for (i = 0; i < sband->n_bitrates; i++) {
  121. struct ieee80211_rate *r = &sband->bitrates[i];
  122. u32 flag;
  123. if (r->bitrate > txrate->bitrate)
  124. break;
  125. if ((rate_flags & r->flags) != rate_flags)
  126. continue;
  127. if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
  128. rate = r->bitrate;
  129. switch (sband->band) {
  130. case NL80211_BAND_2GHZ:
  131. case NL80211_BAND_LC:
  132. if (tx->sdata->deflink.operating_11g_mode)
  133. flag = IEEE80211_RATE_MANDATORY_G;
  134. else
  135. flag = IEEE80211_RATE_MANDATORY_B;
  136. break;
  137. case NL80211_BAND_5GHZ:
  138. case NL80211_BAND_6GHZ:
  139. flag = IEEE80211_RATE_MANDATORY_A;
  140. break;
  141. default:
  142. flag = 0;
  143. WARN_ON(1);
  144. break;
  145. }
  146. if (r->flags & flag)
  147. mrate = r->bitrate;
  148. }
  149. if (rate == -1) {
  150. /* No matching basic rate found; use highest suitable mandatory
  151. * PHY rate */
  152. rate = mrate;
  153. }
  154. /* Don't calculate ACKs for QoS Frames with NoAck Policy set */
  155. if (ieee80211_is_data_qos(hdr->frame_control) &&
  156. *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
  157. dur = 0;
  158. else
  159. /* Time needed to transmit ACK
  160. * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
  161. * to closest integer */
  162. dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
  163. tx->sdata->vif.bss_conf.use_short_preamble);
  164. if (next_frag_len) {
  165. /* Frame is fragmented: duration increases with time needed to
  166. * transmit next fragment plus ACK and 2 x SIFS. */
  167. dur *= 2; /* ACK + SIFS */
  168. /* next fragment */
  169. dur += ieee80211_frame_duration(sband->band, next_frag_len,
  170. txrate->bitrate, erp,
  171. tx->sdata->vif.bss_conf.use_short_preamble);
  172. }
  173. return cpu_to_le16(dur);
  174. }
  175. /* tx handlers */
  176. static ieee80211_tx_result debug_noinline
  177. ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
  178. {
  179. struct ieee80211_local *local = tx->local;
  180. struct ieee80211_if_managed *ifmgd;
  181. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  182. /* driver doesn't support power save */
  183. if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS))
  184. return TX_CONTINUE;
  185. /* hardware does dynamic power save */
  186. if (ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
  187. return TX_CONTINUE;
  188. /* dynamic power save disabled */
  189. if (local->hw.conf.dynamic_ps_timeout <= 0)
  190. return TX_CONTINUE;
  191. /* we are scanning, don't enable power save */
  192. if (local->scanning)
  193. return TX_CONTINUE;
  194. if (!local->ps_sdata)
  195. return TX_CONTINUE;
  196. /* No point if we're going to suspend */
  197. if (local->quiescing)
  198. return TX_CONTINUE;
  199. /* dynamic ps is supported only in managed mode */
  200. if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
  201. return TX_CONTINUE;
  202. if (unlikely(info->flags & IEEE80211_TX_INTFL_OFFCHAN_TX_OK))
  203. return TX_CONTINUE;
  204. ifmgd = &tx->sdata->u.mgd;
  205. /*
  206. * Don't wakeup from power save if u-apsd is enabled, voip ac has
  207. * u-apsd enabled and the frame is in voip class. This effectively
  208. * means that even if all access categories have u-apsd enabled, in
  209. * practise u-apsd is only used with the voip ac. This is a
  210. * workaround for the case when received voip class packets do not
  211. * have correct qos tag for some reason, due the network or the
  212. * peer application.
  213. *
  214. * Note: ifmgd->uapsd_queues access is racy here. If the value is
  215. * changed via debugfs, user needs to reassociate manually to have
  216. * everything in sync.
  217. */
  218. if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
  219. (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
  220. skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
  221. return TX_CONTINUE;
  222. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  223. ieee80211_stop_queues_by_reason(&local->hw,
  224. IEEE80211_MAX_QUEUE_MAP,
  225. IEEE80211_QUEUE_STOP_REASON_PS,
  226. false);
  227. ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
  228. wiphy_work_queue(local->hw.wiphy,
  229. &local->dynamic_ps_disable_work);
  230. }
  231. /* Don't restart the timer if we're not disassociated */
  232. if (!ifmgd->associated)
  233. return TX_CONTINUE;
  234. mod_timer(&local->dynamic_ps_timer, jiffies +
  235. msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
  236. return TX_CONTINUE;
  237. }
  238. static ieee80211_tx_result debug_noinline
  239. ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
  240. {
  241. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  242. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  243. bool assoc = false;
  244. if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
  245. return TX_CONTINUE;
  246. if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
  247. test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
  248. !ieee80211_is_probe_req(hdr->frame_control) &&
  249. !ieee80211_is_any_nullfunc(hdr->frame_control))
  250. /*
  251. * When software scanning only nullfunc frames (to notify
  252. * the sleep state to the AP) and probe requests (for the
  253. * active scan) are allowed, all other frames should not be
  254. * sent and we should not get here, but if we do
  255. * nonetheless, drop them to avoid sending them
  256. * off-channel. See the link below and
  257. * ieee80211_start_scan() for more.
  258. *
  259. * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
  260. */
  261. return TX_DROP;
  262. if (tx->sdata->vif.type == NL80211_IFTYPE_OCB)
  263. return TX_CONTINUE;
  264. if (tx->flags & IEEE80211_TX_PS_BUFFERED)
  265. return TX_CONTINUE;
  266. if (tx->sta)
  267. assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
  268. if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
  269. if (unlikely(!assoc &&
  270. ieee80211_is_data(hdr->frame_control))) {
  271. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  272. sdata_info(tx->sdata,
  273. "dropped data frame to not associated station %pM\n",
  274. hdr->addr1);
  275. #endif
  276. I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
  277. return TX_DROP;
  278. }
  279. } else if (unlikely(ieee80211_is_data(hdr->frame_control) &&
  280. ieee80211_vif_get_num_mcast_if(tx->sdata) == 0)) {
  281. /*
  282. * No associated STAs - no need to send multicast
  283. * frames.
  284. */
  285. return TX_DROP;
  286. }
  287. return TX_CONTINUE;
  288. }
  289. /* This function is called whenever the AP is about to exceed the maximum limit
  290. * of buffered frames for power saving STAs. This situation should not really
  291. * happen often during normal operation, so dropping the oldest buffered packet
  292. * from each queue should be OK to make some room for new frames. */
  293. static void purge_old_ps_buffers(struct ieee80211_local *local)
  294. {
  295. int total = 0, purged = 0;
  296. struct sk_buff *skb;
  297. struct ieee80211_sub_if_data *sdata;
  298. struct sta_info *sta;
  299. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  300. struct ps_data *ps;
  301. if (sdata->vif.type == NL80211_IFTYPE_AP)
  302. ps = &sdata->u.ap.ps;
  303. else if (ieee80211_vif_is_mesh(&sdata->vif))
  304. ps = &sdata->u.mesh.ps;
  305. else
  306. continue;
  307. skb = skb_dequeue(&ps->bc_buf);
  308. if (skb) {
  309. purged++;
  310. ieee80211_free_txskb(&local->hw, skb);
  311. }
  312. total += skb_queue_len(&ps->bc_buf);
  313. }
  314. /*
  315. * Drop one frame from each station from the lowest-priority
  316. * AC that has frames at all.
  317. */
  318. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  319. int ac;
  320. for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
  321. skb = skb_dequeue(&sta->ps_tx_buf[ac]);
  322. total += skb_queue_len(&sta->ps_tx_buf[ac]);
  323. if (skb) {
  324. purged++;
  325. ieee80211_free_txskb(&local->hw, skb);
  326. break;
  327. }
  328. }
  329. }
  330. local->total_ps_buffered = total;
  331. ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
  332. }
  333. static ieee80211_tx_result
  334. ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
  335. {
  336. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  337. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  338. struct ps_data *ps;
  339. /*
  340. * broadcast/multicast frame
  341. *
  342. * If any of the associated/peer stations is in power save mode,
  343. * the frame is buffered to be sent after DTIM beacon frame.
  344. * This is done either by the hardware or us.
  345. */
  346. /* powersaving STAs currently only in AP/VLAN/mesh mode */
  347. if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
  348. tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  349. if (!tx->sdata->bss)
  350. return TX_CONTINUE;
  351. ps = &tx->sdata->bss->ps;
  352. } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
  353. ps = &tx->sdata->u.mesh.ps;
  354. } else {
  355. return TX_CONTINUE;
  356. }
  357. /* no buffering for ordered frames */
  358. if (ieee80211_has_order(hdr->frame_control))
  359. return TX_CONTINUE;
  360. if (ieee80211_is_probe_req(hdr->frame_control))
  361. return TX_CONTINUE;
  362. if (ieee80211_hw_check(&tx->local->hw, QUEUE_CONTROL))
  363. info->hw_queue = tx->sdata->vif.cab_queue;
  364. /* no stations in PS mode and no buffered packets */
  365. if (!atomic_read(&ps->num_sta_ps) && skb_queue_empty(&ps->bc_buf))
  366. return TX_CONTINUE;
  367. info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
  368. /* device releases frame after DTIM beacon */
  369. if (!ieee80211_hw_check(&tx->local->hw, HOST_BROADCAST_PS_BUFFERING))
  370. return TX_CONTINUE;
  371. /* buffered in mac80211 */
  372. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  373. purge_old_ps_buffers(tx->local);
  374. if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
  375. ps_dbg(tx->sdata,
  376. "BC TX buffer full - dropping the oldest frame\n");
  377. ieee80211_free_txskb(&tx->local->hw, skb_dequeue(&ps->bc_buf));
  378. } else
  379. tx->local->total_ps_buffered++;
  380. skb_queue_tail(&ps->bc_buf, tx->skb);
  381. return TX_QUEUED;
  382. }
  383. static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
  384. struct sk_buff *skb)
  385. {
  386. if (!ieee80211_is_mgmt(fc))
  387. return 0;
  388. if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
  389. return 0;
  390. if (!ieee80211_is_robust_mgmt_frame(skb))
  391. return 0;
  392. return 1;
  393. }
  394. static ieee80211_tx_result
  395. ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
  396. {
  397. struct sta_info *sta = tx->sta;
  398. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  399. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  400. struct ieee80211_local *local = tx->local;
  401. if (unlikely(!sta))
  402. return TX_CONTINUE;
  403. if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
  404. test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
  405. test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
  406. !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
  407. int ac = skb_get_queue_mapping(tx->skb);
  408. if (ieee80211_is_mgmt(hdr->frame_control) &&
  409. !ieee80211_is_bufferable_mmpdu(tx->skb)) {
  410. info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
  411. return TX_CONTINUE;
  412. }
  413. ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
  414. sta->sta.addr, sta->sta.aid, ac);
  415. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  416. purge_old_ps_buffers(tx->local);
  417. /* sync with ieee80211_sta_ps_deliver_wakeup */
  418. spin_lock(&sta->ps_lock);
  419. /*
  420. * STA woke up the meantime and all the frames on ps_tx_buf have
  421. * been queued to pending queue. No reordering can happen, go
  422. * ahead and Tx the packet.
  423. */
  424. if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
  425. !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
  426. !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
  427. spin_unlock(&sta->ps_lock);
  428. return TX_CONTINUE;
  429. }
  430. if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
  431. struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
  432. ps_dbg(tx->sdata,
  433. "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
  434. sta->sta.addr, ac);
  435. ieee80211_free_txskb(&local->hw, old);
  436. } else
  437. tx->local->total_ps_buffered++;
  438. info->control.jiffies = jiffies;
  439. info->control.vif = &tx->sdata->vif;
  440. info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
  441. info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
  442. skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
  443. spin_unlock(&sta->ps_lock);
  444. if (!timer_pending(&local->sta_cleanup))
  445. mod_timer(&local->sta_cleanup,
  446. round_jiffies(jiffies +
  447. STA_INFO_CLEANUP_INTERVAL));
  448. /*
  449. * We queued up some frames, so the TIM bit might
  450. * need to be set, recalculate it.
  451. */
  452. sta_info_recalc_tim(sta);
  453. return TX_QUEUED;
  454. } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
  455. ps_dbg(tx->sdata,
  456. "STA %pM in PS mode, but polling/in SP -> send frame\n",
  457. sta->sta.addr);
  458. }
  459. return TX_CONTINUE;
  460. }
  461. static ieee80211_tx_result debug_noinline
  462. ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
  463. {
  464. if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
  465. return TX_CONTINUE;
  466. if (tx->flags & IEEE80211_TX_UNICAST)
  467. return ieee80211_tx_h_unicast_ps_buf(tx);
  468. else
  469. return ieee80211_tx_h_multicast_ps_buf(tx);
  470. }
  471. static ieee80211_tx_result debug_noinline
  472. ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
  473. {
  474. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  475. if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
  476. if (tx->sdata->control_port_no_encrypt)
  477. info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  478. info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
  479. info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
  480. }
  481. return TX_CONTINUE;
  482. }
  483. static struct ieee80211_key *
  484. ieee80211_select_link_key(struct ieee80211_tx_data *tx)
  485. {
  486. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  487. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  488. struct ieee80211_link_data *link;
  489. unsigned int link_id;
  490. link_id = u32_get_bits(info->control.flags, IEEE80211_TX_CTRL_MLO_LINK);
  491. if (link_id == IEEE80211_LINK_UNSPECIFIED) {
  492. link = &tx->sdata->deflink;
  493. } else {
  494. link = rcu_dereference(tx->sdata->link[link_id]);
  495. if (!link)
  496. return NULL;
  497. }
  498. if (ieee80211_is_group_privacy_action(tx->skb))
  499. return rcu_dereference(link->default_multicast_key);
  500. else if (ieee80211_is_mgmt(hdr->frame_control) &&
  501. is_multicast_ether_addr(hdr->addr1) &&
  502. ieee80211_is_robust_mgmt_frame(tx->skb))
  503. return rcu_dereference(link->default_mgmt_key);
  504. else if (is_multicast_ether_addr(hdr->addr1))
  505. return rcu_dereference(link->default_multicast_key);
  506. return NULL;
  507. }
  508. static ieee80211_tx_result debug_noinline
  509. ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
  510. {
  511. struct ieee80211_key *key;
  512. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  513. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  514. if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)) {
  515. tx->key = NULL;
  516. return TX_CONTINUE;
  517. }
  518. if (tx->sta &&
  519. (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
  520. tx->key = key;
  521. else if ((key = ieee80211_select_link_key(tx)))
  522. tx->key = key;
  523. else if (!is_multicast_ether_addr(hdr->addr1) &&
  524. (key = rcu_dereference(tx->sdata->default_unicast_key)))
  525. tx->key = key;
  526. else
  527. tx->key = NULL;
  528. if (tx->key) {
  529. bool skip_hw = false;
  530. /* TODO: add threshold stuff again */
  531. switch (tx->key->conf.cipher) {
  532. case WLAN_CIPHER_SUITE_WEP40:
  533. case WLAN_CIPHER_SUITE_WEP104:
  534. case WLAN_CIPHER_SUITE_TKIP:
  535. if (!ieee80211_is_data_present(hdr->frame_control))
  536. tx->key = NULL;
  537. break;
  538. case WLAN_CIPHER_SUITE_CCMP:
  539. case WLAN_CIPHER_SUITE_CCMP_256:
  540. case WLAN_CIPHER_SUITE_GCMP:
  541. case WLAN_CIPHER_SUITE_GCMP_256:
  542. if (!ieee80211_is_data_present(hdr->frame_control) &&
  543. !ieee80211_use_mfp(hdr->frame_control, tx->sta,
  544. tx->skb) &&
  545. !ieee80211_is_group_privacy_action(tx->skb))
  546. tx->key = NULL;
  547. else
  548. skip_hw = (tx->key->conf.flags &
  549. IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
  550. ieee80211_is_mgmt(hdr->frame_control);
  551. break;
  552. case WLAN_CIPHER_SUITE_AES_CMAC:
  553. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  554. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  555. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  556. if (!ieee80211_is_mgmt(hdr->frame_control))
  557. tx->key = NULL;
  558. break;
  559. }
  560. if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
  561. !ieee80211_is_deauth(hdr->frame_control)) &&
  562. tx->skb->protocol != tx->sdata->control_port_protocol)
  563. return TX_DROP;
  564. if (!skip_hw && tx->key &&
  565. tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
  566. info->control.hw_key = &tx->key->conf;
  567. } else if (ieee80211_is_data_present(hdr->frame_control) && tx->sta &&
  568. test_sta_flag(tx->sta, WLAN_STA_USES_ENCRYPTION)) {
  569. return TX_DROP;
  570. }
  571. return TX_CONTINUE;
  572. }
  573. static ieee80211_tx_result debug_noinline
  574. ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
  575. {
  576. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  577. struct ieee80211_hdr *hdr = (void *)tx->skb->data;
  578. struct ieee80211_supported_band *sband;
  579. u32 len;
  580. struct ieee80211_tx_rate_control txrc;
  581. struct ieee80211_sta_rates *ratetbl = NULL;
  582. bool encap = info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP;
  583. bool assoc = false;
  584. memset(&txrc, 0, sizeof(txrc));
  585. sband = tx->local->hw.wiphy->bands[info->band];
  586. len = min_t(u32, tx->skb->len + FCS_LEN,
  587. tx->local->hw.wiphy->frag_threshold);
  588. /* set up the tx rate control struct we give the RC algo */
  589. txrc.hw = &tx->local->hw;
  590. txrc.sband = sband;
  591. txrc.bss_conf = &tx->sdata->vif.bss_conf;
  592. txrc.skb = tx->skb;
  593. txrc.reported_rate.idx = -1;
  594. if (unlikely(info->control.flags & IEEE80211_TX_CTRL_DONT_USE_RATE_MASK)) {
  595. txrc.rate_idx_mask = ~0;
  596. } else {
  597. txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
  598. if (tx->sdata->rc_has_mcs_mask[info->band])
  599. txrc.rate_idx_mcs_mask =
  600. tx->sdata->rc_rateidx_mcs_mask[info->band];
  601. }
  602. txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
  603. tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
  604. tx->sdata->vif.type == NL80211_IFTYPE_ADHOC ||
  605. tx->sdata->vif.type == NL80211_IFTYPE_OCB);
  606. /* set up RTS protection if desired */
  607. if (len > tx->local->hw.wiphy->rts_threshold) {
  608. txrc.rts = true;
  609. }
  610. info->control.use_rts = txrc.rts;
  611. info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
  612. /*
  613. * Use short preamble if the BSS can handle it, but not for
  614. * management frames unless we know the receiver can handle
  615. * that -- the management frame might be to a station that
  616. * just wants a probe response.
  617. */
  618. if (tx->sdata->vif.bss_conf.use_short_preamble &&
  619. (ieee80211_is_tx_data(tx->skb) ||
  620. (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
  621. txrc.short_preamble = true;
  622. info->control.short_preamble = txrc.short_preamble;
  623. /* don't ask rate control when rate already injected via radiotap */
  624. if (info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT)
  625. return TX_CONTINUE;
  626. if (tx->sta)
  627. assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
  628. /*
  629. * Lets not bother rate control if we're associated and cannot
  630. * talk to the sta. This should not happen.
  631. */
  632. if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
  633. !rate_usable_index_exists(sband, &tx->sta->sta),
  634. "%s: Dropped data frame as no usable bitrate found while "
  635. "scanning and associated. Target station: "
  636. "%pM on %d GHz band\n",
  637. tx->sdata->name,
  638. encap ? ((struct ethhdr *)hdr)->h_dest : hdr->addr1,
  639. info->band ? 5 : 2))
  640. return TX_DROP;
  641. /*
  642. * If we're associated with the sta at this point we know we can at
  643. * least send the frame at the lowest bit rate.
  644. */
  645. rate_control_get_rate(tx->sdata, tx->sta, &txrc);
  646. if (tx->sta && !info->control.skip_table)
  647. ratetbl = rcu_dereference(tx->sta->sta.rates);
  648. if (unlikely(info->control.rates[0].idx < 0)) {
  649. if (ratetbl) {
  650. struct ieee80211_tx_rate rate = {
  651. .idx = ratetbl->rate[0].idx,
  652. .flags = ratetbl->rate[0].flags,
  653. .count = ratetbl->rate[0].count
  654. };
  655. if (ratetbl->rate[0].idx < 0)
  656. return TX_DROP;
  657. tx->rate = rate;
  658. } else {
  659. return TX_DROP;
  660. }
  661. } else {
  662. tx->rate = info->control.rates[0];
  663. }
  664. if (txrc.reported_rate.idx < 0) {
  665. txrc.reported_rate = tx->rate;
  666. if (tx->sta && ieee80211_is_tx_data(tx->skb))
  667. tx->sta->deflink.tx_stats.last_rate = txrc.reported_rate;
  668. } else if (tx->sta)
  669. tx->sta->deflink.tx_stats.last_rate = txrc.reported_rate;
  670. if (ratetbl)
  671. return TX_CONTINUE;
  672. if (unlikely(!info->control.rates[0].count))
  673. info->control.rates[0].count = 1;
  674. if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
  675. (info->flags & IEEE80211_TX_CTL_NO_ACK)))
  676. info->control.rates[0].count = 1;
  677. return TX_CONTINUE;
  678. }
  679. static __le16 ieee80211_tx_next_seq(struct sta_info *sta, int tid)
  680. {
  681. u16 *seq = &sta->tid_seq[tid];
  682. __le16 ret = cpu_to_le16(*seq);
  683. /* Increase the sequence number. */
  684. *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
  685. return ret;
  686. }
  687. static ieee80211_tx_result debug_noinline
  688. ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
  689. {
  690. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  691. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  692. int tid;
  693. /*
  694. * Packet injection may want to control the sequence
  695. * number, if we have no matching interface then we
  696. * neither assign one ourselves nor ask the driver to.
  697. */
  698. if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
  699. return TX_CONTINUE;
  700. if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
  701. return TX_CONTINUE;
  702. if (ieee80211_hdrlen(hdr->frame_control) < 24)
  703. return TX_CONTINUE;
  704. if (ieee80211_is_qos_nullfunc(hdr->frame_control))
  705. return TX_CONTINUE;
  706. if (info->control.flags & IEEE80211_TX_CTRL_NO_SEQNO)
  707. return TX_CONTINUE;
  708. /* SNS11 from 802.11be 10.3.2.14 */
  709. if (unlikely(is_multicast_ether_addr(hdr->addr1) &&
  710. ieee80211_vif_is_mld(info->control.vif) &&
  711. info->control.vif->type == NL80211_IFTYPE_AP)) {
  712. if (info->control.flags & IEEE80211_TX_CTRL_MCAST_MLO_FIRST_TX)
  713. tx->sdata->mld_mcast_seq += 0x10;
  714. hdr->seq_ctrl = cpu_to_le16(tx->sdata->mld_mcast_seq);
  715. return TX_CONTINUE;
  716. }
  717. /*
  718. * Anything but QoS data that has a sequence number field
  719. * (is long enough) gets a sequence number from the global
  720. * counter. QoS data frames with a multicast destination
  721. * also use the global counter (802.11-2012 9.3.2.10).
  722. */
  723. if (!ieee80211_is_data_qos(hdr->frame_control) ||
  724. is_multicast_ether_addr(hdr->addr1)) {
  725. /* driver should assign sequence number */
  726. info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
  727. /* for pure STA mode without beacons, we can do it */
  728. hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
  729. tx->sdata->sequence_number += 0x10;
  730. if (tx->sta)
  731. tx->sta->deflink.tx_stats.msdu[IEEE80211_NUM_TIDS]++;
  732. return TX_CONTINUE;
  733. }
  734. /*
  735. * This should be true for injected/management frames only, for
  736. * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
  737. * above since they are not QoS-data frames.
  738. */
  739. if (!tx->sta)
  740. return TX_CONTINUE;
  741. /* include per-STA, per-TID sequence counter */
  742. tid = ieee80211_get_tid(hdr);
  743. tx->sta->deflink.tx_stats.msdu[tid]++;
  744. hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid);
  745. return TX_CONTINUE;
  746. }
  747. static int ieee80211_fragment(struct ieee80211_tx_data *tx,
  748. struct sk_buff *skb, int hdrlen,
  749. int frag_threshold)
  750. {
  751. struct ieee80211_local *local = tx->local;
  752. struct ieee80211_tx_info *info;
  753. struct sk_buff *tmp;
  754. int per_fragm = frag_threshold - hdrlen - FCS_LEN;
  755. int pos = hdrlen + per_fragm;
  756. int rem = skb->len - hdrlen - per_fragm;
  757. if (WARN_ON(rem < 0))
  758. return -EINVAL;
  759. /* first fragment was already added to queue by caller */
  760. while (rem) {
  761. int fraglen = per_fragm;
  762. if (fraglen > rem)
  763. fraglen = rem;
  764. rem -= fraglen;
  765. tmp = dev_alloc_skb(local->tx_headroom +
  766. frag_threshold +
  767. IEEE80211_ENCRYPT_HEADROOM +
  768. IEEE80211_ENCRYPT_TAILROOM);
  769. if (!tmp)
  770. return -ENOMEM;
  771. __skb_queue_tail(&tx->skbs, tmp);
  772. skb_reserve(tmp,
  773. local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM);
  774. /* copy control information */
  775. memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
  776. info = IEEE80211_SKB_CB(tmp);
  777. info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
  778. IEEE80211_TX_CTL_FIRST_FRAGMENT);
  779. if (rem)
  780. info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
  781. skb_copy_queue_mapping(tmp, skb);
  782. tmp->priority = skb->priority;
  783. tmp->dev = skb->dev;
  784. /* copy header and data */
  785. skb_put_data(tmp, skb->data, hdrlen);
  786. skb_put_data(tmp, skb->data + pos, fraglen);
  787. pos += fraglen;
  788. }
  789. /* adjust first fragment's length */
  790. skb_trim(skb, hdrlen + per_fragm);
  791. return 0;
  792. }
  793. static ieee80211_tx_result debug_noinline
  794. ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
  795. {
  796. struct sk_buff *skb = tx->skb;
  797. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  798. struct ieee80211_hdr *hdr = (void *)skb->data;
  799. int frag_threshold = tx->local->hw.wiphy->frag_threshold;
  800. int hdrlen;
  801. int fragnum;
  802. /* no matter what happens, tx->skb moves to tx->skbs */
  803. __skb_queue_tail(&tx->skbs, skb);
  804. tx->skb = NULL;
  805. if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
  806. return TX_CONTINUE;
  807. if (ieee80211_hw_check(&tx->local->hw, SUPPORTS_TX_FRAG))
  808. return TX_CONTINUE;
  809. /*
  810. * Warn when submitting a fragmented A-MPDU frame and drop it.
  811. * This scenario is handled in ieee80211_tx_prepare but extra
  812. * caution taken here as fragmented ampdu may cause Tx stop.
  813. */
  814. if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
  815. return TX_DROP;
  816. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  817. /* internal error, why isn't DONTFRAG set? */
  818. if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
  819. return TX_DROP;
  820. /*
  821. * Now fragment the frame. This will allocate all the fragments and
  822. * chain them (using skb as the first fragment) to skb->next.
  823. * During transmission, we will remove the successfully transmitted
  824. * fragments from this list. When the low-level driver rejects one
  825. * of the fragments then we will simply pretend to accept the skb
  826. * but store it away as pending.
  827. */
  828. if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
  829. return TX_DROP;
  830. /* update duration/seq/flags of fragments */
  831. fragnum = 0;
  832. skb_queue_walk(&tx->skbs, skb) {
  833. const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
  834. hdr = (void *)skb->data;
  835. info = IEEE80211_SKB_CB(skb);
  836. if (!skb_queue_is_last(&tx->skbs, skb)) {
  837. hdr->frame_control |= morefrags;
  838. /*
  839. * No multi-rate retries for fragmented frames, that
  840. * would completely throw off the NAV at other STAs.
  841. */
  842. info->control.rates[1].idx = -1;
  843. info->control.rates[2].idx = -1;
  844. info->control.rates[3].idx = -1;
  845. BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
  846. info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  847. } else {
  848. hdr->frame_control &= ~morefrags;
  849. }
  850. hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
  851. fragnum++;
  852. }
  853. return TX_CONTINUE;
  854. }
  855. static ieee80211_tx_result debug_noinline
  856. ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
  857. {
  858. struct sk_buff *skb;
  859. int ac = -1;
  860. if (!tx->sta)
  861. return TX_CONTINUE;
  862. skb_queue_walk(&tx->skbs, skb) {
  863. ac = skb_get_queue_mapping(skb);
  864. tx->sta->deflink.tx_stats.bytes[ac] += skb->len;
  865. }
  866. if (ac >= 0)
  867. tx->sta->deflink.tx_stats.packets[ac]++;
  868. return TX_CONTINUE;
  869. }
  870. static ieee80211_tx_result debug_noinline
  871. ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
  872. {
  873. if (!tx->key)
  874. return TX_CONTINUE;
  875. switch (tx->key->conf.cipher) {
  876. case WLAN_CIPHER_SUITE_WEP40:
  877. case WLAN_CIPHER_SUITE_WEP104:
  878. return ieee80211_crypto_wep_encrypt(tx);
  879. case WLAN_CIPHER_SUITE_TKIP:
  880. return ieee80211_crypto_tkip_encrypt(tx);
  881. case WLAN_CIPHER_SUITE_CCMP:
  882. return ieee80211_crypto_ccmp_encrypt(
  883. tx, IEEE80211_CCMP_MIC_LEN);
  884. case WLAN_CIPHER_SUITE_CCMP_256:
  885. return ieee80211_crypto_ccmp_encrypt(
  886. tx, IEEE80211_CCMP_256_MIC_LEN);
  887. case WLAN_CIPHER_SUITE_AES_CMAC:
  888. return ieee80211_crypto_aes_cmac_encrypt(tx);
  889. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  890. return ieee80211_crypto_aes_cmac_256_encrypt(tx);
  891. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  892. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  893. return ieee80211_crypto_aes_gmac_encrypt(tx);
  894. case WLAN_CIPHER_SUITE_GCMP:
  895. case WLAN_CIPHER_SUITE_GCMP_256:
  896. return ieee80211_crypto_gcmp_encrypt(tx);
  897. }
  898. return TX_DROP;
  899. }
  900. static ieee80211_tx_result debug_noinline
  901. ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
  902. {
  903. struct sk_buff *skb;
  904. struct ieee80211_hdr *hdr;
  905. int next_len;
  906. bool group_addr;
  907. skb_queue_walk(&tx->skbs, skb) {
  908. hdr = (void *) skb->data;
  909. if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
  910. break; /* must not overwrite AID */
  911. if (!skb_queue_is_last(&tx->skbs, skb)) {
  912. struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
  913. next_len = next->len;
  914. } else
  915. next_len = 0;
  916. group_addr = is_multicast_ether_addr(hdr->addr1);
  917. hdr->duration_id =
  918. ieee80211_duration(tx, skb, group_addr, next_len);
  919. }
  920. return TX_CONTINUE;
  921. }
  922. /* actual transmit path */
  923. static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
  924. struct sk_buff *skb,
  925. struct ieee80211_tx_info *info,
  926. struct tid_ampdu_tx *tid_tx,
  927. int tid)
  928. {
  929. bool queued = false;
  930. bool reset_agg_timer = false;
  931. struct sk_buff *purge_skb = NULL;
  932. if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
  933. reset_agg_timer = true;
  934. } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
  935. /*
  936. * nothing -- this aggregation session is being started
  937. * but that might still fail with the driver
  938. */
  939. } else if (!tx->sta->sta.txq[tid]) {
  940. spin_lock(&tx->sta->lock);
  941. /*
  942. * Need to re-check now, because we may get here
  943. *
  944. * 1) in the window during which the setup is actually
  945. * already done, but not marked yet because not all
  946. * packets are spliced over to the driver pending
  947. * queue yet -- if this happened we acquire the lock
  948. * either before or after the splice happens, but
  949. * need to recheck which of these cases happened.
  950. *
  951. * 2) during session teardown, if the OPERATIONAL bit
  952. * was cleared due to the teardown but the pointer
  953. * hasn't been assigned NULL yet (or we loaded it
  954. * before it was assigned) -- in this case it may
  955. * now be NULL which means we should just let the
  956. * packet pass through because splicing the frames
  957. * back is already done.
  958. */
  959. tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
  960. if (!tid_tx) {
  961. /* do nothing, let packet pass through */
  962. } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
  963. reset_agg_timer = true;
  964. } else {
  965. queued = true;
  966. if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) {
  967. clear_sta_flag(tx->sta, WLAN_STA_SP);
  968. ps_dbg(tx->sta->sdata,
  969. "STA %pM aid %d: SP frame queued, close the SP w/o telling the peer\n",
  970. tx->sta->sta.addr, tx->sta->sta.aid);
  971. }
  972. info->control.vif = &tx->sdata->vif;
  973. info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
  974. info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
  975. __skb_queue_tail(&tid_tx->pending, skb);
  976. if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
  977. purge_skb = __skb_dequeue(&tid_tx->pending);
  978. }
  979. spin_unlock(&tx->sta->lock);
  980. if (purge_skb)
  981. ieee80211_free_txskb(&tx->local->hw, purge_skb);
  982. }
  983. /* reset session timer */
  984. if (reset_agg_timer)
  985. tid_tx->last_tx = jiffies;
  986. return queued;
  987. }
  988. void ieee80211_aggr_check(struct ieee80211_sub_if_data *sdata,
  989. struct sta_info *sta, struct sk_buff *skb)
  990. {
  991. struct rate_control_ref *ref = sdata->local->rate_ctrl;
  992. u16 tid;
  993. if (!ref || !(ref->ops->capa & RATE_CTRL_CAPA_AMPDU_TRIGGER))
  994. return;
  995. if (!sta || !sta->sta.deflink.ht_cap.ht_supported ||
  996. !sta->sta.wme || skb_get_queue_mapping(skb) == IEEE80211_AC_VO ||
  997. skb->protocol == sdata->control_port_protocol)
  998. return;
  999. tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
  1000. if (likely(sta->ampdu_mlme.tid_tx[tid]))
  1001. return;
  1002. ieee80211_start_tx_ba_session(&sta->sta, tid, 0);
  1003. }
  1004. /*
  1005. * initialises @tx
  1006. * pass %NULL for the station if unknown, a valid pointer if known
  1007. * or an ERR_PTR() if the station is known not to exist
  1008. */
  1009. static ieee80211_tx_result
  1010. ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
  1011. struct ieee80211_tx_data *tx,
  1012. struct sta_info *sta, struct sk_buff *skb)
  1013. {
  1014. struct ieee80211_local *local = sdata->local;
  1015. struct ieee80211_hdr *hdr;
  1016. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1017. bool aggr_check = false;
  1018. int tid;
  1019. memset(tx, 0, sizeof(*tx));
  1020. tx->skb = skb;
  1021. tx->local = local;
  1022. tx->sdata = sdata;
  1023. __skb_queue_head_init(&tx->skbs);
  1024. /*
  1025. * If this flag is set to true anywhere, and we get here,
  1026. * we are doing the needed processing, so remove the flag
  1027. * now.
  1028. */
  1029. info->control.flags &= ~IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
  1030. hdr = (struct ieee80211_hdr *) skb->data;
  1031. if (likely(sta)) {
  1032. if (!IS_ERR(sta))
  1033. tx->sta = sta;
  1034. } else {
  1035. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  1036. tx->sta = rcu_dereference(sdata->u.vlan.sta);
  1037. if (!tx->sta && sdata->wdev.use_4addr)
  1038. return TX_DROP;
  1039. } else if (tx->sdata->control_port_protocol == tx->skb->protocol) {
  1040. tx->sta = sta_info_get_bss(sdata, hdr->addr1);
  1041. }
  1042. if (!tx->sta && !is_multicast_ether_addr(hdr->addr1)) {
  1043. tx->sta = sta_info_get(sdata, hdr->addr1);
  1044. aggr_check = true;
  1045. }
  1046. }
  1047. if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
  1048. !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
  1049. ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
  1050. !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) {
  1051. struct tid_ampdu_tx *tid_tx;
  1052. tid = ieee80211_get_tid(hdr);
  1053. tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
  1054. if (!tid_tx && aggr_check) {
  1055. ieee80211_aggr_check(sdata, tx->sta, skb);
  1056. tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
  1057. }
  1058. if (tid_tx) {
  1059. bool queued;
  1060. queued = ieee80211_tx_prep_agg(tx, skb, info,
  1061. tid_tx, tid);
  1062. if (unlikely(queued))
  1063. return TX_QUEUED;
  1064. }
  1065. }
  1066. if (is_multicast_ether_addr(hdr->addr1)) {
  1067. tx->flags &= ~IEEE80211_TX_UNICAST;
  1068. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  1069. } else
  1070. tx->flags |= IEEE80211_TX_UNICAST;
  1071. if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
  1072. if (!(tx->flags & IEEE80211_TX_UNICAST) ||
  1073. skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
  1074. info->flags & IEEE80211_TX_CTL_AMPDU)
  1075. info->flags |= IEEE80211_TX_CTL_DONTFRAG;
  1076. }
  1077. if (!tx->sta)
  1078. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
  1079. else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT)) {
  1080. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
  1081. ieee80211_check_fast_xmit(tx->sta);
  1082. }
  1083. info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
  1084. return TX_CONTINUE;
  1085. }
  1086. static struct txq_info *ieee80211_get_txq(struct ieee80211_local *local,
  1087. struct ieee80211_vif *vif,
  1088. struct sta_info *sta,
  1089. struct sk_buff *skb)
  1090. {
  1091. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1092. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1093. struct ieee80211_txq *txq = NULL;
  1094. if ((info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) ||
  1095. (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE))
  1096. return NULL;
  1097. if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) &&
  1098. unlikely(!ieee80211_is_data_present(hdr->frame_control))) {
  1099. if ((!ieee80211_is_mgmt(hdr->frame_control) ||
  1100. ieee80211_is_bufferable_mmpdu(skb) ||
  1101. vif->type == NL80211_IFTYPE_STATION) &&
  1102. sta && sta->uploaded) {
  1103. /*
  1104. * This will be NULL if the driver didn't set the
  1105. * opt-in hardware flag.
  1106. */
  1107. txq = sta->sta.txq[IEEE80211_NUM_TIDS];
  1108. }
  1109. } else if (sta) {
  1110. u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
  1111. if (!sta->uploaded)
  1112. return NULL;
  1113. txq = sta->sta.txq[tid];
  1114. } else {
  1115. txq = vif->txq;
  1116. }
  1117. if (!txq)
  1118. return NULL;
  1119. return to_txq_info(txq);
  1120. }
  1121. static void ieee80211_set_skb_enqueue_time(struct sk_buff *skb)
  1122. {
  1123. struct sk_buff *next;
  1124. codel_time_t now = codel_get_time();
  1125. skb_list_walk_safe(skb, skb, next)
  1126. IEEE80211_SKB_CB(skb)->control.enqueue_time = now;
  1127. }
  1128. static u32 codel_skb_len_func(const struct sk_buff *skb)
  1129. {
  1130. return skb->len;
  1131. }
  1132. static codel_time_t codel_skb_time_func(const struct sk_buff *skb)
  1133. {
  1134. const struct ieee80211_tx_info *info;
  1135. info = (const struct ieee80211_tx_info *)skb->cb;
  1136. return info->control.enqueue_time;
  1137. }
  1138. static struct sk_buff *codel_dequeue_func(struct codel_vars *cvars,
  1139. void *ctx)
  1140. {
  1141. struct ieee80211_local *local;
  1142. struct txq_info *txqi;
  1143. struct fq *fq;
  1144. struct fq_flow *flow;
  1145. txqi = ctx;
  1146. local = vif_to_sdata(txqi->txq.vif)->local;
  1147. fq = &local->fq;
  1148. if (cvars == &txqi->def_cvars)
  1149. flow = &txqi->tin.default_flow;
  1150. else
  1151. flow = &fq->flows[cvars - local->cvars];
  1152. return fq_flow_dequeue(fq, flow);
  1153. }
  1154. static void codel_drop_func(struct sk_buff *skb,
  1155. void *ctx)
  1156. {
  1157. struct ieee80211_local *local;
  1158. struct ieee80211_hw *hw;
  1159. struct txq_info *txqi;
  1160. txqi = ctx;
  1161. local = vif_to_sdata(txqi->txq.vif)->local;
  1162. hw = &local->hw;
  1163. ieee80211_free_txskb(hw, skb);
  1164. }
  1165. static struct sk_buff *fq_tin_dequeue_func(struct fq *fq,
  1166. struct fq_tin *tin,
  1167. struct fq_flow *flow)
  1168. {
  1169. struct ieee80211_local *local;
  1170. struct txq_info *txqi;
  1171. struct codel_vars *cvars;
  1172. struct codel_params *cparams;
  1173. struct codel_stats *cstats;
  1174. local = container_of(fq, struct ieee80211_local, fq);
  1175. txqi = container_of(tin, struct txq_info, tin);
  1176. cstats = &txqi->cstats;
  1177. if (txqi->txq.sta) {
  1178. struct sta_info *sta = container_of(txqi->txq.sta,
  1179. struct sta_info, sta);
  1180. cparams = &sta->cparams;
  1181. } else {
  1182. cparams = &local->cparams;
  1183. }
  1184. if (flow == &tin->default_flow)
  1185. cvars = &txqi->def_cvars;
  1186. else
  1187. cvars = &local->cvars[flow - fq->flows];
  1188. return codel_dequeue(txqi,
  1189. &flow->backlog,
  1190. cparams,
  1191. cvars,
  1192. cstats,
  1193. codel_skb_len_func,
  1194. codel_skb_time_func,
  1195. codel_drop_func,
  1196. codel_dequeue_func);
  1197. }
  1198. static void fq_skb_free_func(struct fq *fq,
  1199. struct fq_tin *tin,
  1200. struct fq_flow *flow,
  1201. struct sk_buff *skb)
  1202. {
  1203. struct ieee80211_local *local;
  1204. local = container_of(fq, struct ieee80211_local, fq);
  1205. ieee80211_free_txskb(&local->hw, skb);
  1206. }
  1207. static void ieee80211_txq_enqueue(struct ieee80211_local *local,
  1208. struct txq_info *txqi,
  1209. struct sk_buff *skb)
  1210. {
  1211. struct fq *fq = &local->fq;
  1212. struct fq_tin *tin = &txqi->tin;
  1213. u32 flow_idx = fq_flow_idx(fq, skb);
  1214. ieee80211_set_skb_enqueue_time(skb);
  1215. spin_lock_bh(&fq->lock);
  1216. /*
  1217. * For management frames, don't really apply codel etc.,
  1218. * we don't want to apply any shaping or anything we just
  1219. * want to simplify the driver API by having them on the
  1220. * txqi.
  1221. */
  1222. if (unlikely(txqi->txq.tid == IEEE80211_NUM_TIDS)) {
  1223. IEEE80211_SKB_CB(skb)->control.flags |=
  1224. IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
  1225. __skb_queue_tail(&txqi->frags, skb);
  1226. } else {
  1227. fq_tin_enqueue(fq, tin, flow_idx, skb,
  1228. fq_skb_free_func);
  1229. }
  1230. spin_unlock_bh(&fq->lock);
  1231. }
  1232. static bool fq_vlan_filter_func(struct fq *fq, struct fq_tin *tin,
  1233. struct fq_flow *flow, struct sk_buff *skb,
  1234. void *data)
  1235. {
  1236. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1237. return info->control.vif == data;
  1238. }
  1239. void ieee80211_txq_remove_vlan(struct ieee80211_local *local,
  1240. struct ieee80211_sub_if_data *sdata)
  1241. {
  1242. struct fq *fq = &local->fq;
  1243. struct txq_info *txqi;
  1244. struct fq_tin *tin;
  1245. struct ieee80211_sub_if_data *ap;
  1246. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
  1247. return;
  1248. ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
  1249. if (!ap->vif.txq)
  1250. return;
  1251. txqi = to_txq_info(ap->vif.txq);
  1252. tin = &txqi->tin;
  1253. spin_lock_bh(&fq->lock);
  1254. fq_tin_filter(fq, tin, fq_vlan_filter_func, &sdata->vif,
  1255. fq_skb_free_func);
  1256. spin_unlock_bh(&fq->lock);
  1257. }
  1258. void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata,
  1259. struct sta_info *sta,
  1260. struct txq_info *txqi, int tid)
  1261. {
  1262. fq_tin_init(&txqi->tin);
  1263. codel_vars_init(&txqi->def_cvars);
  1264. codel_stats_init(&txqi->cstats);
  1265. __skb_queue_head_init(&txqi->frags);
  1266. INIT_LIST_HEAD(&txqi->schedule_order);
  1267. txqi->txq.vif = &sdata->vif;
  1268. if (!sta) {
  1269. sdata->vif.txq = &txqi->txq;
  1270. txqi->txq.tid = 0;
  1271. txqi->txq.ac = IEEE80211_AC_BE;
  1272. return;
  1273. }
  1274. if (tid == IEEE80211_NUM_TIDS) {
  1275. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  1276. /* Drivers need to opt in to the management MPDU TXQ */
  1277. if (!ieee80211_hw_check(&sdata->local->hw,
  1278. STA_MMPDU_TXQ))
  1279. return;
  1280. } else if (!ieee80211_hw_check(&sdata->local->hw,
  1281. BUFF_MMPDU_TXQ)) {
  1282. /* Drivers need to opt in to the bufferable MMPDU TXQ */
  1283. return;
  1284. }
  1285. txqi->txq.ac = IEEE80211_AC_VO;
  1286. } else {
  1287. txqi->txq.ac = ieee80211_ac_from_tid(tid);
  1288. }
  1289. txqi->txq.sta = &sta->sta;
  1290. txqi->txq.tid = tid;
  1291. sta->sta.txq[tid] = &txqi->txq;
  1292. }
  1293. void ieee80211_txq_purge(struct ieee80211_local *local,
  1294. struct txq_info *txqi)
  1295. {
  1296. struct fq *fq = &local->fq;
  1297. struct fq_tin *tin = &txqi->tin;
  1298. spin_lock_bh(&fq->lock);
  1299. fq_tin_reset(fq, tin, fq_skb_free_func);
  1300. ieee80211_purge_tx_queue(&local->hw, &txqi->frags);
  1301. spin_unlock_bh(&fq->lock);
  1302. spin_lock_bh(&local->active_txq_lock[txqi->txq.ac]);
  1303. list_del_init(&txqi->schedule_order);
  1304. spin_unlock_bh(&local->active_txq_lock[txqi->txq.ac]);
  1305. }
  1306. void ieee80211_txq_set_params(struct ieee80211_local *local)
  1307. {
  1308. if (local->hw.wiphy->txq_limit)
  1309. local->fq.limit = local->hw.wiphy->txq_limit;
  1310. else
  1311. local->hw.wiphy->txq_limit = local->fq.limit;
  1312. if (local->hw.wiphy->txq_memory_limit)
  1313. local->fq.memory_limit = local->hw.wiphy->txq_memory_limit;
  1314. else
  1315. local->hw.wiphy->txq_memory_limit = local->fq.memory_limit;
  1316. if (local->hw.wiphy->txq_quantum)
  1317. local->fq.quantum = local->hw.wiphy->txq_quantum;
  1318. else
  1319. local->hw.wiphy->txq_quantum = local->fq.quantum;
  1320. }
  1321. int ieee80211_txq_setup_flows(struct ieee80211_local *local)
  1322. {
  1323. struct fq *fq = &local->fq;
  1324. int ret;
  1325. int i;
  1326. bool supp_vht = false;
  1327. enum nl80211_band band;
  1328. ret = fq_init(fq, 4096);
  1329. if (ret)
  1330. return ret;
  1331. /*
  1332. * If the hardware doesn't support VHT, it is safe to limit the maximum
  1333. * queue size. 4 Mbytes is 64 max-size aggregates in 802.11n.
  1334. */
  1335. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  1336. struct ieee80211_supported_band *sband;
  1337. sband = local->hw.wiphy->bands[band];
  1338. if (!sband)
  1339. continue;
  1340. supp_vht = supp_vht || sband->vht_cap.vht_supported;
  1341. }
  1342. if (!supp_vht)
  1343. fq->memory_limit = 4 << 20; /* 4 Mbytes */
  1344. codel_params_init(&local->cparams);
  1345. local->cparams.interval = MS2TIME(100);
  1346. local->cparams.target = MS2TIME(20);
  1347. local->cparams.ecn = true;
  1348. local->cvars = kvcalloc(fq->flows_cnt, sizeof(local->cvars[0]),
  1349. GFP_KERNEL);
  1350. if (!local->cvars) {
  1351. spin_lock_bh(&fq->lock);
  1352. fq_reset(fq, fq_skb_free_func);
  1353. spin_unlock_bh(&fq->lock);
  1354. return -ENOMEM;
  1355. }
  1356. for (i = 0; i < fq->flows_cnt; i++)
  1357. codel_vars_init(&local->cvars[i]);
  1358. ieee80211_txq_set_params(local);
  1359. return 0;
  1360. }
  1361. void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
  1362. {
  1363. struct fq *fq = &local->fq;
  1364. kvfree(local->cvars);
  1365. local->cvars = NULL;
  1366. spin_lock_bh(&fq->lock);
  1367. fq_reset(fq, fq_skb_free_func);
  1368. spin_unlock_bh(&fq->lock);
  1369. }
  1370. static bool ieee80211_queue_skb(struct ieee80211_local *local,
  1371. struct ieee80211_sub_if_data *sdata,
  1372. struct sta_info *sta,
  1373. struct sk_buff *skb)
  1374. {
  1375. struct ieee80211_vif *vif;
  1376. struct txq_info *txqi;
  1377. if (sdata->vif.type == NL80211_IFTYPE_MONITOR)
  1378. return false;
  1379. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1380. sdata = container_of(sdata->bss,
  1381. struct ieee80211_sub_if_data, u.ap);
  1382. vif = &sdata->vif;
  1383. txqi = ieee80211_get_txq(local, vif, sta, skb);
  1384. if (!txqi)
  1385. return false;
  1386. ieee80211_txq_enqueue(local, txqi, skb);
  1387. schedule_and_wake_txq(local, txqi);
  1388. return true;
  1389. }
  1390. static bool ieee80211_tx_frags(struct ieee80211_local *local,
  1391. struct ieee80211_vif *vif,
  1392. struct sta_info *sta,
  1393. struct sk_buff_head *skbs,
  1394. bool txpending)
  1395. {
  1396. struct ieee80211_tx_control control = {};
  1397. struct sk_buff *skb, *tmp;
  1398. unsigned long flags;
  1399. skb_queue_walk_safe(skbs, skb, tmp) {
  1400. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1401. int q = info->hw_queue;
  1402. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1403. if (WARN_ON_ONCE(q >= local->hw.queues)) {
  1404. __skb_unlink(skb, skbs);
  1405. ieee80211_free_txskb(&local->hw, skb);
  1406. continue;
  1407. }
  1408. #endif
  1409. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  1410. if (local->queue_stop_reasons[q] ||
  1411. (!txpending && !skb_queue_empty(&local->pending[q]))) {
  1412. if (unlikely(info->flags &
  1413. IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
  1414. if (local->queue_stop_reasons[q] &
  1415. ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
  1416. /*
  1417. * Drop off-channel frames if queues
  1418. * are stopped for any reason other
  1419. * than off-channel operation. Never
  1420. * queue them.
  1421. */
  1422. spin_unlock_irqrestore(
  1423. &local->queue_stop_reason_lock,
  1424. flags);
  1425. ieee80211_purge_tx_queue(&local->hw,
  1426. skbs);
  1427. return true;
  1428. }
  1429. } else {
  1430. /*
  1431. * Since queue is stopped, queue up frames for
  1432. * later transmission from the tx-pending
  1433. * tasklet when the queue is woken again.
  1434. */
  1435. if (txpending)
  1436. skb_queue_splice_init(skbs,
  1437. &local->pending[q]);
  1438. else
  1439. skb_queue_splice_tail_init(skbs,
  1440. &local->pending[q]);
  1441. spin_unlock_irqrestore(&local->queue_stop_reason_lock,
  1442. flags);
  1443. return false;
  1444. }
  1445. }
  1446. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  1447. info->control.vif = vif;
  1448. control.sta = sta ? &sta->sta : NULL;
  1449. __skb_unlink(skb, skbs);
  1450. drv_tx(local, &control, skb);
  1451. }
  1452. return true;
  1453. }
  1454. /*
  1455. * Returns false if the frame couldn't be transmitted but was queued instead.
  1456. */
  1457. static bool __ieee80211_tx(struct ieee80211_local *local,
  1458. struct sk_buff_head *skbs, struct sta_info *sta,
  1459. bool txpending)
  1460. {
  1461. struct ieee80211_tx_info *info;
  1462. struct ieee80211_sub_if_data *sdata;
  1463. struct ieee80211_vif *vif;
  1464. struct sk_buff *skb;
  1465. bool result;
  1466. if (WARN_ON(skb_queue_empty(skbs)))
  1467. return true;
  1468. skb = skb_peek(skbs);
  1469. info = IEEE80211_SKB_CB(skb);
  1470. sdata = vif_to_sdata(info->control.vif);
  1471. if (sta && !sta->uploaded)
  1472. sta = NULL;
  1473. switch (sdata->vif.type) {
  1474. case NL80211_IFTYPE_MONITOR:
  1475. if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
  1476. vif = &sdata->vif;
  1477. break;
  1478. }
  1479. sdata = rcu_dereference(local->monitor_sdata);
  1480. if (sdata && ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF)) {
  1481. vif = &sdata->vif;
  1482. info->hw_queue =
  1483. vif->hw_queue[skb_get_queue_mapping(skb)];
  1484. } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
  1485. ieee80211_purge_tx_queue(&local->hw, skbs);
  1486. return true;
  1487. } else
  1488. vif = NULL;
  1489. break;
  1490. case NL80211_IFTYPE_AP_VLAN:
  1491. sdata = container_of(sdata->bss,
  1492. struct ieee80211_sub_if_data, u.ap);
  1493. fallthrough;
  1494. default:
  1495. vif = &sdata->vif;
  1496. break;
  1497. }
  1498. result = ieee80211_tx_frags(local, vif, sta, skbs, txpending);
  1499. WARN_ON_ONCE(!skb_queue_empty(skbs));
  1500. return result;
  1501. }
  1502. /*
  1503. * Invoke TX handlers, return 0 on success and non-zero if the
  1504. * frame was dropped or queued.
  1505. *
  1506. * The handlers are split into an early and late part. The latter is everything
  1507. * that can be sensitive to reordering, and will be deferred to after packets
  1508. * are dequeued from the intermediate queues (when they are enabled).
  1509. */
  1510. static int invoke_tx_handlers_early(struct ieee80211_tx_data *tx)
  1511. {
  1512. ieee80211_tx_result res = TX_DROP;
  1513. #define CALL_TXH(txh) \
  1514. do { \
  1515. res = txh(tx); \
  1516. if (res != TX_CONTINUE) \
  1517. goto txh_done; \
  1518. } while (0)
  1519. CALL_TXH(ieee80211_tx_h_dynamic_ps);
  1520. CALL_TXH(ieee80211_tx_h_check_assoc);
  1521. CALL_TXH(ieee80211_tx_h_ps_buf);
  1522. CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
  1523. CALL_TXH(ieee80211_tx_h_select_key);
  1524. txh_done:
  1525. if (unlikely(res == TX_DROP)) {
  1526. I802_DEBUG_INC(tx->local->tx_handlers_drop);
  1527. if (tx->skb)
  1528. ieee80211_free_txskb(&tx->local->hw, tx->skb);
  1529. else
  1530. ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
  1531. return -1;
  1532. } else if (unlikely(res == TX_QUEUED)) {
  1533. I802_DEBUG_INC(tx->local->tx_handlers_queued);
  1534. return -1;
  1535. }
  1536. return 0;
  1537. }
  1538. /*
  1539. * Late handlers can be called while the sta lock is held. Handlers that can
  1540. * cause packets to be generated will cause deadlock!
  1541. */
  1542. static int invoke_tx_handlers_late(struct ieee80211_tx_data *tx)
  1543. {
  1544. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  1545. ieee80211_tx_result res = TX_CONTINUE;
  1546. if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
  1547. CALL_TXH(ieee80211_tx_h_rate_ctrl);
  1548. if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
  1549. __skb_queue_tail(&tx->skbs, tx->skb);
  1550. tx->skb = NULL;
  1551. goto txh_done;
  1552. }
  1553. CALL_TXH(ieee80211_tx_h_michael_mic_add);
  1554. CALL_TXH(ieee80211_tx_h_sequence);
  1555. CALL_TXH(ieee80211_tx_h_fragment);
  1556. /* handlers after fragment must be aware of tx info fragmentation! */
  1557. CALL_TXH(ieee80211_tx_h_stats);
  1558. CALL_TXH(ieee80211_tx_h_encrypt);
  1559. if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
  1560. CALL_TXH(ieee80211_tx_h_calculate_duration);
  1561. #undef CALL_TXH
  1562. txh_done:
  1563. if (unlikely(res == TX_DROP)) {
  1564. I802_DEBUG_INC(tx->local->tx_handlers_drop);
  1565. if (tx->skb)
  1566. ieee80211_free_txskb(&tx->local->hw, tx->skb);
  1567. else
  1568. ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
  1569. return -1;
  1570. } else if (unlikely(res == TX_QUEUED)) {
  1571. I802_DEBUG_INC(tx->local->tx_handlers_queued);
  1572. return -1;
  1573. }
  1574. return 0;
  1575. }
  1576. static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
  1577. {
  1578. int r = invoke_tx_handlers_early(tx);
  1579. if (r)
  1580. return r;
  1581. return invoke_tx_handlers_late(tx);
  1582. }
  1583. bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
  1584. struct ieee80211_vif *vif, struct sk_buff *skb,
  1585. int band, struct ieee80211_sta **sta)
  1586. {
  1587. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1588. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1589. struct ieee80211_tx_data tx;
  1590. struct sk_buff *skb2;
  1591. if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
  1592. return false;
  1593. info->band = band;
  1594. info->control.vif = vif;
  1595. info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
  1596. if (invoke_tx_handlers(&tx))
  1597. return false;
  1598. if (sta) {
  1599. if (tx.sta)
  1600. *sta = &tx.sta->sta;
  1601. else
  1602. *sta = NULL;
  1603. }
  1604. /* this function isn't suitable for fragmented data frames */
  1605. skb2 = __skb_dequeue(&tx.skbs);
  1606. if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
  1607. ieee80211_free_txskb(hw, skb2);
  1608. ieee80211_purge_tx_queue(hw, &tx.skbs);
  1609. return false;
  1610. }
  1611. return true;
  1612. }
  1613. EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
  1614. /*
  1615. * Returns false if the frame couldn't be transmitted but was queued instead.
  1616. */
  1617. static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
  1618. struct sta_info *sta, struct sk_buff *skb,
  1619. bool txpending)
  1620. {
  1621. struct ieee80211_local *local = sdata->local;
  1622. struct ieee80211_tx_data tx;
  1623. ieee80211_tx_result res_prepare;
  1624. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1625. bool result = true;
  1626. if (unlikely(skb->len < 10)) {
  1627. dev_kfree_skb(skb);
  1628. return true;
  1629. }
  1630. /* initialises tx */
  1631. res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
  1632. if (unlikely(res_prepare == TX_DROP)) {
  1633. ieee80211_free_txskb(&local->hw, skb);
  1634. return true;
  1635. } else if (unlikely(res_prepare == TX_QUEUED)) {
  1636. return true;
  1637. }
  1638. /* set up hw_queue value early */
  1639. if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
  1640. !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
  1641. info->hw_queue =
  1642. sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
  1643. if (invoke_tx_handlers_early(&tx))
  1644. return true;
  1645. if (ieee80211_queue_skb(local, sdata, tx.sta, tx.skb))
  1646. return true;
  1647. if (!invoke_tx_handlers_late(&tx))
  1648. result = __ieee80211_tx(local, &tx.skbs, tx.sta, txpending);
  1649. return result;
  1650. }
  1651. /* device xmit handlers */
  1652. enum ieee80211_encrypt {
  1653. ENCRYPT_NO,
  1654. ENCRYPT_MGMT,
  1655. ENCRYPT_DATA,
  1656. };
  1657. static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
  1658. struct sk_buff *skb,
  1659. int head_need,
  1660. enum ieee80211_encrypt encrypt)
  1661. {
  1662. struct ieee80211_local *local = sdata->local;
  1663. bool enc_tailroom;
  1664. int tail_need = 0;
  1665. enc_tailroom = encrypt == ENCRYPT_MGMT ||
  1666. (encrypt == ENCRYPT_DATA &&
  1667. sdata->crypto_tx_tailroom_needed_cnt);
  1668. if (enc_tailroom) {
  1669. tail_need = IEEE80211_ENCRYPT_TAILROOM;
  1670. tail_need -= skb_tailroom(skb);
  1671. tail_need = max_t(int, tail_need, 0);
  1672. }
  1673. if (skb_cloned(skb) &&
  1674. (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
  1675. !skb_clone_writable(skb, ETH_HLEN) || enc_tailroom))
  1676. I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
  1677. else if (head_need || tail_need)
  1678. I802_DEBUG_INC(local->tx_expand_skb_head);
  1679. else
  1680. return 0;
  1681. if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
  1682. wiphy_debug(local->hw.wiphy,
  1683. "failed to reallocate TX buffer\n");
  1684. return -ENOMEM;
  1685. }
  1686. return 0;
  1687. }
  1688. void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
  1689. struct sta_info *sta, struct sk_buff *skb)
  1690. {
  1691. struct ieee80211_local *local = sdata->local;
  1692. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1693. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1694. int headroom;
  1695. enum ieee80211_encrypt encrypt;
  1696. if (info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)
  1697. encrypt = ENCRYPT_NO;
  1698. else if (ieee80211_is_mgmt(hdr->frame_control))
  1699. encrypt = ENCRYPT_MGMT;
  1700. else
  1701. encrypt = ENCRYPT_DATA;
  1702. headroom = local->tx_headroom;
  1703. if (encrypt != ENCRYPT_NO)
  1704. headroom += IEEE80211_ENCRYPT_HEADROOM;
  1705. headroom -= skb_headroom(skb);
  1706. headroom = max_t(int, 0, headroom);
  1707. if (ieee80211_skb_resize(sdata, skb, headroom, encrypt)) {
  1708. ieee80211_free_txskb(&local->hw, skb);
  1709. return;
  1710. }
  1711. /* reload after potential resize */
  1712. hdr = (struct ieee80211_hdr *) skb->data;
  1713. info->control.vif = &sdata->vif;
  1714. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  1715. if (ieee80211_is_data(hdr->frame_control) &&
  1716. is_unicast_ether_addr(hdr->addr1)) {
  1717. if (mesh_nexthop_resolve(sdata, skb))
  1718. return; /* skb queued: don't free */
  1719. } else {
  1720. ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
  1721. }
  1722. }
  1723. ieee80211_set_qos_hdr(sdata, skb);
  1724. ieee80211_tx(sdata, sta, skb, false);
  1725. }
  1726. static bool ieee80211_validate_radiotap_len(struct sk_buff *skb)
  1727. {
  1728. struct ieee80211_radiotap_header *rthdr =
  1729. (struct ieee80211_radiotap_header *)skb->data;
  1730. /* check for not even having the fixed radiotap header part */
  1731. if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
  1732. return false; /* too short to be possibly valid */
  1733. /* is it a header version we can trust to find length from? */
  1734. if (unlikely(rthdr->it_version))
  1735. return false; /* only version 0 is supported */
  1736. /* does the skb contain enough to deliver on the alleged length? */
  1737. if (unlikely(skb->len < ieee80211_get_radiotap_len(skb->data)))
  1738. return false; /* skb too short for claimed rt header extent */
  1739. return true;
  1740. }
  1741. bool ieee80211_parse_tx_radiotap(struct sk_buff *skb,
  1742. struct net_device *dev)
  1743. {
  1744. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1745. struct ieee80211_radiotap_iterator iterator;
  1746. struct ieee80211_radiotap_header *rthdr =
  1747. (struct ieee80211_radiotap_header *) skb->data;
  1748. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1749. int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
  1750. NULL);
  1751. u16 txflags;
  1752. u16 rate = 0;
  1753. bool rate_found = false;
  1754. u8 rate_retries = 0;
  1755. u16 rate_flags = 0;
  1756. u8 mcs_known, mcs_flags, mcs_bw;
  1757. u16 vht_known;
  1758. u8 vht_mcs = 0, vht_nss = 0;
  1759. int i;
  1760. if (!ieee80211_validate_radiotap_len(skb))
  1761. return false;
  1762. info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  1763. IEEE80211_TX_CTL_DONTFRAG;
  1764. /*
  1765. * for every radiotap entry that is present
  1766. * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
  1767. * entries present, or -EINVAL on error)
  1768. */
  1769. while (!ret) {
  1770. ret = ieee80211_radiotap_iterator_next(&iterator);
  1771. if (ret)
  1772. continue;
  1773. /* see if this argument is something we can use */
  1774. switch (iterator.this_arg_index) {
  1775. /*
  1776. * You must take care when dereferencing iterator.this_arg
  1777. * for multibyte types... the pointer is not aligned. Use
  1778. * get_unaligned((type *)iterator.this_arg) to dereference
  1779. * iterator.this_arg for type "type" safely on all arches.
  1780. */
  1781. case IEEE80211_RADIOTAP_FLAGS:
  1782. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
  1783. /*
  1784. * this indicates that the skb we have been
  1785. * handed has the 32-bit FCS CRC at the end...
  1786. * we should react to that by snipping it off
  1787. * because it will be recomputed and added
  1788. * on transmission
  1789. */
  1790. if (skb->len < (iterator._max_length + FCS_LEN))
  1791. return false;
  1792. skb_trim(skb, skb->len - FCS_LEN);
  1793. }
  1794. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
  1795. info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1796. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
  1797. info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
  1798. break;
  1799. case IEEE80211_RADIOTAP_TX_FLAGS:
  1800. txflags = get_unaligned_le16(iterator.this_arg);
  1801. if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
  1802. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  1803. if (txflags & IEEE80211_RADIOTAP_F_TX_NOSEQNO)
  1804. info->control.flags |= IEEE80211_TX_CTRL_NO_SEQNO;
  1805. if (txflags & IEEE80211_RADIOTAP_F_TX_ORDER)
  1806. info->control.flags |=
  1807. IEEE80211_TX_CTRL_DONT_REORDER;
  1808. break;
  1809. case IEEE80211_RADIOTAP_RATE:
  1810. rate = *iterator.this_arg;
  1811. rate_flags = 0;
  1812. rate_found = true;
  1813. break;
  1814. case IEEE80211_RADIOTAP_ANTENNA:
  1815. /* this can appear multiple times, keep a bitmap */
  1816. info->control.antennas |= BIT(*iterator.this_arg);
  1817. break;
  1818. case IEEE80211_RADIOTAP_DATA_RETRIES:
  1819. rate_retries = *iterator.this_arg;
  1820. break;
  1821. case IEEE80211_RADIOTAP_MCS:
  1822. mcs_known = iterator.this_arg[0];
  1823. mcs_flags = iterator.this_arg[1];
  1824. if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
  1825. break;
  1826. rate_found = true;
  1827. rate = iterator.this_arg[2];
  1828. rate_flags = IEEE80211_TX_RC_MCS;
  1829. if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
  1830. mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
  1831. rate_flags |= IEEE80211_TX_RC_SHORT_GI;
  1832. mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
  1833. if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
  1834. mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
  1835. rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  1836. if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_FEC &&
  1837. mcs_flags & IEEE80211_RADIOTAP_MCS_FEC_LDPC)
  1838. info->flags |= IEEE80211_TX_CTL_LDPC;
  1839. if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_STBC) {
  1840. u8 stbc = u8_get_bits(mcs_flags,
  1841. IEEE80211_RADIOTAP_MCS_STBC_MASK);
  1842. info->flags |=
  1843. u32_encode_bits(stbc,
  1844. IEEE80211_TX_CTL_STBC);
  1845. }
  1846. break;
  1847. case IEEE80211_RADIOTAP_VHT:
  1848. vht_known = get_unaligned_le16(iterator.this_arg);
  1849. rate_found = true;
  1850. rate_flags = IEEE80211_TX_RC_VHT_MCS;
  1851. if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
  1852. (iterator.this_arg[2] &
  1853. IEEE80211_RADIOTAP_VHT_FLAG_SGI))
  1854. rate_flags |= IEEE80211_TX_RC_SHORT_GI;
  1855. if (vht_known &
  1856. IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
  1857. if (iterator.this_arg[3] == 1)
  1858. rate_flags |=
  1859. IEEE80211_TX_RC_40_MHZ_WIDTH;
  1860. else if (iterator.this_arg[3] == 4)
  1861. rate_flags |=
  1862. IEEE80211_TX_RC_80_MHZ_WIDTH;
  1863. else if (iterator.this_arg[3] == 11)
  1864. rate_flags |=
  1865. IEEE80211_TX_RC_160_MHZ_WIDTH;
  1866. }
  1867. vht_mcs = iterator.this_arg[4] >> 4;
  1868. if (vht_mcs > 11)
  1869. vht_mcs = 0;
  1870. vht_nss = iterator.this_arg[4] & 0xF;
  1871. if (!vht_nss || vht_nss > 8)
  1872. vht_nss = 1;
  1873. break;
  1874. /*
  1875. * Please update the file
  1876. * Documentation/networking/mac80211-injection.rst
  1877. * when parsing new fields here.
  1878. */
  1879. default:
  1880. break;
  1881. }
  1882. }
  1883. if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
  1884. return false;
  1885. if (rate_found) {
  1886. struct ieee80211_supported_band *sband =
  1887. local->hw.wiphy->bands[info->band];
  1888. info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
  1889. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  1890. info->control.rates[i].idx = -1;
  1891. info->control.rates[i].flags = 0;
  1892. info->control.rates[i].count = 0;
  1893. }
  1894. if (rate_flags & IEEE80211_TX_RC_MCS) {
  1895. /* reset antennas if not enough */
  1896. if (IEEE80211_HT_MCS_CHAINS(rate) >
  1897. hweight8(info->control.antennas))
  1898. info->control.antennas = 0;
  1899. info->control.rates[0].idx = rate;
  1900. } else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
  1901. /* reset antennas if not enough */
  1902. if (vht_nss > hweight8(info->control.antennas))
  1903. info->control.antennas = 0;
  1904. ieee80211_rate_set_vht(info->control.rates, vht_mcs,
  1905. vht_nss);
  1906. } else if (sband) {
  1907. for (i = 0; i < sband->n_bitrates; i++) {
  1908. if (rate * 5 != sband->bitrates[i].bitrate)
  1909. continue;
  1910. info->control.rates[0].idx = i;
  1911. break;
  1912. }
  1913. }
  1914. if (info->control.rates[0].idx < 0)
  1915. info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
  1916. info->control.rates[0].flags = rate_flags;
  1917. info->control.rates[0].count = min_t(u8, rate_retries + 1,
  1918. local->hw.max_rate_tries);
  1919. }
  1920. return true;
  1921. }
  1922. netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
  1923. struct net_device *dev)
  1924. {
  1925. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1926. struct ieee80211_chanctx_conf *chanctx_conf;
  1927. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1928. struct ieee80211_hdr *hdr;
  1929. struct ieee80211_sub_if_data *tmp_sdata, *sdata;
  1930. struct cfg80211_chan_def *chandef;
  1931. u16 len_rthdr;
  1932. int hdrlen;
  1933. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1934. if (unlikely(!ieee80211_sdata_running(sdata)))
  1935. goto fail;
  1936. memset(info, 0, sizeof(*info));
  1937. info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
  1938. IEEE80211_TX_CTL_INJECTED;
  1939. /* Sanity-check the length of the radiotap header */
  1940. if (!ieee80211_validate_radiotap_len(skb))
  1941. goto fail;
  1942. /* we now know there is a radiotap header with a length we can use */
  1943. len_rthdr = ieee80211_get_radiotap_len(skb->data);
  1944. /*
  1945. * fix up the pointers accounting for the radiotap
  1946. * header still being in there. We are being given
  1947. * a precooked IEEE80211 header so no need for
  1948. * normal processing
  1949. */
  1950. skb_set_mac_header(skb, len_rthdr);
  1951. /*
  1952. * these are just fixed to the end of the rt area since we
  1953. * don't have any better information and at this point, nobody cares
  1954. */
  1955. skb_set_network_header(skb, len_rthdr);
  1956. skb_set_transport_header(skb, len_rthdr);
  1957. if (skb->len < len_rthdr + 2)
  1958. goto fail;
  1959. hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
  1960. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1961. if (skb->len < len_rthdr + hdrlen)
  1962. goto fail;
  1963. /*
  1964. * Initialize skb->protocol if the injected frame is a data frame
  1965. * carrying a rfc1042 header
  1966. */
  1967. if (ieee80211_is_data(hdr->frame_control) &&
  1968. skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
  1969. u8 *payload = (u8 *)hdr + hdrlen;
  1970. if (ether_addr_equal(payload, rfc1042_header))
  1971. skb->protocol = cpu_to_be16((payload[6] << 8) |
  1972. payload[7]);
  1973. }
  1974. rcu_read_lock();
  1975. /*
  1976. * We process outgoing injected frames that have a local address
  1977. * we handle as though they are non-injected frames.
  1978. * This code here isn't entirely correct, the local MAC address
  1979. * isn't always enough to find the interface to use; for proper
  1980. * VLAN support we have an nl80211-based mechanism.
  1981. *
  1982. * This is necessary, for example, for old hostapd versions that
  1983. * don't use nl80211-based management TX/RX.
  1984. */
  1985. list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
  1986. if (!ieee80211_sdata_running(tmp_sdata))
  1987. continue;
  1988. if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  1989. tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1990. continue;
  1991. if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
  1992. sdata = tmp_sdata;
  1993. break;
  1994. }
  1995. }
  1996. chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
  1997. if (!chanctx_conf) {
  1998. tmp_sdata = rcu_dereference(local->monitor_sdata);
  1999. if (tmp_sdata)
  2000. chanctx_conf =
  2001. rcu_dereference(tmp_sdata->vif.bss_conf.chanctx_conf);
  2002. }
  2003. if (chanctx_conf)
  2004. chandef = &chanctx_conf->def;
  2005. else
  2006. goto fail_rcu;
  2007. /*
  2008. * If driver/HW supports IEEE80211_CHAN_CAN_MONITOR we still
  2009. * shouldn't transmit on disabled channels.
  2010. */
  2011. if (!cfg80211_chandef_usable(local->hw.wiphy, chandef,
  2012. IEEE80211_CHAN_DISABLED))
  2013. goto fail_rcu;
  2014. /*
  2015. * Frame injection is not allowed if beaconing is not allowed
  2016. * or if we need radar detection. Beaconing is usually not allowed when
  2017. * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
  2018. * Passive scan is also used in world regulatory domains where
  2019. * your country is not known and as such it should be treated as
  2020. * NO TX unless the channel is explicitly allowed in which case
  2021. * your current regulatory domain would not have the passive scan
  2022. * flag.
  2023. *
  2024. * Since AP mode uses monitor interfaces to inject/TX management
  2025. * frames we can make AP mode the exception to this rule once it
  2026. * supports radar detection as its implementation can deal with
  2027. * radar detection by itself. We can do that later by adding a
  2028. * monitor flag interfaces used for AP support.
  2029. */
  2030. if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
  2031. sdata->vif.type))
  2032. goto fail_rcu;
  2033. info->band = chandef->chan->band;
  2034. /* Initialize skb->priority according to frame type and TID class,
  2035. * with respect to the sub interface that the frame will actually
  2036. * be transmitted on. If the DONT_REORDER flag is set, the original
  2037. * skb-priority is preserved to assure frames injected with this
  2038. * flag are not reordered relative to each other.
  2039. */
  2040. ieee80211_select_queue_80211(sdata, skb, hdr);
  2041. skb_set_queue_mapping(skb, ieee80211_ac_from_tid(skb->priority));
  2042. /*
  2043. * Process the radiotap header. This will now take into account the
  2044. * selected chandef above to accurately set injection rates and
  2045. * retransmissions.
  2046. */
  2047. if (!ieee80211_parse_tx_radiotap(skb, dev))
  2048. goto fail_rcu;
  2049. /* remove the injection radiotap header */
  2050. skb_pull(skb, len_rthdr);
  2051. ieee80211_xmit(sdata, NULL, skb);
  2052. rcu_read_unlock();
  2053. return NETDEV_TX_OK;
  2054. fail_rcu:
  2055. rcu_read_unlock();
  2056. fail:
  2057. dev_kfree_skb(skb);
  2058. return NETDEV_TX_OK; /* meaning, we dealt with the skb */
  2059. }
  2060. static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
  2061. {
  2062. u16 ethertype = (skb->data[12] << 8) | skb->data[13];
  2063. return ethertype == ETH_P_TDLS &&
  2064. skb->len > 14 &&
  2065. skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
  2066. }
  2067. int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
  2068. struct sk_buff *skb,
  2069. struct sta_info **sta_out)
  2070. {
  2071. struct sta_info *sta;
  2072. switch (sdata->vif.type) {
  2073. case NL80211_IFTYPE_AP_VLAN:
  2074. sta = rcu_dereference(sdata->u.vlan.sta);
  2075. if (sta) {
  2076. *sta_out = sta;
  2077. return 0;
  2078. } else if (sdata->wdev.use_4addr) {
  2079. return -ENOLINK;
  2080. }
  2081. fallthrough;
  2082. case NL80211_IFTYPE_AP:
  2083. case NL80211_IFTYPE_OCB:
  2084. case NL80211_IFTYPE_ADHOC:
  2085. if (is_multicast_ether_addr(skb->data)) {
  2086. *sta_out = ERR_PTR(-ENOENT);
  2087. return 0;
  2088. }
  2089. sta = sta_info_get_bss(sdata, skb->data);
  2090. break;
  2091. #ifdef CONFIG_MAC80211_MESH
  2092. case NL80211_IFTYPE_MESH_POINT:
  2093. /* determined much later */
  2094. *sta_out = NULL;
  2095. return 0;
  2096. #endif
  2097. case NL80211_IFTYPE_STATION:
  2098. if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
  2099. sta = sta_info_get(sdata, skb->data);
  2100. if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
  2101. if (test_sta_flag(sta,
  2102. WLAN_STA_TDLS_PEER_AUTH)) {
  2103. *sta_out = sta;
  2104. return 0;
  2105. }
  2106. /*
  2107. * TDLS link during setup - throw out frames to
  2108. * peer. Allow TDLS-setup frames to unauthorized
  2109. * peers for the special case of a link teardown
  2110. * after a TDLS sta is removed due to being
  2111. * unreachable.
  2112. */
  2113. if (!ieee80211_is_tdls_setup(skb))
  2114. return -EINVAL;
  2115. }
  2116. }
  2117. sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
  2118. if (!sta)
  2119. return -ENOLINK;
  2120. break;
  2121. default:
  2122. return -EINVAL;
  2123. }
  2124. *sta_out = sta ?: ERR_PTR(-ENOENT);
  2125. return 0;
  2126. }
  2127. static u16 ieee80211_store_ack_skb(struct ieee80211_local *local,
  2128. struct sk_buff *skb,
  2129. u32 *info_flags,
  2130. u64 *cookie)
  2131. {
  2132. struct sk_buff *ack_skb;
  2133. u16 info_id = 0;
  2134. if (skb->sk)
  2135. ack_skb = skb_clone_sk(skb);
  2136. else
  2137. ack_skb = skb_clone(skb, GFP_ATOMIC);
  2138. if (ack_skb) {
  2139. unsigned long flags;
  2140. int id;
  2141. spin_lock_irqsave(&local->ack_status_lock, flags);
  2142. id = idr_alloc(&local->ack_status_frames, ack_skb,
  2143. 1, 0x2000, GFP_ATOMIC);
  2144. spin_unlock_irqrestore(&local->ack_status_lock, flags);
  2145. if (id >= 0) {
  2146. info_id = id;
  2147. *info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
  2148. if (cookie) {
  2149. *cookie = ieee80211_mgmt_tx_cookie(local);
  2150. IEEE80211_SKB_CB(ack_skb)->ack.cookie = *cookie;
  2151. }
  2152. } else {
  2153. kfree_skb(ack_skb);
  2154. }
  2155. }
  2156. return info_id;
  2157. }
  2158. /**
  2159. * ieee80211_build_hdr - build 802.11 header in the given frame
  2160. * @sdata: virtual interface to build the header for
  2161. * @skb: the skb to build the header in
  2162. * @info_flags: skb flags to set
  2163. * @sta: the station pointer
  2164. * @ctrl_flags: info control flags to set
  2165. * @cookie: cookie pointer to fill (if not %NULL)
  2166. *
  2167. * This function takes the skb with 802.3 header and reformats the header to
  2168. * the appropriate IEEE 802.11 header based on which interface the packet is
  2169. * being transmitted on.
  2170. *
  2171. * Note that this function also takes care of the TX status request and
  2172. * potential unsharing of the SKB - this needs to be interleaved with the
  2173. * header building.
  2174. *
  2175. * The function requires the read-side RCU lock held
  2176. *
  2177. * Returns: the (possibly reallocated) skb or an ERR_PTR() code
  2178. */
  2179. static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
  2180. struct sk_buff *skb, u32 info_flags,
  2181. struct sta_info *sta, u32 ctrl_flags,
  2182. u64 *cookie)
  2183. {
  2184. struct ieee80211_local *local = sdata->local;
  2185. struct ieee80211_tx_info *info;
  2186. int head_need;
  2187. u16 ethertype, hdrlen, meshhdrlen = 0;
  2188. __le16 fc;
  2189. struct ieee80211_hdr hdr;
  2190. struct ieee80211s_hdr mesh_hdr __maybe_unused;
  2191. struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
  2192. const u8 *encaps_data;
  2193. int encaps_len, skip_header_bytes;
  2194. bool wme_sta = false, authorized = false;
  2195. bool tdls_peer;
  2196. bool multicast;
  2197. u16 info_id = 0;
  2198. struct ieee80211_chanctx_conf *chanctx_conf = NULL;
  2199. enum nl80211_band band;
  2200. int ret;
  2201. u8 link_id = u32_get_bits(ctrl_flags, IEEE80211_TX_CTRL_MLO_LINK);
  2202. if (IS_ERR(sta))
  2203. sta = NULL;
  2204. #ifdef CONFIG_MAC80211_DEBUGFS
  2205. if (local->force_tx_status)
  2206. info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
  2207. #endif
  2208. /* convert Ethernet header to proper 802.11 header (based on
  2209. * operation mode) */
  2210. ethertype = (skb->data[12] << 8) | skb->data[13];
  2211. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
  2212. if (!ieee80211_vif_is_mld(&sdata->vif))
  2213. chanctx_conf =
  2214. rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
  2215. switch (sdata->vif.type) {
  2216. case NL80211_IFTYPE_AP_VLAN:
  2217. if (sdata->wdev.use_4addr) {
  2218. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  2219. /* RA TA DA SA */
  2220. memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
  2221. memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
  2222. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  2223. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  2224. hdrlen = 30;
  2225. authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
  2226. wme_sta = sta->sta.wme;
  2227. }
  2228. if (!ieee80211_vif_is_mld(&sdata->vif)) {
  2229. struct ieee80211_sub_if_data *ap_sdata;
  2230. /* override chanctx_conf from AP (we don't have one) */
  2231. ap_sdata = container_of(sdata->bss,
  2232. struct ieee80211_sub_if_data,
  2233. u.ap);
  2234. chanctx_conf =
  2235. rcu_dereference(ap_sdata->vif.bss_conf.chanctx_conf);
  2236. }
  2237. if (sdata->wdev.use_4addr)
  2238. break;
  2239. fallthrough;
  2240. case NL80211_IFTYPE_AP:
  2241. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  2242. /* DA BSSID SA */
  2243. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  2244. if (ieee80211_vif_is_mld(&sdata->vif) && sta && !sta->sta.mlo) {
  2245. struct ieee80211_link_data *link;
  2246. link_id = sta->deflink.link_id;
  2247. link = rcu_dereference(sdata->link[link_id]);
  2248. if (WARN_ON(!link)) {
  2249. ret = -ENOLINK;
  2250. goto free;
  2251. }
  2252. memcpy(hdr.addr2, link->conf->addr, ETH_ALEN);
  2253. } else if (link_id == IEEE80211_LINK_UNSPECIFIED ||
  2254. (sta && sta->sta.mlo)) {
  2255. memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
  2256. } else {
  2257. struct ieee80211_bss_conf *conf;
  2258. conf = rcu_dereference(sdata->vif.link_conf[link_id]);
  2259. if (unlikely(!conf)) {
  2260. ret = -ENOLINK;
  2261. goto free;
  2262. }
  2263. memcpy(hdr.addr2, conf->addr, ETH_ALEN);
  2264. }
  2265. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  2266. hdrlen = 24;
  2267. break;
  2268. #ifdef CONFIG_MAC80211_MESH
  2269. case NL80211_IFTYPE_MESH_POINT:
  2270. if (!is_multicast_ether_addr(skb->data)) {
  2271. struct sta_info *next_hop;
  2272. bool mpp_lookup = true;
  2273. mpath = mesh_path_lookup(sdata, skb->data);
  2274. if (mpath) {
  2275. mpp_lookup = false;
  2276. next_hop = rcu_dereference(mpath->next_hop);
  2277. if (!next_hop ||
  2278. !(mpath->flags & (MESH_PATH_ACTIVE |
  2279. MESH_PATH_RESOLVING)))
  2280. mpp_lookup = true;
  2281. }
  2282. if (mpp_lookup) {
  2283. mppath = mpp_path_lookup(sdata, skb->data);
  2284. if (mppath)
  2285. mppath->exp_time = jiffies;
  2286. }
  2287. if (mppath && mpath)
  2288. mesh_path_del(sdata, mpath->dst);
  2289. }
  2290. /*
  2291. * Use address extension if it is a packet from
  2292. * another interface or if we know the destination
  2293. * is being proxied by a portal (i.e. portal address
  2294. * differs from proxied address)
  2295. */
  2296. if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
  2297. !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
  2298. hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
  2299. skb->data, skb->data + ETH_ALEN);
  2300. meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
  2301. NULL, NULL);
  2302. } else {
  2303. /* DS -> MBSS (802.11-2012 13.11.3.3).
  2304. * For unicast with unknown forwarding information,
  2305. * destination might be in the MBSS or if that fails
  2306. * forwarded to another mesh gate. In either case
  2307. * resolution will be handled in ieee80211_xmit(), so
  2308. * leave the original DA. This also works for mcast */
  2309. const u8 *mesh_da = skb->data;
  2310. if (mppath)
  2311. mesh_da = mppath->mpp;
  2312. else if (mpath)
  2313. mesh_da = mpath->dst;
  2314. hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
  2315. mesh_da, sdata->vif.addr);
  2316. if (is_multicast_ether_addr(mesh_da))
  2317. /* DA TA mSA AE:SA */
  2318. meshhdrlen = ieee80211_new_mesh_header(
  2319. sdata, &mesh_hdr,
  2320. skb->data + ETH_ALEN, NULL);
  2321. else
  2322. /* RA TA mDA mSA AE:DA SA */
  2323. meshhdrlen = ieee80211_new_mesh_header(
  2324. sdata, &mesh_hdr, skb->data,
  2325. skb->data + ETH_ALEN);
  2326. }
  2327. /* For injected frames, fill RA right away as nexthop lookup
  2328. * will be skipped.
  2329. */
  2330. if ((ctrl_flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP) &&
  2331. is_zero_ether_addr(hdr.addr1))
  2332. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  2333. break;
  2334. #endif
  2335. case NL80211_IFTYPE_STATION:
  2336. /* we already did checks when looking up the RA STA */
  2337. tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
  2338. if (tdls_peer) {
  2339. /* For TDLS only one link can be valid with peer STA */
  2340. int tdls_link_id = ieee80211_tdls_sta_link_id(sta);
  2341. struct ieee80211_link_data *link;
  2342. /* DA SA BSSID */
  2343. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  2344. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  2345. link = rcu_dereference(sdata->link[tdls_link_id]);
  2346. if (WARN_ON_ONCE(!link)) {
  2347. ret = -EINVAL;
  2348. goto free;
  2349. }
  2350. memcpy(hdr.addr3, link->u.mgd.bssid, ETH_ALEN);
  2351. hdrlen = 24;
  2352. } else if (sdata->u.mgd.use_4addr &&
  2353. cpu_to_be16(ethertype) != sdata->control_port_protocol) {
  2354. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
  2355. IEEE80211_FCTL_TODS);
  2356. /* RA TA DA SA */
  2357. memcpy(hdr.addr1, sdata->deflink.u.mgd.bssid, ETH_ALEN);
  2358. memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
  2359. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  2360. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  2361. hdrlen = 30;
  2362. } else {
  2363. fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
  2364. /* BSSID SA DA */
  2365. memcpy(hdr.addr1, sdata->vif.cfg.ap_addr, ETH_ALEN);
  2366. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  2367. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  2368. hdrlen = 24;
  2369. }
  2370. break;
  2371. case NL80211_IFTYPE_OCB:
  2372. /* DA SA BSSID */
  2373. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  2374. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  2375. eth_broadcast_addr(hdr.addr3);
  2376. hdrlen = 24;
  2377. break;
  2378. case NL80211_IFTYPE_ADHOC:
  2379. /* DA SA BSSID */
  2380. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  2381. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  2382. memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
  2383. hdrlen = 24;
  2384. break;
  2385. default:
  2386. ret = -EINVAL;
  2387. goto free;
  2388. }
  2389. if (!chanctx_conf) {
  2390. if (!ieee80211_vif_is_mld(&sdata->vif)) {
  2391. ret = -ENOTCONN;
  2392. goto free;
  2393. }
  2394. /* MLD transmissions must not rely on the band */
  2395. band = 0;
  2396. } else {
  2397. band = chanctx_conf->def.chan->band;
  2398. }
  2399. multicast = is_multicast_ether_addr(hdr.addr1);
  2400. /* sta is always NULL for mesh */
  2401. if (sta) {
  2402. authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
  2403. wme_sta = sta->sta.wme;
  2404. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2405. /* For mesh, the use of the QoS header is mandatory */
  2406. wme_sta = true;
  2407. }
  2408. /* receiver does QoS (which also means we do) use it */
  2409. if (wme_sta) {
  2410. fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  2411. hdrlen += 2;
  2412. }
  2413. /*
  2414. * Drop unicast frames to unauthorised stations unless they are
  2415. * EAPOL frames from the local station.
  2416. */
  2417. if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
  2418. (sdata->vif.type != NL80211_IFTYPE_OCB) &&
  2419. !multicast && !authorized &&
  2420. (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
  2421. !ieee80211_is_our_addr(sdata, skb->data + ETH_ALEN, NULL)))) {
  2422. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  2423. net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
  2424. sdata->name, hdr.addr1);
  2425. #endif
  2426. I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
  2427. ret = -EPERM;
  2428. goto free;
  2429. }
  2430. if (unlikely(!multicast &&
  2431. ((skb->sk &&
  2432. skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS) ||
  2433. ctrl_flags & IEEE80211_TX_CTL_REQ_TX_STATUS)))
  2434. info_id = ieee80211_store_ack_skb(local, skb, &info_flags,
  2435. cookie);
  2436. /*
  2437. * If the skb is shared we need to obtain our own copy.
  2438. */
  2439. skb = skb_share_check(skb, GFP_ATOMIC);
  2440. if (unlikely(!skb)) {
  2441. ret = -ENOMEM;
  2442. goto free;
  2443. }
  2444. hdr.frame_control = fc;
  2445. hdr.duration_id = 0;
  2446. hdr.seq_ctrl = 0;
  2447. skip_header_bytes = ETH_HLEN;
  2448. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  2449. encaps_data = bridge_tunnel_header;
  2450. encaps_len = sizeof(bridge_tunnel_header);
  2451. skip_header_bytes -= 2;
  2452. } else if (ethertype >= ETH_P_802_3_MIN) {
  2453. encaps_data = rfc1042_header;
  2454. encaps_len = sizeof(rfc1042_header);
  2455. skip_header_bytes -= 2;
  2456. } else {
  2457. encaps_data = NULL;
  2458. encaps_len = 0;
  2459. }
  2460. skb_pull(skb, skip_header_bytes);
  2461. head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
  2462. /*
  2463. * So we need to modify the skb header and hence need a copy of
  2464. * that. The head_need variable above doesn't, so far, include
  2465. * the needed header space that we don't need right away. If we
  2466. * can, then we don't reallocate right now but only after the
  2467. * frame arrives at the master device (if it does...)
  2468. *
  2469. * If we cannot, however, then we will reallocate to include all
  2470. * the ever needed space. Also, if we need to reallocate it anyway,
  2471. * make it big enough for everything we may ever need.
  2472. */
  2473. if (head_need > 0 || skb_cloned(skb)) {
  2474. head_need += IEEE80211_ENCRYPT_HEADROOM;
  2475. head_need += local->tx_headroom;
  2476. head_need = max_t(int, 0, head_need);
  2477. if (ieee80211_skb_resize(sdata, skb, head_need, ENCRYPT_DATA)) {
  2478. ieee80211_free_txskb(&local->hw, skb);
  2479. skb = NULL;
  2480. return ERR_PTR(-ENOMEM);
  2481. }
  2482. }
  2483. if (encaps_data)
  2484. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  2485. #ifdef CONFIG_MAC80211_MESH
  2486. if (meshhdrlen > 0)
  2487. memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
  2488. #endif
  2489. if (ieee80211_is_data_qos(fc)) {
  2490. __le16 *qos_control;
  2491. qos_control = skb_push(skb, 2);
  2492. memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
  2493. /*
  2494. * Maybe we could actually set some fields here, for now just
  2495. * initialise to zero to indicate no special operation.
  2496. */
  2497. *qos_control = 0;
  2498. } else
  2499. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  2500. skb_reset_mac_header(skb);
  2501. info = IEEE80211_SKB_CB(skb);
  2502. memset(info, 0, sizeof(*info));
  2503. info->flags = info_flags;
  2504. if (info_id) {
  2505. info->status_data = info_id;
  2506. info->status_data_idr = 1;
  2507. }
  2508. info->band = band;
  2509. if (likely(!cookie)) {
  2510. ctrl_flags |= u32_encode_bits(link_id,
  2511. IEEE80211_TX_CTRL_MLO_LINK);
  2512. } else {
  2513. unsigned int pre_conf_link_id;
  2514. /*
  2515. * ctrl_flags already have been set by
  2516. * ieee80211_tx_control_port(), here
  2517. * we just sanity check that
  2518. */
  2519. pre_conf_link_id = u32_get_bits(ctrl_flags,
  2520. IEEE80211_TX_CTRL_MLO_LINK);
  2521. if (pre_conf_link_id != link_id &&
  2522. link_id != IEEE80211_LINK_UNSPECIFIED) {
  2523. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  2524. net_info_ratelimited("%s: dropped frame to %pM with bad link ID request (%d vs. %d)\n",
  2525. sdata->name, hdr.addr1,
  2526. pre_conf_link_id, link_id);
  2527. #endif
  2528. ret = -EINVAL;
  2529. goto free;
  2530. }
  2531. }
  2532. info->control.flags = ctrl_flags;
  2533. return skb;
  2534. free:
  2535. kfree_skb(skb);
  2536. return ERR_PTR(ret);
  2537. }
  2538. /*
  2539. * fast-xmit overview
  2540. *
  2541. * The core idea of this fast-xmit is to remove per-packet checks by checking
  2542. * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
  2543. * checks that are needed to get the sta->fast_tx pointer assigned, after which
  2544. * much less work can be done per packet. For example, fragmentation must be
  2545. * disabled or the fast_tx pointer will not be set. All the conditions are seen
  2546. * in the code here.
  2547. *
  2548. * Once assigned, the fast_tx data structure also caches the per-packet 802.11
  2549. * header and other data to aid packet processing in ieee80211_xmit_fast().
  2550. *
  2551. * The most difficult part of this is that when any of these assumptions
  2552. * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
  2553. * ieee80211_check_fast_xmit() or friends) is required to reset the data,
  2554. * since the per-packet code no longer checks the conditions. This is reflected
  2555. * by the calls to these functions throughout the rest of the code, and must be
  2556. * maintained if any of the TX path checks change.
  2557. */
  2558. void ieee80211_check_fast_xmit(struct sta_info *sta)
  2559. {
  2560. struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
  2561. struct ieee80211_local *local = sta->local;
  2562. struct ieee80211_sub_if_data *sdata = sta->sdata;
  2563. struct ieee80211_hdr *hdr = (void *)build.hdr;
  2564. struct ieee80211_chanctx_conf *chanctx_conf;
  2565. __le16 fc;
  2566. if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
  2567. return;
  2568. if (ieee80211_vif_is_mesh(&sdata->vif))
  2569. mesh_fast_tx_flush_sta(sdata, sta);
  2570. /* Locking here protects both the pointer itself, and against concurrent
  2571. * invocations winning data access races to, e.g., the key pointer that
  2572. * is used.
  2573. * Without it, the invocation of this function right after the key
  2574. * pointer changes wouldn't be sufficient, as another CPU could access
  2575. * the pointer, then stall, and then do the cache update after the CPU
  2576. * that invalidated the key.
  2577. * With the locking, such scenarios cannot happen as the check for the
  2578. * key and the fast-tx assignment are done atomically, so the CPU that
  2579. * modifies the key will either wait or other one will see the key
  2580. * cleared/changed already.
  2581. */
  2582. spin_lock_bh(&sta->lock);
  2583. if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
  2584. !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
  2585. sdata->vif.type == NL80211_IFTYPE_STATION)
  2586. goto out;
  2587. if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED) || !sta->uploaded)
  2588. goto out;
  2589. if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
  2590. test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
  2591. test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
  2592. test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
  2593. goto out;
  2594. if (sdata->noack_map)
  2595. goto out;
  2596. /* fast-xmit doesn't handle fragmentation at all */
  2597. if (local->hw.wiphy->frag_threshold != (u32)-1 &&
  2598. !ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG))
  2599. goto out;
  2600. if (!ieee80211_vif_is_mld(&sdata->vif)) {
  2601. rcu_read_lock();
  2602. chanctx_conf =
  2603. rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
  2604. if (!chanctx_conf) {
  2605. rcu_read_unlock();
  2606. goto out;
  2607. }
  2608. build.band = chanctx_conf->def.chan->band;
  2609. rcu_read_unlock();
  2610. } else {
  2611. /* MLD transmissions must not rely on the band */
  2612. build.band = 0;
  2613. }
  2614. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
  2615. switch (sdata->vif.type) {
  2616. case NL80211_IFTYPE_ADHOC:
  2617. /* DA SA BSSID */
  2618. build.da_offs = offsetof(struct ieee80211_hdr, addr1);
  2619. build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
  2620. memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
  2621. build.hdr_len = 24;
  2622. break;
  2623. case NL80211_IFTYPE_STATION:
  2624. if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
  2625. /* For TDLS only one link can be valid with peer STA */
  2626. int tdls_link_id = ieee80211_tdls_sta_link_id(sta);
  2627. struct ieee80211_link_data *link;
  2628. /* DA SA BSSID */
  2629. build.da_offs = offsetof(struct ieee80211_hdr, addr1);
  2630. build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
  2631. rcu_read_lock();
  2632. link = rcu_dereference(sdata->link[tdls_link_id]);
  2633. if (!WARN_ON_ONCE(!link))
  2634. memcpy(hdr->addr3, link->u.mgd.bssid, ETH_ALEN);
  2635. rcu_read_unlock();
  2636. build.hdr_len = 24;
  2637. break;
  2638. }
  2639. if (sdata->u.mgd.use_4addr) {
  2640. /* non-regular ethertype cannot use the fastpath */
  2641. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
  2642. IEEE80211_FCTL_TODS);
  2643. /* RA TA DA SA */
  2644. memcpy(hdr->addr1, sdata->deflink.u.mgd.bssid, ETH_ALEN);
  2645. memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
  2646. build.da_offs = offsetof(struct ieee80211_hdr, addr3);
  2647. build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
  2648. build.hdr_len = 30;
  2649. break;
  2650. }
  2651. fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
  2652. /* BSSID SA DA */
  2653. memcpy(hdr->addr1, sdata->vif.cfg.ap_addr, ETH_ALEN);
  2654. build.da_offs = offsetof(struct ieee80211_hdr, addr3);
  2655. build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
  2656. build.hdr_len = 24;
  2657. break;
  2658. case NL80211_IFTYPE_AP_VLAN:
  2659. if (sdata->wdev.use_4addr) {
  2660. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
  2661. IEEE80211_FCTL_TODS);
  2662. /* RA TA DA SA */
  2663. memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
  2664. memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
  2665. build.da_offs = offsetof(struct ieee80211_hdr, addr3);
  2666. build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
  2667. build.hdr_len = 30;
  2668. break;
  2669. }
  2670. fallthrough;
  2671. case NL80211_IFTYPE_AP:
  2672. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  2673. /* DA BSSID SA */
  2674. build.da_offs = offsetof(struct ieee80211_hdr, addr1);
  2675. if (sta->sta.mlo || !ieee80211_vif_is_mld(&sdata->vif)) {
  2676. memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
  2677. } else {
  2678. unsigned int link_id = sta->deflink.link_id;
  2679. struct ieee80211_link_data *link;
  2680. rcu_read_lock();
  2681. link = rcu_dereference(sdata->link[link_id]);
  2682. if (WARN_ON(!link)) {
  2683. rcu_read_unlock();
  2684. goto out;
  2685. }
  2686. memcpy(hdr->addr2, link->conf->addr, ETH_ALEN);
  2687. rcu_read_unlock();
  2688. }
  2689. build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
  2690. build.hdr_len = 24;
  2691. break;
  2692. default:
  2693. /* not handled on fast-xmit */
  2694. goto out;
  2695. }
  2696. if (sta->sta.wme) {
  2697. build.hdr_len += 2;
  2698. fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  2699. }
  2700. /* We store the key here so there's no point in using rcu_dereference()
  2701. * but that's fine because the code that changes the pointers will call
  2702. * this function after doing so. For a single CPU that would be enough,
  2703. * for multiple see the comment above.
  2704. */
  2705. build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
  2706. if (!build.key)
  2707. build.key = rcu_access_pointer(sdata->default_unicast_key);
  2708. if (build.key) {
  2709. bool gen_iv, iv_spc, mmic;
  2710. gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
  2711. iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
  2712. mmic = build.key->conf.flags &
  2713. (IEEE80211_KEY_FLAG_GENERATE_MMIC |
  2714. IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
  2715. /* don't handle software crypto */
  2716. if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  2717. goto out;
  2718. /* Key is being removed */
  2719. if (build.key->flags & KEY_FLAG_TAINTED)
  2720. goto out;
  2721. switch (build.key->conf.cipher) {
  2722. case WLAN_CIPHER_SUITE_CCMP:
  2723. case WLAN_CIPHER_SUITE_CCMP_256:
  2724. if (gen_iv)
  2725. build.pn_offs = build.hdr_len;
  2726. if (gen_iv || iv_spc)
  2727. build.hdr_len += IEEE80211_CCMP_HDR_LEN;
  2728. break;
  2729. case WLAN_CIPHER_SUITE_GCMP:
  2730. case WLAN_CIPHER_SUITE_GCMP_256:
  2731. if (gen_iv)
  2732. build.pn_offs = build.hdr_len;
  2733. if (gen_iv || iv_spc)
  2734. build.hdr_len += IEEE80211_GCMP_HDR_LEN;
  2735. break;
  2736. case WLAN_CIPHER_SUITE_TKIP:
  2737. /* cannot handle MMIC or IV generation in xmit-fast */
  2738. if (mmic || gen_iv)
  2739. goto out;
  2740. if (iv_spc)
  2741. build.hdr_len += IEEE80211_TKIP_IV_LEN;
  2742. break;
  2743. case WLAN_CIPHER_SUITE_WEP40:
  2744. case WLAN_CIPHER_SUITE_WEP104:
  2745. /* cannot handle IV generation in fast-xmit */
  2746. if (gen_iv)
  2747. goto out;
  2748. if (iv_spc)
  2749. build.hdr_len += IEEE80211_WEP_IV_LEN;
  2750. break;
  2751. case WLAN_CIPHER_SUITE_AES_CMAC:
  2752. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  2753. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  2754. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  2755. WARN(1,
  2756. "management cipher suite 0x%x enabled for data\n",
  2757. build.key->conf.cipher);
  2758. goto out;
  2759. default:
  2760. /* we don't know how to generate IVs for this at all */
  2761. if (WARN_ON(gen_iv))
  2762. goto out;
  2763. }
  2764. fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  2765. }
  2766. hdr->frame_control = fc;
  2767. memcpy(build.hdr + build.hdr_len,
  2768. rfc1042_header, sizeof(rfc1042_header));
  2769. build.hdr_len += sizeof(rfc1042_header);
  2770. fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
  2771. /* if the kmemdup fails, continue w/o fast_tx */
  2772. out:
  2773. /* we might have raced against another call to this function */
  2774. old = rcu_dereference_protected(sta->fast_tx,
  2775. lockdep_is_held(&sta->lock));
  2776. rcu_assign_pointer(sta->fast_tx, fast_tx);
  2777. if (old)
  2778. kfree_rcu(old, rcu_head);
  2779. spin_unlock_bh(&sta->lock);
  2780. }
  2781. void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
  2782. {
  2783. struct sta_info *sta;
  2784. rcu_read_lock();
  2785. list_for_each_entry_rcu(sta, &local->sta_list, list)
  2786. ieee80211_check_fast_xmit(sta);
  2787. rcu_read_unlock();
  2788. }
  2789. void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
  2790. {
  2791. struct ieee80211_local *local = sdata->local;
  2792. struct sta_info *sta;
  2793. rcu_read_lock();
  2794. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  2795. if (sdata != sta->sdata &&
  2796. (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
  2797. continue;
  2798. ieee80211_check_fast_xmit(sta);
  2799. }
  2800. rcu_read_unlock();
  2801. }
  2802. void ieee80211_clear_fast_xmit(struct sta_info *sta)
  2803. {
  2804. struct ieee80211_fast_tx *fast_tx;
  2805. spin_lock_bh(&sta->lock);
  2806. fast_tx = rcu_dereference_protected(sta->fast_tx,
  2807. lockdep_is_held(&sta->lock));
  2808. RCU_INIT_POINTER(sta->fast_tx, NULL);
  2809. spin_unlock_bh(&sta->lock);
  2810. if (fast_tx)
  2811. kfree_rcu(fast_tx, rcu_head);
  2812. }
  2813. static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
  2814. struct sk_buff *skb, int headroom)
  2815. {
  2816. if (skb_headroom(skb) < headroom) {
  2817. I802_DEBUG_INC(local->tx_expand_skb_head);
  2818. if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
  2819. wiphy_debug(local->hw.wiphy,
  2820. "failed to reallocate TX buffer\n");
  2821. return false;
  2822. }
  2823. }
  2824. return true;
  2825. }
  2826. static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
  2827. struct ieee80211_fast_tx *fast_tx,
  2828. struct sk_buff *skb)
  2829. {
  2830. struct ieee80211_local *local = sdata->local;
  2831. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  2832. struct ieee80211_hdr *hdr;
  2833. struct ethhdr *amsdu_hdr;
  2834. int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
  2835. int subframe_len = skb->len - hdr_len;
  2836. void *data;
  2837. u8 *qc, *h_80211_src, *h_80211_dst;
  2838. const u8 *bssid;
  2839. if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
  2840. return false;
  2841. if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
  2842. return true;
  2843. if (!ieee80211_amsdu_realloc_pad(local, skb,
  2844. sizeof(*amsdu_hdr) +
  2845. local->hw.extra_tx_headroom))
  2846. return false;
  2847. data = skb_push(skb, sizeof(*amsdu_hdr));
  2848. memmove(data, data + sizeof(*amsdu_hdr), hdr_len);
  2849. hdr = data;
  2850. amsdu_hdr = data + hdr_len;
  2851. /* h_80211_src/dst is addr* field within hdr */
  2852. h_80211_src = data + fast_tx->sa_offs;
  2853. h_80211_dst = data + fast_tx->da_offs;
  2854. amsdu_hdr->h_proto = cpu_to_be16(subframe_len);
  2855. ether_addr_copy(amsdu_hdr->h_source, h_80211_src);
  2856. ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst);
  2857. /* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA
  2858. * fields needs to be changed to BSSID for A-MSDU frames depending
  2859. * on FromDS/ToDS values.
  2860. */
  2861. switch (sdata->vif.type) {
  2862. case NL80211_IFTYPE_STATION:
  2863. bssid = sdata->vif.cfg.ap_addr;
  2864. break;
  2865. case NL80211_IFTYPE_AP:
  2866. case NL80211_IFTYPE_AP_VLAN:
  2867. bssid = sdata->vif.addr;
  2868. break;
  2869. default:
  2870. bssid = NULL;
  2871. }
  2872. if (bssid && ieee80211_has_fromds(hdr->frame_control))
  2873. ether_addr_copy(h_80211_src, bssid);
  2874. if (bssid && ieee80211_has_tods(hdr->frame_control))
  2875. ether_addr_copy(h_80211_dst, bssid);
  2876. qc = ieee80211_get_qos_ctl(hdr);
  2877. *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
  2878. info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
  2879. return true;
  2880. }
  2881. static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
  2882. struct sta_info *sta,
  2883. struct ieee80211_fast_tx *fast_tx,
  2884. struct sk_buff *skb,
  2885. const u8 *da, const u8 *sa)
  2886. {
  2887. struct ieee80211_local *local = sdata->local;
  2888. struct fq *fq = &local->fq;
  2889. struct fq_tin *tin;
  2890. struct fq_flow *flow;
  2891. u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
  2892. struct ieee80211_txq *txq = sta->sta.txq[tid];
  2893. struct txq_info *txqi;
  2894. struct sk_buff **frag_tail, *head;
  2895. int subframe_len = skb->len - ETH_ALEN;
  2896. u8 max_subframes = sta->sta.max_amsdu_subframes;
  2897. int max_frags = local->hw.max_tx_fragments;
  2898. int max_amsdu_len = sta->sta.cur->max_amsdu_len;
  2899. int orig_truesize;
  2900. u32 flow_idx;
  2901. __be16 len;
  2902. void *data;
  2903. bool ret = false;
  2904. unsigned int orig_len;
  2905. int n = 2, nfrags, pad = 0;
  2906. u16 hdrlen;
  2907. if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
  2908. return false;
  2909. if (sdata->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
  2910. return false;
  2911. if (ieee80211_vif_is_mesh(&sdata->vif))
  2912. return false;
  2913. if (skb_is_gso(skb))
  2914. return false;
  2915. if (!txq)
  2916. return false;
  2917. txqi = to_txq_info(txq);
  2918. if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
  2919. return false;
  2920. if (sta->sta.cur->max_rc_amsdu_len)
  2921. max_amsdu_len = min_t(int, max_amsdu_len,
  2922. sta->sta.cur->max_rc_amsdu_len);
  2923. if (sta->sta.cur->max_tid_amsdu_len[tid])
  2924. max_amsdu_len = min_t(int, max_amsdu_len,
  2925. sta->sta.cur->max_tid_amsdu_len[tid]);
  2926. flow_idx = fq_flow_idx(fq, skb);
  2927. spin_lock_bh(&fq->lock);
  2928. /* TODO: Ideally aggregation should be done on dequeue to remain
  2929. * responsive to environment changes.
  2930. */
  2931. tin = &txqi->tin;
  2932. flow = fq_flow_classify(fq, tin, flow_idx, skb);
  2933. head = skb_peek_tail(&flow->queue);
  2934. if (!head || skb_is_gso(head))
  2935. goto out;
  2936. orig_truesize = head->truesize;
  2937. orig_len = head->len;
  2938. if (skb->len + head->len > max_amsdu_len)
  2939. goto out;
  2940. nfrags = 1 + skb_shinfo(skb)->nr_frags;
  2941. nfrags += 1 + skb_shinfo(head)->nr_frags;
  2942. frag_tail = &skb_shinfo(head)->frag_list;
  2943. while (*frag_tail) {
  2944. nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
  2945. frag_tail = &(*frag_tail)->next;
  2946. n++;
  2947. }
  2948. if (max_subframes && n > max_subframes)
  2949. goto out;
  2950. if (max_frags && nfrags > max_frags)
  2951. goto out;
  2952. if (!drv_can_aggregate_in_amsdu(local, head, skb))
  2953. goto out;
  2954. if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
  2955. goto out;
  2956. /* If n == 2, the "while (*frag_tail)" loop above didn't execute
  2957. * and frag_tail should be &skb_shinfo(head)->frag_list.
  2958. * However, ieee80211_amsdu_prepare_head() can reallocate it.
  2959. * Reload frag_tail to have it pointing to the correct place.
  2960. */
  2961. if (n == 2)
  2962. frag_tail = &skb_shinfo(head)->frag_list;
  2963. /*
  2964. * Pad out the previous subframe to a multiple of 4 by adding the
  2965. * padding to the next one, that's being added. Note that head->len
  2966. * is the length of the full A-MSDU, but that works since each time
  2967. * we add a new subframe we pad out the previous one to a multiple
  2968. * of 4 and thus it no longer matters in the next round.
  2969. */
  2970. hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header);
  2971. if ((head->len - hdrlen) & 3)
  2972. pad = 4 - ((head->len - hdrlen) & 3);
  2973. if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) +
  2974. 2 + pad))
  2975. goto out_recalc;
  2976. ret = true;
  2977. data = skb_push(skb, ETH_ALEN + 2);
  2978. ether_addr_copy(data, da);
  2979. ether_addr_copy(data + ETH_ALEN, sa);
  2980. data += 2 * ETH_ALEN;
  2981. len = cpu_to_be16(subframe_len);
  2982. memcpy(data, &len, 2);
  2983. memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
  2984. memset(skb_push(skb, pad), 0, pad);
  2985. head->len += skb->len;
  2986. head->data_len += skb->len;
  2987. *frag_tail = skb;
  2988. out_recalc:
  2989. fq->memory_usage += head->truesize - orig_truesize;
  2990. if (head->len != orig_len) {
  2991. flow->backlog += head->len - orig_len;
  2992. tin->backlog_bytes += head->len - orig_len;
  2993. }
  2994. out:
  2995. spin_unlock_bh(&fq->lock);
  2996. return ret;
  2997. }
  2998. /*
  2999. * Can be called while the sta lock is held. Anything that can cause packets to
  3000. * be generated will cause deadlock!
  3001. */
  3002. static ieee80211_tx_result
  3003. ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
  3004. struct sta_info *sta, u8 pn_offs,
  3005. struct ieee80211_key *key,
  3006. struct ieee80211_tx_data *tx)
  3007. {
  3008. struct sk_buff *skb = tx->skb;
  3009. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  3010. struct ieee80211_hdr *hdr = (void *)skb->data;
  3011. u8 tid = IEEE80211_NUM_TIDS;
  3012. if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL) &&
  3013. ieee80211_tx_h_rate_ctrl(tx) != TX_CONTINUE)
  3014. return TX_DROP;
  3015. if (key)
  3016. info->control.hw_key = &key->conf;
  3017. dev_sw_netstats_tx_add(skb->dev, 1, skb->len);
  3018. if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
  3019. tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
  3020. hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
  3021. } else {
  3022. info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
  3023. hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
  3024. sdata->sequence_number += 0x10;
  3025. }
  3026. if (skb_shinfo(skb)->gso_size)
  3027. sta->deflink.tx_stats.msdu[tid] +=
  3028. DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
  3029. else
  3030. sta->deflink.tx_stats.msdu[tid]++;
  3031. info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
  3032. /* statistics normally done by ieee80211_tx_h_stats (but that
  3033. * has to consider fragmentation, so is more complex)
  3034. */
  3035. sta->deflink.tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
  3036. sta->deflink.tx_stats.packets[skb_get_queue_mapping(skb)]++;
  3037. if (pn_offs) {
  3038. u64 pn;
  3039. u8 *crypto_hdr = skb->data + pn_offs;
  3040. switch (key->conf.cipher) {
  3041. case WLAN_CIPHER_SUITE_CCMP:
  3042. case WLAN_CIPHER_SUITE_CCMP_256:
  3043. case WLAN_CIPHER_SUITE_GCMP:
  3044. case WLAN_CIPHER_SUITE_GCMP_256:
  3045. pn = atomic64_inc_return(&key->conf.tx_pn);
  3046. crypto_hdr[0] = pn;
  3047. crypto_hdr[1] = pn >> 8;
  3048. crypto_hdr[3] = 0x20 | (key->conf.keyidx << 6);
  3049. crypto_hdr[4] = pn >> 16;
  3050. crypto_hdr[5] = pn >> 24;
  3051. crypto_hdr[6] = pn >> 32;
  3052. crypto_hdr[7] = pn >> 40;
  3053. break;
  3054. }
  3055. }
  3056. return TX_CONTINUE;
  3057. }
  3058. static netdev_features_t
  3059. ieee80211_sdata_netdev_features(struct ieee80211_sub_if_data *sdata)
  3060. {
  3061. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
  3062. return sdata->vif.netdev_features;
  3063. if (!sdata->bss)
  3064. return 0;
  3065. sdata = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
  3066. return sdata->vif.netdev_features;
  3067. }
  3068. static struct sk_buff *
  3069. ieee80211_tx_skb_fixup(struct sk_buff *skb, netdev_features_t features)
  3070. {
  3071. if (skb_is_gso(skb)) {
  3072. struct sk_buff *segs;
  3073. segs = skb_gso_segment(skb, features);
  3074. if (!segs)
  3075. return skb;
  3076. if (IS_ERR(segs))
  3077. goto free;
  3078. consume_skb(skb);
  3079. return segs;
  3080. }
  3081. if (skb_needs_linearize(skb, features) && __skb_linearize(skb))
  3082. goto free;
  3083. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  3084. int ofs = skb_checksum_start_offset(skb);
  3085. if (skb->encapsulation)
  3086. skb_set_inner_transport_header(skb, ofs);
  3087. else
  3088. skb_set_transport_header(skb, ofs);
  3089. if (skb_csum_hwoffload_help(skb, features))
  3090. goto free;
  3091. }
  3092. skb_mark_not_on_list(skb);
  3093. return skb;
  3094. free:
  3095. kfree_skb(skb);
  3096. return NULL;
  3097. }
  3098. void __ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
  3099. struct sta_info *sta,
  3100. struct ieee80211_fast_tx *fast_tx,
  3101. struct sk_buff *skb, bool ampdu,
  3102. const u8 *da, const u8 *sa)
  3103. {
  3104. struct ieee80211_local *local = sdata->local;
  3105. struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
  3106. struct ieee80211_tx_info *info;
  3107. struct ieee80211_tx_data tx;
  3108. ieee80211_tx_result r;
  3109. int hw_headroom = sdata->local->hw.extra_tx_headroom;
  3110. int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
  3111. skb = skb_share_check(skb, GFP_ATOMIC);
  3112. if (unlikely(!skb))
  3113. return;
  3114. if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
  3115. ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb, da, sa))
  3116. return;
  3117. /* will not be crypto-handled beyond what we do here, so use false
  3118. * as the may-encrypt argument for the resize to not account for
  3119. * more room than we already have in 'extra_head'
  3120. */
  3121. if (unlikely(ieee80211_skb_resize(sdata, skb,
  3122. max_t(int, extra_head + hw_headroom -
  3123. skb_headroom(skb), 0),
  3124. ENCRYPT_NO)))
  3125. goto free;
  3126. hdr = skb_push(skb, extra_head);
  3127. memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
  3128. memcpy(skb->data + fast_tx->da_offs, da, ETH_ALEN);
  3129. memcpy(skb->data + fast_tx->sa_offs, sa, ETH_ALEN);
  3130. info = IEEE80211_SKB_CB(skb);
  3131. memset(info, 0, sizeof(*info));
  3132. info->band = fast_tx->band;
  3133. info->control.vif = &sdata->vif;
  3134. info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
  3135. IEEE80211_TX_CTL_DONTFRAG;
  3136. info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT |
  3137. u32_encode_bits(IEEE80211_LINK_UNSPECIFIED,
  3138. IEEE80211_TX_CTRL_MLO_LINK);
  3139. #ifdef CONFIG_MAC80211_DEBUGFS
  3140. if (local->force_tx_status)
  3141. info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
  3142. #endif
  3143. if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
  3144. u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
  3145. *ieee80211_get_qos_ctl(hdr) = tid;
  3146. }
  3147. __skb_queue_head_init(&tx.skbs);
  3148. tx.flags = IEEE80211_TX_UNICAST;
  3149. tx.local = local;
  3150. tx.sdata = sdata;
  3151. tx.sta = sta;
  3152. tx.key = fast_tx->key;
  3153. if (ieee80211_queue_skb(local, sdata, sta, skb))
  3154. return;
  3155. tx.skb = skb;
  3156. r = ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
  3157. fast_tx->key, &tx);
  3158. tx.skb = NULL;
  3159. if (r == TX_DROP)
  3160. goto free;
  3161. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  3162. sdata = container_of(sdata->bss,
  3163. struct ieee80211_sub_if_data, u.ap);
  3164. __skb_queue_tail(&tx.skbs, skb);
  3165. ieee80211_tx_frags(local, &sdata->vif, sta, &tx.skbs, false);
  3166. return;
  3167. free:
  3168. kfree_skb(skb);
  3169. }
  3170. static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
  3171. struct sta_info *sta,
  3172. struct ieee80211_fast_tx *fast_tx,
  3173. struct sk_buff *skb)
  3174. {
  3175. u16 ethertype = (skb->data[12] << 8) | skb->data[13];
  3176. struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
  3177. struct tid_ampdu_tx *tid_tx = NULL;
  3178. struct sk_buff *next;
  3179. struct ethhdr eth;
  3180. u8 tid = IEEE80211_NUM_TIDS;
  3181. /* control port protocol needs a lot of special handling */
  3182. if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
  3183. return false;
  3184. /* only RFC 1042 SNAP */
  3185. if (ethertype < ETH_P_802_3_MIN)
  3186. return false;
  3187. /* don't handle TX status request here either */
  3188. if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
  3189. return false;
  3190. if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
  3191. tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
  3192. tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
  3193. if (tid_tx) {
  3194. if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
  3195. return false;
  3196. if (tid_tx->timeout)
  3197. tid_tx->last_tx = jiffies;
  3198. }
  3199. }
  3200. memcpy(&eth, skb->data, ETH_HLEN - 2);
  3201. /* after this point (skb is modified) we cannot return false */
  3202. skb = ieee80211_tx_skb_fixup(skb, ieee80211_sdata_netdev_features(sdata));
  3203. if (!skb)
  3204. return true;
  3205. skb_list_walk_safe(skb, skb, next) {
  3206. skb_mark_not_on_list(skb);
  3207. __ieee80211_xmit_fast(sdata, sta, fast_tx, skb, tid_tx,
  3208. eth.h_dest, eth.h_source);
  3209. }
  3210. return true;
  3211. }
  3212. struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
  3213. struct ieee80211_txq *txq)
  3214. {
  3215. struct ieee80211_local *local = hw_to_local(hw);
  3216. struct txq_info *txqi = container_of(txq, struct txq_info, txq);
  3217. struct ieee80211_hdr *hdr;
  3218. struct sk_buff *skb = NULL;
  3219. struct fq *fq = &local->fq;
  3220. struct fq_tin *tin = &txqi->tin;
  3221. struct ieee80211_tx_info *info;
  3222. struct ieee80211_tx_data tx;
  3223. ieee80211_tx_result r;
  3224. struct ieee80211_vif *vif = txq->vif;
  3225. int q = vif->hw_queue[txq->ac];
  3226. unsigned long flags;
  3227. bool q_stopped;
  3228. WARN_ON_ONCE(softirq_count() == 0);
  3229. if (!ieee80211_txq_airtime_check(hw, txq))
  3230. return NULL;
  3231. begin:
  3232. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  3233. q_stopped = local->queue_stop_reasons[q];
  3234. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  3235. if (unlikely(q_stopped)) {
  3236. /* mark for waking later */
  3237. set_bit(IEEE80211_TXQ_DIRTY, &txqi->flags);
  3238. return NULL;
  3239. }
  3240. spin_lock_bh(&fq->lock);
  3241. /* Make sure fragments stay together. */
  3242. skb = __skb_dequeue(&txqi->frags);
  3243. if (unlikely(skb)) {
  3244. if (!(IEEE80211_SKB_CB(skb)->control.flags &
  3245. IEEE80211_TX_INTCFL_NEED_TXPROCESSING))
  3246. goto out;
  3247. IEEE80211_SKB_CB(skb)->control.flags &=
  3248. ~IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
  3249. } else {
  3250. if (unlikely(test_bit(IEEE80211_TXQ_STOP, &txqi->flags)))
  3251. goto out;
  3252. skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
  3253. }
  3254. if (!skb)
  3255. goto out;
  3256. spin_unlock_bh(&fq->lock);
  3257. hdr = (struct ieee80211_hdr *)skb->data;
  3258. info = IEEE80211_SKB_CB(skb);
  3259. memset(&tx, 0, sizeof(tx));
  3260. __skb_queue_head_init(&tx.skbs);
  3261. tx.local = local;
  3262. tx.skb = skb;
  3263. tx.sdata = vif_to_sdata(info->control.vif);
  3264. if (txq->sta) {
  3265. tx.sta = container_of(txq->sta, struct sta_info, sta);
  3266. /*
  3267. * Drop unicast frames to unauthorised stations unless they are
  3268. * injected frames or EAPOL frames from the local station.
  3269. */
  3270. if (unlikely(!(info->flags & IEEE80211_TX_CTL_INJECTED) &&
  3271. ieee80211_is_data(hdr->frame_control) &&
  3272. !ieee80211_vif_is_mesh(&tx.sdata->vif) &&
  3273. tx.sdata->vif.type != NL80211_IFTYPE_OCB &&
  3274. !is_multicast_ether_addr(hdr->addr1) &&
  3275. !test_sta_flag(tx.sta, WLAN_STA_AUTHORIZED) &&
  3276. (!(info->control.flags &
  3277. IEEE80211_TX_CTRL_PORT_CTRL_PROTO) ||
  3278. !ieee80211_is_our_addr(tx.sdata, hdr->addr2,
  3279. NULL)))) {
  3280. I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
  3281. ieee80211_free_txskb(&local->hw, skb);
  3282. goto begin;
  3283. }
  3284. }
  3285. /*
  3286. * The key can be removed while the packet was queued, so need to call
  3287. * this here to get the current key.
  3288. */
  3289. r = ieee80211_tx_h_select_key(&tx);
  3290. if (r != TX_CONTINUE) {
  3291. ieee80211_free_txskb(&local->hw, skb);
  3292. goto begin;
  3293. }
  3294. if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
  3295. info->flags |= (IEEE80211_TX_CTL_AMPDU |
  3296. IEEE80211_TX_CTL_DONTFRAG);
  3297. if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
  3298. if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
  3299. r = ieee80211_tx_h_rate_ctrl(&tx);
  3300. if (r != TX_CONTINUE) {
  3301. ieee80211_free_txskb(&local->hw, skb);
  3302. goto begin;
  3303. }
  3304. }
  3305. goto encap_out;
  3306. }
  3307. if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
  3308. struct sta_info *sta = container_of(txq->sta, struct sta_info,
  3309. sta);
  3310. u8 pn_offs = 0;
  3311. if (tx.key &&
  3312. (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
  3313. pn_offs = ieee80211_hdrlen(hdr->frame_control);
  3314. r = ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
  3315. tx.key, &tx);
  3316. if (r != TX_CONTINUE) {
  3317. ieee80211_free_txskb(&local->hw, skb);
  3318. goto begin;
  3319. }
  3320. } else {
  3321. if (invoke_tx_handlers_late(&tx))
  3322. goto begin;
  3323. skb = __skb_dequeue(&tx.skbs);
  3324. info = IEEE80211_SKB_CB(skb);
  3325. if (!skb_queue_empty(&tx.skbs)) {
  3326. spin_lock_bh(&fq->lock);
  3327. skb_queue_splice_tail(&tx.skbs, &txqi->frags);
  3328. spin_unlock_bh(&fq->lock);
  3329. }
  3330. }
  3331. if (skb_has_frag_list(skb) &&
  3332. !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
  3333. if (skb_linearize(skb)) {
  3334. ieee80211_free_txskb(&local->hw, skb);
  3335. goto begin;
  3336. }
  3337. }
  3338. switch (tx.sdata->vif.type) {
  3339. case NL80211_IFTYPE_MONITOR:
  3340. if (tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
  3341. vif = &tx.sdata->vif;
  3342. break;
  3343. }
  3344. tx.sdata = rcu_dereference(local->monitor_sdata);
  3345. if (tx.sdata &&
  3346. ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF)) {
  3347. vif = &tx.sdata->vif;
  3348. info->hw_queue =
  3349. vif->hw_queue[skb_get_queue_mapping(skb)];
  3350. } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
  3351. ieee80211_free_txskb(&local->hw, skb);
  3352. goto begin;
  3353. } else {
  3354. info->control.vif = NULL;
  3355. return skb;
  3356. }
  3357. break;
  3358. case NL80211_IFTYPE_AP_VLAN:
  3359. tx.sdata = container_of(tx.sdata->bss,
  3360. struct ieee80211_sub_if_data, u.ap);
  3361. fallthrough;
  3362. default:
  3363. vif = &tx.sdata->vif;
  3364. break;
  3365. }
  3366. encap_out:
  3367. info->control.vif = vif;
  3368. if (tx.sta &&
  3369. wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) {
  3370. bool ampdu = txq->ac != IEEE80211_AC_VO;
  3371. u32 airtime;
  3372. airtime = ieee80211_calc_expected_tx_airtime(hw, vif, txq->sta,
  3373. skb->len, ampdu);
  3374. if (airtime) {
  3375. airtime = ieee80211_info_set_tx_time_est(info, airtime);
  3376. ieee80211_sta_update_pending_airtime(local, tx.sta,
  3377. txq->ac,
  3378. airtime,
  3379. false);
  3380. }
  3381. }
  3382. return skb;
  3383. out:
  3384. spin_unlock_bh(&fq->lock);
  3385. return skb;
  3386. }
  3387. EXPORT_SYMBOL(ieee80211_tx_dequeue);
  3388. static inline s32 ieee80211_sta_deficit(struct sta_info *sta, u8 ac)
  3389. {
  3390. struct airtime_info *air_info = &sta->airtime[ac];
  3391. return air_info->deficit - atomic_read(&air_info->aql_tx_pending);
  3392. }
  3393. static void
  3394. ieee80211_txq_set_active(struct txq_info *txqi)
  3395. {
  3396. struct sta_info *sta;
  3397. if (!txqi->txq.sta)
  3398. return;
  3399. sta = container_of(txqi->txq.sta, struct sta_info, sta);
  3400. sta->airtime[txqi->txq.ac].last_active = jiffies;
  3401. }
  3402. static bool
  3403. ieee80211_txq_keep_active(struct txq_info *txqi)
  3404. {
  3405. struct sta_info *sta;
  3406. if (!txqi->txq.sta)
  3407. return false;
  3408. sta = container_of(txqi->txq.sta, struct sta_info, sta);
  3409. if (ieee80211_sta_deficit(sta, txqi->txq.ac) >= 0)
  3410. return false;
  3411. return ieee80211_sta_keep_active(sta, txqi->txq.ac);
  3412. }
  3413. struct ieee80211_txq *ieee80211_next_txq(struct ieee80211_hw *hw, u8 ac)
  3414. {
  3415. struct ieee80211_local *local = hw_to_local(hw);
  3416. struct ieee80211_txq *ret = NULL;
  3417. struct txq_info *txqi = NULL, *head = NULL;
  3418. bool found_eligible_txq = false;
  3419. spin_lock_bh(&local->active_txq_lock[ac]);
  3420. if (!local->schedule_round[ac])
  3421. goto out;
  3422. begin:
  3423. txqi = list_first_entry_or_null(&local->active_txqs[ac],
  3424. struct txq_info,
  3425. schedule_order);
  3426. if (!txqi)
  3427. goto out;
  3428. if (txqi == head) {
  3429. if (!found_eligible_txq)
  3430. goto out;
  3431. else
  3432. found_eligible_txq = false;
  3433. }
  3434. if (!head)
  3435. head = txqi;
  3436. if (txqi->txq.sta) {
  3437. struct sta_info *sta = container_of(txqi->txq.sta,
  3438. struct sta_info, sta);
  3439. bool aql_check = ieee80211_txq_airtime_check(hw, &txqi->txq);
  3440. s32 deficit = ieee80211_sta_deficit(sta, txqi->txq.ac);
  3441. if (aql_check)
  3442. found_eligible_txq = true;
  3443. if (deficit < 0)
  3444. sta->airtime[txqi->txq.ac].deficit +=
  3445. sta->airtime_weight;
  3446. if (deficit < 0 || !aql_check) {
  3447. list_move_tail(&txqi->schedule_order,
  3448. &local->active_txqs[txqi->txq.ac]);
  3449. goto begin;
  3450. }
  3451. }
  3452. if (txqi->schedule_round == local->schedule_round[ac])
  3453. goto out;
  3454. list_del_init(&txqi->schedule_order);
  3455. txqi->schedule_round = local->schedule_round[ac];
  3456. ret = &txqi->txq;
  3457. out:
  3458. spin_unlock_bh(&local->active_txq_lock[ac]);
  3459. return ret;
  3460. }
  3461. EXPORT_SYMBOL(ieee80211_next_txq);
  3462. void __ieee80211_schedule_txq(struct ieee80211_hw *hw,
  3463. struct ieee80211_txq *txq,
  3464. bool force)
  3465. {
  3466. struct ieee80211_local *local = hw_to_local(hw);
  3467. struct txq_info *txqi = to_txq_info(txq);
  3468. bool has_queue;
  3469. spin_lock_bh(&local->active_txq_lock[txq->ac]);
  3470. has_queue = force || txq_has_queue(txq);
  3471. if (list_empty(&txqi->schedule_order) &&
  3472. (has_queue || ieee80211_txq_keep_active(txqi))) {
  3473. /* If airtime accounting is active, always enqueue STAs at the
  3474. * head of the list to ensure that they only get moved to the
  3475. * back by the airtime DRR scheduler once they have a negative
  3476. * deficit. A station that already has a negative deficit will
  3477. * get immediately moved to the back of the list on the next
  3478. * call to ieee80211_next_txq().
  3479. */
  3480. if (txqi->txq.sta && local->airtime_flags && has_queue &&
  3481. wiphy_ext_feature_isset(local->hw.wiphy,
  3482. NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
  3483. list_add(&txqi->schedule_order,
  3484. &local->active_txqs[txq->ac]);
  3485. else
  3486. list_add_tail(&txqi->schedule_order,
  3487. &local->active_txqs[txq->ac]);
  3488. if (has_queue)
  3489. ieee80211_txq_set_active(txqi);
  3490. }
  3491. spin_unlock_bh(&local->active_txq_lock[txq->ac]);
  3492. }
  3493. EXPORT_SYMBOL(__ieee80211_schedule_txq);
  3494. DEFINE_STATIC_KEY_FALSE(aql_disable);
  3495. bool ieee80211_txq_airtime_check(struct ieee80211_hw *hw,
  3496. struct ieee80211_txq *txq)
  3497. {
  3498. struct sta_info *sta;
  3499. struct ieee80211_local *local = hw_to_local(hw);
  3500. if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
  3501. return true;
  3502. if (static_branch_unlikely(&aql_disable))
  3503. return true;
  3504. if (!txq->sta)
  3505. return true;
  3506. if (unlikely(txq->tid == IEEE80211_NUM_TIDS))
  3507. return true;
  3508. sta = container_of(txq->sta, struct sta_info, sta);
  3509. if (atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
  3510. sta->airtime[txq->ac].aql_limit_low)
  3511. return true;
  3512. if (atomic_read(&local->aql_total_pending_airtime) <
  3513. local->aql_threshold &&
  3514. atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
  3515. sta->airtime[txq->ac].aql_limit_high)
  3516. return true;
  3517. return false;
  3518. }
  3519. EXPORT_SYMBOL(ieee80211_txq_airtime_check);
  3520. static bool
  3521. ieee80211_txq_schedule_airtime_check(struct ieee80211_local *local, u8 ac)
  3522. {
  3523. unsigned int num_txq = 0;
  3524. struct txq_info *txq;
  3525. u32 aql_limit;
  3526. if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
  3527. return true;
  3528. list_for_each_entry(txq, &local->active_txqs[ac], schedule_order)
  3529. num_txq++;
  3530. aql_limit = (num_txq - 1) * local->aql_txq_limit_low[ac] / 2 +
  3531. local->aql_txq_limit_high[ac];
  3532. return atomic_read(&local->aql_ac_pending_airtime[ac]) < aql_limit;
  3533. }
  3534. bool ieee80211_txq_may_transmit(struct ieee80211_hw *hw,
  3535. struct ieee80211_txq *txq)
  3536. {
  3537. struct ieee80211_local *local = hw_to_local(hw);
  3538. struct txq_info *iter, *tmp, *txqi = to_txq_info(txq);
  3539. struct sta_info *sta;
  3540. u8 ac = txq->ac;
  3541. spin_lock_bh(&local->active_txq_lock[ac]);
  3542. if (!txqi->txq.sta)
  3543. goto out;
  3544. if (list_empty(&txqi->schedule_order))
  3545. goto out;
  3546. if (!ieee80211_txq_schedule_airtime_check(local, ac))
  3547. goto out;
  3548. list_for_each_entry_safe(iter, tmp, &local->active_txqs[ac],
  3549. schedule_order) {
  3550. if (iter == txqi)
  3551. break;
  3552. if (!iter->txq.sta) {
  3553. list_move_tail(&iter->schedule_order,
  3554. &local->active_txqs[ac]);
  3555. continue;
  3556. }
  3557. sta = container_of(iter->txq.sta, struct sta_info, sta);
  3558. if (ieee80211_sta_deficit(sta, ac) < 0)
  3559. sta->airtime[ac].deficit += sta->airtime_weight;
  3560. list_move_tail(&iter->schedule_order, &local->active_txqs[ac]);
  3561. }
  3562. sta = container_of(txqi->txq.sta, struct sta_info, sta);
  3563. if (sta->airtime[ac].deficit >= 0)
  3564. goto out;
  3565. sta->airtime[ac].deficit += sta->airtime_weight;
  3566. list_move_tail(&txqi->schedule_order, &local->active_txqs[ac]);
  3567. spin_unlock_bh(&local->active_txq_lock[ac]);
  3568. return false;
  3569. out:
  3570. if (!list_empty(&txqi->schedule_order))
  3571. list_del_init(&txqi->schedule_order);
  3572. spin_unlock_bh(&local->active_txq_lock[ac]);
  3573. return true;
  3574. }
  3575. EXPORT_SYMBOL(ieee80211_txq_may_transmit);
  3576. void ieee80211_txq_schedule_start(struct ieee80211_hw *hw, u8 ac)
  3577. {
  3578. struct ieee80211_local *local = hw_to_local(hw);
  3579. spin_lock_bh(&local->active_txq_lock[ac]);
  3580. if (ieee80211_txq_schedule_airtime_check(local, ac)) {
  3581. local->schedule_round[ac]++;
  3582. if (!local->schedule_round[ac])
  3583. local->schedule_round[ac]++;
  3584. } else {
  3585. local->schedule_round[ac] = 0;
  3586. }
  3587. spin_unlock_bh(&local->active_txq_lock[ac]);
  3588. }
  3589. EXPORT_SYMBOL(ieee80211_txq_schedule_start);
  3590. void __ieee80211_subif_start_xmit(struct sk_buff *skb,
  3591. struct net_device *dev,
  3592. u32 info_flags,
  3593. u32 ctrl_flags,
  3594. u64 *cookie)
  3595. {
  3596. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  3597. struct ieee80211_local *local = sdata->local;
  3598. struct sta_info *sta;
  3599. struct sk_buff *next;
  3600. int len = skb->len;
  3601. if (unlikely(!ieee80211_sdata_running(sdata) || skb->len < ETH_HLEN)) {
  3602. kfree_skb(skb);
  3603. return;
  3604. }
  3605. sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
  3606. rcu_read_lock();
  3607. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  3608. ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT) &&
  3609. ieee80211_mesh_xmit_fast(sdata, skb, ctrl_flags))
  3610. goto out;
  3611. if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
  3612. goto out_free;
  3613. if (IS_ERR(sta))
  3614. sta = NULL;
  3615. skb_set_queue_mapping(skb, ieee80211_select_queue(sdata, sta, skb));
  3616. ieee80211_aggr_check(sdata, sta, skb);
  3617. if (sta) {
  3618. struct ieee80211_fast_tx *fast_tx;
  3619. fast_tx = rcu_dereference(sta->fast_tx);
  3620. if (fast_tx &&
  3621. ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
  3622. goto out;
  3623. }
  3624. /* the frame could be fragmented, software-encrypted, and other
  3625. * things so we cannot really handle checksum or GSO offload.
  3626. * fix it up in software before we handle anything else.
  3627. */
  3628. skb = ieee80211_tx_skb_fixup(skb, 0);
  3629. if (!skb) {
  3630. len = 0;
  3631. goto out;
  3632. }
  3633. skb_list_walk_safe(skb, skb, next) {
  3634. skb_mark_not_on_list(skb);
  3635. if (skb->protocol == sdata->control_port_protocol)
  3636. ctrl_flags |= IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
  3637. skb = ieee80211_build_hdr(sdata, skb, info_flags,
  3638. sta, ctrl_flags, cookie);
  3639. if (IS_ERR(skb)) {
  3640. kfree_skb_list(next);
  3641. goto out;
  3642. }
  3643. dev_sw_netstats_tx_add(dev, 1, skb->len);
  3644. ieee80211_xmit(sdata, sta, skb);
  3645. }
  3646. goto out;
  3647. out_free:
  3648. kfree_skb(skb);
  3649. len = 0;
  3650. out:
  3651. if (len)
  3652. ieee80211_tpt_led_trig_tx(local, len);
  3653. rcu_read_unlock();
  3654. }
  3655. static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta)
  3656. {
  3657. struct ethhdr *eth;
  3658. int err;
  3659. err = skb_ensure_writable(skb, ETH_HLEN);
  3660. if (unlikely(err))
  3661. return err;
  3662. eth = (void *)skb->data;
  3663. ether_addr_copy(eth->h_dest, sta->sta.addr);
  3664. return 0;
  3665. }
  3666. static bool ieee80211_multicast_to_unicast(struct sk_buff *skb,
  3667. struct net_device *dev)
  3668. {
  3669. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  3670. const struct ethhdr *eth = (void *)skb->data;
  3671. const struct vlan_ethhdr *ethvlan = (void *)skb->data;
  3672. __be16 ethertype;
  3673. switch (sdata->vif.type) {
  3674. case NL80211_IFTYPE_AP_VLAN:
  3675. if (sdata->u.vlan.sta)
  3676. return false;
  3677. if (sdata->wdev.use_4addr)
  3678. return false;
  3679. fallthrough;
  3680. case NL80211_IFTYPE_AP:
  3681. /* check runtime toggle for this bss */
  3682. if (!sdata->bss->multicast_to_unicast)
  3683. return false;
  3684. break;
  3685. default:
  3686. return false;
  3687. }
  3688. /* multicast to unicast conversion only for some payload */
  3689. ethertype = eth->h_proto;
  3690. if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN)
  3691. ethertype = ethvlan->h_vlan_encapsulated_proto;
  3692. switch (ethertype) {
  3693. case htons(ETH_P_ARP):
  3694. case htons(ETH_P_IP):
  3695. case htons(ETH_P_IPV6):
  3696. break;
  3697. default:
  3698. return false;
  3699. }
  3700. return true;
  3701. }
  3702. static void
  3703. ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev,
  3704. struct sk_buff_head *queue)
  3705. {
  3706. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  3707. struct ieee80211_local *local = sdata->local;
  3708. const struct ethhdr *eth = (struct ethhdr *)skb->data;
  3709. struct sta_info *sta, *first = NULL;
  3710. struct sk_buff *cloned_skb;
  3711. rcu_read_lock();
  3712. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  3713. if (sdata != sta->sdata)
  3714. /* AP-VLAN mismatch */
  3715. continue;
  3716. if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr)))
  3717. /* do not send back to source */
  3718. continue;
  3719. if (!first) {
  3720. first = sta;
  3721. continue;
  3722. }
  3723. cloned_skb = skb_clone(skb, GFP_ATOMIC);
  3724. if (!cloned_skb)
  3725. goto multicast;
  3726. if (unlikely(ieee80211_change_da(cloned_skb, sta))) {
  3727. dev_kfree_skb(cloned_skb);
  3728. goto multicast;
  3729. }
  3730. __skb_queue_tail(queue, cloned_skb);
  3731. }
  3732. if (likely(first)) {
  3733. if (unlikely(ieee80211_change_da(skb, first)))
  3734. goto multicast;
  3735. __skb_queue_tail(queue, skb);
  3736. } else {
  3737. /* no STA connected, drop */
  3738. kfree_skb(skb);
  3739. skb = NULL;
  3740. }
  3741. goto out;
  3742. multicast:
  3743. __skb_queue_purge(queue);
  3744. __skb_queue_tail(queue, skb);
  3745. out:
  3746. rcu_read_unlock();
  3747. }
  3748. static void ieee80211_mlo_multicast_tx_one(struct ieee80211_sub_if_data *sdata,
  3749. struct sk_buff *skb, u32 ctrl_flags,
  3750. unsigned int link_id)
  3751. {
  3752. struct sk_buff *out;
  3753. out = skb_copy(skb, GFP_ATOMIC);
  3754. if (!out)
  3755. return;
  3756. ctrl_flags |= u32_encode_bits(link_id, IEEE80211_TX_CTRL_MLO_LINK);
  3757. __ieee80211_subif_start_xmit(out, sdata->dev, 0, ctrl_flags, NULL);
  3758. }
  3759. static void ieee80211_mlo_multicast_tx(struct net_device *dev,
  3760. struct sk_buff *skb)
  3761. {
  3762. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  3763. unsigned long links = sdata->vif.active_links;
  3764. unsigned int link;
  3765. u32 ctrl_flags = IEEE80211_TX_CTRL_MCAST_MLO_FIRST_TX;
  3766. if (hweight16(links) == 1) {
  3767. ctrl_flags |= u32_encode_bits(__ffs(links),
  3768. IEEE80211_TX_CTRL_MLO_LINK);
  3769. __ieee80211_subif_start_xmit(skb, sdata->dev, 0, ctrl_flags,
  3770. NULL);
  3771. return;
  3772. }
  3773. for_each_set_bit(link, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
  3774. ieee80211_mlo_multicast_tx_one(sdata, skb, ctrl_flags, link);
  3775. ctrl_flags = 0;
  3776. }
  3777. kfree_skb(skb);
  3778. }
  3779. /**
  3780. * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
  3781. * @skb: packet to be sent
  3782. * @dev: incoming interface
  3783. *
  3784. * On failure skb will be freed.
  3785. *
  3786. * Returns: the netdev TX status (but really only %NETDEV_TX_OK)
  3787. */
  3788. netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
  3789. struct net_device *dev)
  3790. {
  3791. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  3792. const struct ethhdr *eth = (void *)skb->data;
  3793. if (likely(!is_multicast_ether_addr(eth->h_dest)))
  3794. goto normal;
  3795. if (unlikely(!ieee80211_sdata_running(sdata))) {
  3796. kfree_skb(skb);
  3797. return NETDEV_TX_OK;
  3798. }
  3799. if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) {
  3800. struct sk_buff_head queue;
  3801. __skb_queue_head_init(&queue);
  3802. ieee80211_convert_to_unicast(skb, dev, &queue);
  3803. while ((skb = __skb_dequeue(&queue)))
  3804. __ieee80211_subif_start_xmit(skb, dev, 0,
  3805. IEEE80211_TX_CTRL_MLO_LINK_UNSPEC,
  3806. NULL);
  3807. } else if (ieee80211_vif_is_mld(&sdata->vif) &&
  3808. sdata->vif.type == NL80211_IFTYPE_AP &&
  3809. !ieee80211_hw_check(&sdata->local->hw, MLO_MCAST_MULTI_LINK_TX)) {
  3810. ieee80211_mlo_multicast_tx(dev, skb);
  3811. } else {
  3812. normal:
  3813. __ieee80211_subif_start_xmit(skb, dev, 0,
  3814. IEEE80211_TX_CTRL_MLO_LINK_UNSPEC,
  3815. NULL);
  3816. }
  3817. return NETDEV_TX_OK;
  3818. }
  3819. static bool __ieee80211_tx_8023(struct ieee80211_sub_if_data *sdata,
  3820. struct sk_buff *skb, struct sta_info *sta,
  3821. bool txpending)
  3822. {
  3823. struct ieee80211_local *local = sdata->local;
  3824. struct ieee80211_tx_control control = {};
  3825. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  3826. struct ieee80211_sta *pubsta = NULL;
  3827. unsigned long flags;
  3828. int q = info->hw_queue;
  3829. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  3830. if (local->queue_stop_reasons[q] ||
  3831. (!txpending && !skb_queue_empty(&local->pending[q]))) {
  3832. if (txpending)
  3833. skb_queue_head(&local->pending[q], skb);
  3834. else
  3835. skb_queue_tail(&local->pending[q], skb);
  3836. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  3837. return false;
  3838. }
  3839. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  3840. if (sta && sta->uploaded)
  3841. pubsta = &sta->sta;
  3842. control.sta = pubsta;
  3843. drv_tx(local, &control, skb);
  3844. return true;
  3845. }
  3846. static bool ieee80211_tx_8023(struct ieee80211_sub_if_data *sdata,
  3847. struct sk_buff *skb, struct sta_info *sta,
  3848. bool txpending)
  3849. {
  3850. struct ieee80211_local *local = sdata->local;
  3851. struct sk_buff *next;
  3852. bool ret = true;
  3853. if (ieee80211_queue_skb(local, sdata, sta, skb))
  3854. return true;
  3855. skb_list_walk_safe(skb, skb, next) {
  3856. skb_mark_not_on_list(skb);
  3857. if (!__ieee80211_tx_8023(sdata, skb, sta, txpending))
  3858. ret = false;
  3859. }
  3860. return ret;
  3861. }
  3862. static void ieee80211_8023_xmit(struct ieee80211_sub_if_data *sdata,
  3863. struct net_device *dev, struct sta_info *sta,
  3864. struct ieee80211_key *key, struct sk_buff *skb)
  3865. {
  3866. struct ieee80211_tx_info *info;
  3867. struct ieee80211_local *local = sdata->local;
  3868. struct tid_ampdu_tx *tid_tx;
  3869. struct sk_buff *seg, *next;
  3870. unsigned int skbs = 0, len = 0;
  3871. u16 queue;
  3872. u8 tid;
  3873. queue = ieee80211_select_queue(sdata, sta, skb);
  3874. skb_set_queue_mapping(skb, queue);
  3875. if (unlikely(test_bit(SCAN_SW_SCANNING, &local->scanning)) &&
  3876. test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
  3877. goto out_free;
  3878. skb = skb_share_check(skb, GFP_ATOMIC);
  3879. if (unlikely(!skb))
  3880. return;
  3881. ieee80211_aggr_check(sdata, sta, skb);
  3882. tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
  3883. tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
  3884. if (tid_tx) {
  3885. if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
  3886. /* fall back to non-offload slow path */
  3887. __ieee80211_subif_start_xmit(skb, dev, 0,
  3888. IEEE80211_TX_CTRL_MLO_LINK_UNSPEC,
  3889. NULL);
  3890. return;
  3891. }
  3892. if (tid_tx->timeout)
  3893. tid_tx->last_tx = jiffies;
  3894. }
  3895. skb = ieee80211_tx_skb_fixup(skb, ieee80211_sdata_netdev_features(sdata));
  3896. if (!skb)
  3897. return;
  3898. info = IEEE80211_SKB_CB(skb);
  3899. memset(info, 0, sizeof(*info));
  3900. info->hw_queue = sdata->vif.hw_queue[queue];
  3901. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  3902. sdata = container_of(sdata->bss,
  3903. struct ieee80211_sub_if_data, u.ap);
  3904. info->flags |= IEEE80211_TX_CTL_HW_80211_ENCAP;
  3905. info->control.vif = &sdata->vif;
  3906. if (key)
  3907. info->control.hw_key = &key->conf;
  3908. skb_list_walk_safe(skb, seg, next) {
  3909. skbs++;
  3910. len += seg->len;
  3911. if (seg != skb)
  3912. memcpy(IEEE80211_SKB_CB(seg), info, sizeof(*info));
  3913. }
  3914. if (unlikely(skb->sk &&
  3915. skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)) {
  3916. info->status_data = ieee80211_store_ack_skb(local, skb,
  3917. &info->flags, NULL);
  3918. if (info->status_data)
  3919. info->status_data_idr = 1;
  3920. }
  3921. dev_sw_netstats_tx_add(dev, skbs, len);
  3922. sta->deflink.tx_stats.packets[queue] += skbs;
  3923. sta->deflink.tx_stats.bytes[queue] += len;
  3924. ieee80211_tpt_led_trig_tx(local, len);
  3925. ieee80211_tx_8023(sdata, skb, sta, false);
  3926. return;
  3927. out_free:
  3928. kfree_skb(skb);
  3929. }
  3930. netdev_tx_t ieee80211_subif_start_xmit_8023(struct sk_buff *skb,
  3931. struct net_device *dev)
  3932. {
  3933. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  3934. struct ethhdr *ehdr = (struct ethhdr *)skb->data;
  3935. struct ieee80211_key *key;
  3936. struct sta_info *sta;
  3937. if (unlikely(!ieee80211_sdata_running(sdata) || skb->len < ETH_HLEN)) {
  3938. kfree_skb(skb);
  3939. return NETDEV_TX_OK;
  3940. }
  3941. rcu_read_lock();
  3942. if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
  3943. kfree_skb(skb);
  3944. goto out;
  3945. }
  3946. if (unlikely(IS_ERR_OR_NULL(sta) || !sta->uploaded ||
  3947. !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
  3948. sdata->control_port_protocol == ehdr->h_proto))
  3949. goto skip_offload;
  3950. key = rcu_dereference(sta->ptk[sta->ptk_idx]);
  3951. if (!key)
  3952. key = rcu_dereference(sdata->default_unicast_key);
  3953. if (key && (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) ||
  3954. key->conf.cipher == WLAN_CIPHER_SUITE_TKIP))
  3955. goto skip_offload;
  3956. sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
  3957. ieee80211_8023_xmit(sdata, dev, sta, key, skb);
  3958. goto out;
  3959. skip_offload:
  3960. ieee80211_subif_start_xmit(skb, dev);
  3961. out:
  3962. rcu_read_unlock();
  3963. return NETDEV_TX_OK;
  3964. }
  3965. struct sk_buff *
  3966. ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
  3967. struct sk_buff *skb, u32 info_flags)
  3968. {
  3969. struct ieee80211_hdr *hdr;
  3970. struct ieee80211_tx_data tx = {
  3971. .local = sdata->local,
  3972. .sdata = sdata,
  3973. };
  3974. struct sta_info *sta;
  3975. rcu_read_lock();
  3976. if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
  3977. kfree_skb(skb);
  3978. skb = ERR_PTR(-EINVAL);
  3979. goto out;
  3980. }
  3981. skb = ieee80211_build_hdr(sdata, skb, info_flags, sta,
  3982. IEEE80211_TX_CTRL_MLO_LINK_UNSPEC, NULL);
  3983. if (IS_ERR(skb))
  3984. goto out;
  3985. hdr = (void *)skb->data;
  3986. tx.sta = sta_info_get(sdata, hdr->addr1);
  3987. tx.skb = skb;
  3988. if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
  3989. rcu_read_unlock();
  3990. kfree_skb(skb);
  3991. return ERR_PTR(-EINVAL);
  3992. }
  3993. out:
  3994. rcu_read_unlock();
  3995. return skb;
  3996. }
  3997. /*
  3998. * ieee80211_clear_tx_pending may not be called in a context where
  3999. * it is possible that it packets could come in again.
  4000. */
  4001. void ieee80211_clear_tx_pending(struct ieee80211_local *local)
  4002. {
  4003. struct sk_buff *skb;
  4004. int i;
  4005. for (i = 0; i < local->hw.queues; i++) {
  4006. while ((skb = skb_dequeue(&local->pending[i])) != NULL)
  4007. ieee80211_free_txskb(&local->hw, skb);
  4008. }
  4009. }
  4010. /*
  4011. * Returns false if the frame couldn't be transmitted but was queued instead,
  4012. * which in this case means re-queued -- take as an indication to stop sending
  4013. * more pending frames.
  4014. */
  4015. static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
  4016. struct sk_buff *skb)
  4017. {
  4018. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  4019. struct ieee80211_sub_if_data *sdata;
  4020. struct sta_info *sta;
  4021. struct ieee80211_hdr *hdr;
  4022. bool result;
  4023. struct ieee80211_chanctx_conf *chanctx_conf;
  4024. sdata = vif_to_sdata(info->control.vif);
  4025. if (info->control.flags & IEEE80211_TX_INTCFL_NEED_TXPROCESSING) {
  4026. /* update band only for non-MLD */
  4027. if (!ieee80211_vif_is_mld(&sdata->vif)) {
  4028. chanctx_conf =
  4029. rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
  4030. if (unlikely(!chanctx_conf)) {
  4031. dev_kfree_skb(skb);
  4032. return true;
  4033. }
  4034. info->band = chanctx_conf->def.chan->band;
  4035. }
  4036. result = ieee80211_tx(sdata, NULL, skb, true);
  4037. } else if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
  4038. if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
  4039. dev_kfree_skb(skb);
  4040. return true;
  4041. }
  4042. if (IS_ERR(sta) || (sta && !sta->uploaded))
  4043. sta = NULL;
  4044. result = ieee80211_tx_8023(sdata, skb, sta, true);
  4045. } else {
  4046. struct sk_buff_head skbs;
  4047. __skb_queue_head_init(&skbs);
  4048. __skb_queue_tail(&skbs, skb);
  4049. hdr = (struct ieee80211_hdr *)skb->data;
  4050. sta = sta_info_get(sdata, hdr->addr1);
  4051. result = __ieee80211_tx(local, &skbs, sta, true);
  4052. }
  4053. return result;
  4054. }
  4055. /*
  4056. * Transmit all pending packets. Called from tasklet.
  4057. */
  4058. void ieee80211_tx_pending(struct tasklet_struct *t)
  4059. {
  4060. struct ieee80211_local *local = from_tasklet(local, t,
  4061. tx_pending_tasklet);
  4062. unsigned long flags;
  4063. int i;
  4064. bool txok;
  4065. rcu_read_lock();
  4066. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  4067. for (i = 0; i < local->hw.queues; i++) {
  4068. /*
  4069. * If queue is stopped by something other than due to pending
  4070. * frames, or we have no pending frames, proceed to next queue.
  4071. */
  4072. if (local->queue_stop_reasons[i] ||
  4073. skb_queue_empty(&local->pending[i]))
  4074. continue;
  4075. while (!skb_queue_empty(&local->pending[i])) {
  4076. struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
  4077. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  4078. if (WARN_ON(!info->control.vif)) {
  4079. ieee80211_free_txskb(&local->hw, skb);
  4080. continue;
  4081. }
  4082. spin_unlock_irqrestore(&local->queue_stop_reason_lock,
  4083. flags);
  4084. txok = ieee80211_tx_pending_skb(local, skb);
  4085. spin_lock_irqsave(&local->queue_stop_reason_lock,
  4086. flags);
  4087. if (!txok)
  4088. break;
  4089. }
  4090. }
  4091. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  4092. rcu_read_unlock();
  4093. }
  4094. /* functions for drivers to get certain frames */
  4095. static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
  4096. struct ieee80211_link_data *link,
  4097. struct ps_data *ps, struct sk_buff *skb,
  4098. bool is_template)
  4099. {
  4100. u8 *pos, *tim;
  4101. int aid0 = 0;
  4102. int i, have_bits = 0, n1, n2;
  4103. struct ieee80211_bss_conf *link_conf = link->conf;
  4104. /* Generate bitmap for TIM only if there are any STAs in power save
  4105. * mode. */
  4106. if (atomic_read(&ps->num_sta_ps) > 0)
  4107. /* in the hope that this is faster than
  4108. * checking byte-for-byte */
  4109. have_bits = !bitmap_empty((unsigned long *)ps->tim,
  4110. IEEE80211_MAX_AID+1);
  4111. if (!is_template) {
  4112. if (ps->dtim_count == 0)
  4113. ps->dtim_count = link_conf->dtim_period - 1;
  4114. else
  4115. ps->dtim_count--;
  4116. }
  4117. tim = pos = skb_put(skb, 5);
  4118. *pos++ = WLAN_EID_TIM;
  4119. *pos++ = 3;
  4120. *pos++ = ps->dtim_count;
  4121. *pos++ = link_conf->dtim_period;
  4122. if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
  4123. aid0 = 1;
  4124. ps->dtim_bc_mc = aid0 == 1;
  4125. if (have_bits) {
  4126. /* Find largest even number N1 so that bits numbered 1 through
  4127. * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
  4128. * (N2 + 1) x 8 through 2007 are 0. */
  4129. n1 = 0;
  4130. for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
  4131. if (ps->tim[i]) {
  4132. n1 = i & 0xfe;
  4133. break;
  4134. }
  4135. }
  4136. n2 = n1;
  4137. for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
  4138. if (ps->tim[i]) {
  4139. n2 = i;
  4140. break;
  4141. }
  4142. }
  4143. /* Bitmap control */
  4144. *pos++ = n1 | aid0;
  4145. /* Part Virt Bitmap */
  4146. skb_put_data(skb, ps->tim + n1, n2 - n1 + 1);
  4147. tim[1] = n2 - n1 + 4;
  4148. } else {
  4149. *pos++ = aid0; /* Bitmap control */
  4150. if (ieee80211_get_link_sband(link)->band != NL80211_BAND_S1GHZ) {
  4151. tim[1] = 4;
  4152. /* Part Virt Bitmap */
  4153. skb_put_u8(skb, 0);
  4154. }
  4155. }
  4156. }
  4157. static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
  4158. struct ieee80211_link_data *link,
  4159. struct ps_data *ps, struct sk_buff *skb,
  4160. bool is_template)
  4161. {
  4162. struct ieee80211_local *local = sdata->local;
  4163. /*
  4164. * Not very nice, but we want to allow the driver to call
  4165. * ieee80211_beacon_get() as a response to the set_tim()
  4166. * callback. That, however, is already invoked under the
  4167. * sta_lock to guarantee consistent and race-free update
  4168. * of the tim bitmap in mac80211 and the driver.
  4169. */
  4170. if (local->tim_in_locked_section) {
  4171. __ieee80211_beacon_add_tim(sdata, link, ps, skb, is_template);
  4172. } else {
  4173. spin_lock_bh(&local->tim_lock);
  4174. __ieee80211_beacon_add_tim(sdata, link, ps, skb, is_template);
  4175. spin_unlock_bh(&local->tim_lock);
  4176. }
  4177. return 0;
  4178. }
  4179. static void ieee80211_set_beacon_cntdwn(struct ieee80211_sub_if_data *sdata,
  4180. struct beacon_data *beacon,
  4181. struct ieee80211_link_data *link)
  4182. {
  4183. u8 *beacon_data, count, max_count = 1;
  4184. struct probe_resp *resp;
  4185. size_t beacon_data_len;
  4186. u16 *bcn_offsets;
  4187. int i;
  4188. switch (sdata->vif.type) {
  4189. case NL80211_IFTYPE_AP:
  4190. beacon_data = beacon->tail;
  4191. beacon_data_len = beacon->tail_len;
  4192. break;
  4193. case NL80211_IFTYPE_ADHOC:
  4194. beacon_data = beacon->head;
  4195. beacon_data_len = beacon->head_len;
  4196. break;
  4197. case NL80211_IFTYPE_MESH_POINT:
  4198. beacon_data = beacon->head;
  4199. beacon_data_len = beacon->head_len;
  4200. break;
  4201. default:
  4202. return;
  4203. }
  4204. resp = rcu_dereference(link->u.ap.probe_resp);
  4205. bcn_offsets = beacon->cntdwn_counter_offsets;
  4206. count = beacon->cntdwn_current_counter;
  4207. if (link->conf->csa_active)
  4208. max_count = IEEE80211_MAX_CNTDWN_COUNTERS_NUM;
  4209. for (i = 0; i < max_count; ++i) {
  4210. if (bcn_offsets[i]) {
  4211. if (WARN_ON_ONCE(bcn_offsets[i] >= beacon_data_len))
  4212. return;
  4213. beacon_data[bcn_offsets[i]] = count;
  4214. }
  4215. if (sdata->vif.type == NL80211_IFTYPE_AP && resp) {
  4216. u16 *resp_offsets = resp->cntdwn_counter_offsets;
  4217. resp->data[resp_offsets[i]] = count;
  4218. }
  4219. }
  4220. }
  4221. static u8 __ieee80211_beacon_update_cntdwn(struct beacon_data *beacon)
  4222. {
  4223. beacon->cntdwn_current_counter--;
  4224. /* the counter should never reach 0 */
  4225. WARN_ON_ONCE(!beacon->cntdwn_current_counter);
  4226. return beacon->cntdwn_current_counter;
  4227. }
  4228. u8 ieee80211_beacon_update_cntdwn(struct ieee80211_vif *vif, unsigned int link_id)
  4229. {
  4230. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  4231. struct ieee80211_link_data *link;
  4232. struct beacon_data *beacon = NULL;
  4233. u8 count = 0;
  4234. if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
  4235. return 0;
  4236. rcu_read_lock();
  4237. link = rcu_dereference(sdata->link[link_id]);
  4238. if (!link)
  4239. goto unlock;
  4240. if (sdata->vif.type == NL80211_IFTYPE_AP)
  4241. beacon = rcu_dereference(link->u.ap.beacon);
  4242. else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  4243. beacon = rcu_dereference(sdata->u.ibss.presp);
  4244. else if (ieee80211_vif_is_mesh(&sdata->vif))
  4245. beacon = rcu_dereference(sdata->u.mesh.beacon);
  4246. if (!beacon)
  4247. goto unlock;
  4248. count = __ieee80211_beacon_update_cntdwn(beacon);
  4249. unlock:
  4250. rcu_read_unlock();
  4251. return count;
  4252. }
  4253. EXPORT_SYMBOL(ieee80211_beacon_update_cntdwn);
  4254. void ieee80211_beacon_set_cntdwn(struct ieee80211_vif *vif, u8 counter)
  4255. {
  4256. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  4257. struct beacon_data *beacon = NULL;
  4258. rcu_read_lock();
  4259. if (sdata->vif.type == NL80211_IFTYPE_AP)
  4260. beacon = rcu_dereference(sdata->deflink.u.ap.beacon);
  4261. else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  4262. beacon = rcu_dereference(sdata->u.ibss.presp);
  4263. else if (ieee80211_vif_is_mesh(&sdata->vif))
  4264. beacon = rcu_dereference(sdata->u.mesh.beacon);
  4265. if (!beacon)
  4266. goto unlock;
  4267. if (counter < beacon->cntdwn_current_counter)
  4268. beacon->cntdwn_current_counter = counter;
  4269. unlock:
  4270. rcu_read_unlock();
  4271. }
  4272. EXPORT_SYMBOL(ieee80211_beacon_set_cntdwn);
  4273. bool ieee80211_beacon_cntdwn_is_complete(struct ieee80211_vif *vif,
  4274. unsigned int link_id)
  4275. {
  4276. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  4277. struct ieee80211_link_data *link;
  4278. struct beacon_data *beacon = NULL;
  4279. u8 *beacon_data;
  4280. size_t beacon_data_len;
  4281. int ret = false;
  4282. if (!ieee80211_sdata_running(sdata))
  4283. return false;
  4284. if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
  4285. return 0;
  4286. rcu_read_lock();
  4287. link = rcu_dereference(sdata->link[link_id]);
  4288. if (!link)
  4289. goto out;
  4290. if (vif->type == NL80211_IFTYPE_AP) {
  4291. beacon = rcu_dereference(link->u.ap.beacon);
  4292. if (WARN_ON(!beacon || !beacon->tail))
  4293. goto out;
  4294. beacon_data = beacon->tail;
  4295. beacon_data_len = beacon->tail_len;
  4296. } else if (vif->type == NL80211_IFTYPE_ADHOC) {
  4297. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  4298. beacon = rcu_dereference(ifibss->presp);
  4299. if (!beacon)
  4300. goto out;
  4301. beacon_data = beacon->head;
  4302. beacon_data_len = beacon->head_len;
  4303. } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
  4304. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  4305. beacon = rcu_dereference(ifmsh->beacon);
  4306. if (!beacon)
  4307. goto out;
  4308. beacon_data = beacon->head;
  4309. beacon_data_len = beacon->head_len;
  4310. } else {
  4311. WARN_ON(1);
  4312. goto out;
  4313. }
  4314. if (!beacon->cntdwn_counter_offsets[0])
  4315. goto out;
  4316. if (WARN_ON_ONCE(beacon->cntdwn_counter_offsets[0] > beacon_data_len))
  4317. goto out;
  4318. if (beacon_data[beacon->cntdwn_counter_offsets[0]] == 1)
  4319. ret = true;
  4320. out:
  4321. rcu_read_unlock();
  4322. return ret;
  4323. }
  4324. EXPORT_SYMBOL(ieee80211_beacon_cntdwn_is_complete);
  4325. static int ieee80211_beacon_protect(struct sk_buff *skb,
  4326. struct ieee80211_local *local,
  4327. struct ieee80211_sub_if_data *sdata,
  4328. struct ieee80211_link_data *link)
  4329. {
  4330. ieee80211_tx_result res;
  4331. struct ieee80211_tx_data tx;
  4332. struct sk_buff *check_skb;
  4333. memset(&tx, 0, sizeof(tx));
  4334. tx.key = rcu_dereference(link->default_beacon_key);
  4335. if (!tx.key)
  4336. return 0;
  4337. if (unlikely(tx.key->flags & KEY_FLAG_TAINTED)) {
  4338. tx.key = NULL;
  4339. return -EINVAL;
  4340. }
  4341. if (!(tx.key->conf.flags & IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
  4342. tx.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
  4343. IEEE80211_SKB_CB(skb)->control.hw_key = &tx.key->conf;
  4344. tx.local = local;
  4345. tx.sdata = sdata;
  4346. __skb_queue_head_init(&tx.skbs);
  4347. __skb_queue_tail(&tx.skbs, skb);
  4348. res = ieee80211_tx_h_encrypt(&tx);
  4349. check_skb = __skb_dequeue(&tx.skbs);
  4350. /* we may crash after this, but it'd be a bug in crypto */
  4351. WARN_ON(check_skb != skb);
  4352. if (WARN_ON_ONCE(res != TX_CONTINUE))
  4353. return -EINVAL;
  4354. return 0;
  4355. }
  4356. static void
  4357. ieee80211_beacon_get_finish(struct ieee80211_hw *hw,
  4358. struct ieee80211_vif *vif,
  4359. struct ieee80211_link_data *link,
  4360. struct ieee80211_mutable_offsets *offs,
  4361. struct beacon_data *beacon,
  4362. struct sk_buff *skb,
  4363. struct ieee80211_chanctx_conf *chanctx_conf,
  4364. u16 csa_off_base)
  4365. {
  4366. struct ieee80211_local *local = hw_to_local(hw);
  4367. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  4368. struct ieee80211_tx_info *info;
  4369. enum nl80211_band band;
  4370. struct ieee80211_tx_rate_control txrc;
  4371. /* CSA offsets */
  4372. if (offs && beacon) {
  4373. u16 i;
  4374. for (i = 0; i < IEEE80211_MAX_CNTDWN_COUNTERS_NUM; i++) {
  4375. u16 csa_off = beacon->cntdwn_counter_offsets[i];
  4376. if (!csa_off)
  4377. continue;
  4378. offs->cntdwn_counter_offs[i] = csa_off_base + csa_off;
  4379. }
  4380. }
  4381. band = chanctx_conf->def.chan->band;
  4382. info = IEEE80211_SKB_CB(skb);
  4383. info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  4384. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  4385. info->band = band;
  4386. memset(&txrc, 0, sizeof(txrc));
  4387. txrc.hw = hw;
  4388. txrc.sband = local->hw.wiphy->bands[band];
  4389. txrc.bss_conf = link->conf;
  4390. txrc.skb = skb;
  4391. txrc.reported_rate.idx = -1;
  4392. if (sdata->beacon_rate_set && sdata->beacon_rateidx_mask[band])
  4393. txrc.rate_idx_mask = sdata->beacon_rateidx_mask[band];
  4394. else
  4395. txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
  4396. txrc.bss = true;
  4397. rate_control_get_rate(sdata, NULL, &txrc);
  4398. info->control.vif = vif;
  4399. info->control.flags |= u32_encode_bits(link->link_id,
  4400. IEEE80211_TX_CTRL_MLO_LINK);
  4401. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
  4402. IEEE80211_TX_CTL_ASSIGN_SEQ |
  4403. IEEE80211_TX_CTL_FIRST_FRAGMENT;
  4404. }
  4405. static void
  4406. ieee80211_beacon_add_mbssid(struct sk_buff *skb, struct beacon_data *beacon,
  4407. u8 i)
  4408. {
  4409. if (!beacon->mbssid_ies || !beacon->mbssid_ies->cnt ||
  4410. i > beacon->mbssid_ies->cnt)
  4411. return;
  4412. if (i < beacon->mbssid_ies->cnt) {
  4413. skb_put_data(skb, beacon->mbssid_ies->elem[i].data,
  4414. beacon->mbssid_ies->elem[i].len);
  4415. if (beacon->rnr_ies && beacon->rnr_ies->cnt) {
  4416. skb_put_data(skb, beacon->rnr_ies->elem[i].data,
  4417. beacon->rnr_ies->elem[i].len);
  4418. for (i = beacon->mbssid_ies->cnt; i < beacon->rnr_ies->cnt; i++)
  4419. skb_put_data(skb, beacon->rnr_ies->elem[i].data,
  4420. beacon->rnr_ies->elem[i].len);
  4421. }
  4422. return;
  4423. }
  4424. /* i == beacon->mbssid_ies->cnt, include all MBSSID elements */
  4425. for (i = 0; i < beacon->mbssid_ies->cnt; i++)
  4426. skb_put_data(skb, beacon->mbssid_ies->elem[i].data,
  4427. beacon->mbssid_ies->elem[i].len);
  4428. }
  4429. static struct sk_buff *
  4430. ieee80211_beacon_get_ap(struct ieee80211_hw *hw,
  4431. struct ieee80211_vif *vif,
  4432. struct ieee80211_link_data *link,
  4433. struct ieee80211_mutable_offsets *offs,
  4434. bool is_template,
  4435. struct beacon_data *beacon,
  4436. struct ieee80211_chanctx_conf *chanctx_conf,
  4437. u8 ema_index)
  4438. {
  4439. struct ieee80211_local *local = hw_to_local(hw);
  4440. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  4441. struct ieee80211_if_ap *ap = &sdata->u.ap;
  4442. struct sk_buff *skb = NULL;
  4443. u16 csa_off_base = 0;
  4444. int mbssid_len;
  4445. if (beacon->cntdwn_counter_offsets[0]) {
  4446. if (!is_template)
  4447. ieee80211_beacon_update_cntdwn(vif, link->link_id);
  4448. ieee80211_set_beacon_cntdwn(sdata, beacon, link);
  4449. }
  4450. /* headroom, head length,
  4451. * tail length, maximum TIM length and multiple BSSID length
  4452. */
  4453. mbssid_len = ieee80211_get_mbssid_beacon_len(beacon->mbssid_ies,
  4454. beacon->rnr_ies,
  4455. ema_index);
  4456. skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
  4457. beacon->tail_len + 256 +
  4458. local->hw.extra_beacon_tailroom + mbssid_len);
  4459. if (!skb)
  4460. return NULL;
  4461. skb_reserve(skb, local->tx_headroom);
  4462. skb_put_data(skb, beacon->head, beacon->head_len);
  4463. ieee80211_beacon_add_tim(sdata, link, &ap->ps, skb, is_template);
  4464. if (offs) {
  4465. offs->tim_offset = beacon->head_len;
  4466. offs->tim_length = skb->len - beacon->head_len;
  4467. offs->cntdwn_counter_offs[0] = beacon->cntdwn_counter_offsets[0];
  4468. if (mbssid_len) {
  4469. ieee80211_beacon_add_mbssid(skb, beacon, ema_index);
  4470. offs->mbssid_off = skb->len - mbssid_len;
  4471. }
  4472. /* for AP the csa offsets are from tail */
  4473. csa_off_base = skb->len;
  4474. }
  4475. if (beacon->tail)
  4476. skb_put_data(skb, beacon->tail, beacon->tail_len);
  4477. if (ieee80211_beacon_protect(skb, local, sdata, link) < 0) {
  4478. dev_kfree_skb(skb);
  4479. return NULL;
  4480. }
  4481. ieee80211_beacon_get_finish(hw, vif, link, offs, beacon, skb,
  4482. chanctx_conf, csa_off_base);
  4483. return skb;
  4484. }
  4485. static struct ieee80211_ema_beacons *
  4486. ieee80211_beacon_get_ap_ema_list(struct ieee80211_hw *hw,
  4487. struct ieee80211_vif *vif,
  4488. struct ieee80211_link_data *link,
  4489. struct ieee80211_mutable_offsets *offs,
  4490. bool is_template, struct beacon_data *beacon,
  4491. struct ieee80211_chanctx_conf *chanctx_conf)
  4492. {
  4493. struct ieee80211_ema_beacons *ema = NULL;
  4494. if (!beacon->mbssid_ies || !beacon->mbssid_ies->cnt)
  4495. return NULL;
  4496. ema = kzalloc(struct_size(ema, bcn, beacon->mbssid_ies->cnt),
  4497. GFP_ATOMIC);
  4498. if (!ema)
  4499. return NULL;
  4500. for (ema->cnt = 0; ema->cnt < beacon->mbssid_ies->cnt; ema->cnt++) {
  4501. ema->bcn[ema->cnt].skb =
  4502. ieee80211_beacon_get_ap(hw, vif, link,
  4503. &ema->bcn[ema->cnt].offs,
  4504. is_template, beacon,
  4505. chanctx_conf, ema->cnt);
  4506. if (!ema->bcn[ema->cnt].skb)
  4507. break;
  4508. }
  4509. if (ema->cnt == beacon->mbssid_ies->cnt)
  4510. return ema;
  4511. ieee80211_beacon_free_ema_list(ema);
  4512. return NULL;
  4513. }
  4514. #define IEEE80211_INCLUDE_ALL_MBSSID_ELEMS -1
  4515. static struct sk_buff *
  4516. __ieee80211_beacon_get(struct ieee80211_hw *hw,
  4517. struct ieee80211_vif *vif,
  4518. struct ieee80211_mutable_offsets *offs,
  4519. bool is_template,
  4520. unsigned int link_id,
  4521. int ema_index,
  4522. struct ieee80211_ema_beacons **ema_beacons)
  4523. {
  4524. struct ieee80211_local *local = hw_to_local(hw);
  4525. struct beacon_data *beacon = NULL;
  4526. struct sk_buff *skb = NULL;
  4527. struct ieee80211_sub_if_data *sdata = NULL;
  4528. struct ieee80211_chanctx_conf *chanctx_conf;
  4529. struct ieee80211_link_data *link;
  4530. rcu_read_lock();
  4531. sdata = vif_to_sdata(vif);
  4532. link = rcu_dereference(sdata->link[link_id]);
  4533. if (!link)
  4534. goto out;
  4535. chanctx_conf =
  4536. rcu_dereference(link->conf->chanctx_conf);
  4537. if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
  4538. goto out;
  4539. if (offs)
  4540. memset(offs, 0, sizeof(*offs));
  4541. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  4542. beacon = rcu_dereference(link->u.ap.beacon);
  4543. if (!beacon)
  4544. goto out;
  4545. if (ema_beacons) {
  4546. *ema_beacons =
  4547. ieee80211_beacon_get_ap_ema_list(hw, vif, link,
  4548. offs,
  4549. is_template,
  4550. beacon,
  4551. chanctx_conf);
  4552. } else {
  4553. if (beacon->mbssid_ies && beacon->mbssid_ies->cnt) {
  4554. if (ema_index >= beacon->mbssid_ies->cnt)
  4555. goto out; /* End of MBSSID elements */
  4556. if (ema_index <= IEEE80211_INCLUDE_ALL_MBSSID_ELEMS)
  4557. ema_index = beacon->mbssid_ies->cnt;
  4558. } else {
  4559. ema_index = 0;
  4560. }
  4561. skb = ieee80211_beacon_get_ap(hw, vif, link, offs,
  4562. is_template, beacon,
  4563. chanctx_conf,
  4564. ema_index);
  4565. }
  4566. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  4567. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  4568. struct ieee80211_hdr *hdr;
  4569. beacon = rcu_dereference(ifibss->presp);
  4570. if (!beacon)
  4571. goto out;
  4572. if (beacon->cntdwn_counter_offsets[0]) {
  4573. if (!is_template)
  4574. __ieee80211_beacon_update_cntdwn(beacon);
  4575. ieee80211_set_beacon_cntdwn(sdata, beacon, link);
  4576. }
  4577. skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
  4578. local->hw.extra_beacon_tailroom);
  4579. if (!skb)
  4580. goto out;
  4581. skb_reserve(skb, local->tx_headroom);
  4582. skb_put_data(skb, beacon->head, beacon->head_len);
  4583. hdr = (struct ieee80211_hdr *) skb->data;
  4584. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  4585. IEEE80211_STYPE_BEACON);
  4586. ieee80211_beacon_get_finish(hw, vif, link, offs, beacon, skb,
  4587. chanctx_conf, 0);
  4588. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  4589. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  4590. beacon = rcu_dereference(ifmsh->beacon);
  4591. if (!beacon)
  4592. goto out;
  4593. if (beacon->cntdwn_counter_offsets[0]) {
  4594. if (!is_template)
  4595. /* TODO: For mesh csa_counter is in TU, so
  4596. * decrementing it by one isn't correct, but
  4597. * for now we leave it consistent with overall
  4598. * mac80211's behavior.
  4599. */
  4600. __ieee80211_beacon_update_cntdwn(beacon);
  4601. ieee80211_set_beacon_cntdwn(sdata, beacon, link);
  4602. }
  4603. if (ifmsh->sync_ops)
  4604. ifmsh->sync_ops->adjust_tsf(sdata, beacon);
  4605. skb = dev_alloc_skb(local->tx_headroom +
  4606. beacon->head_len +
  4607. 256 + /* TIM IE */
  4608. beacon->tail_len +
  4609. local->hw.extra_beacon_tailroom);
  4610. if (!skb)
  4611. goto out;
  4612. skb_reserve(skb, local->tx_headroom);
  4613. skb_put_data(skb, beacon->head, beacon->head_len);
  4614. ieee80211_beacon_add_tim(sdata, link, &ifmsh->ps, skb,
  4615. is_template);
  4616. if (offs) {
  4617. offs->tim_offset = beacon->head_len;
  4618. offs->tim_length = skb->len - beacon->head_len;
  4619. }
  4620. skb_put_data(skb, beacon->tail, beacon->tail_len);
  4621. ieee80211_beacon_get_finish(hw, vif, link, offs, beacon, skb,
  4622. chanctx_conf, 0);
  4623. } else {
  4624. WARN_ON(1);
  4625. goto out;
  4626. }
  4627. out:
  4628. rcu_read_unlock();
  4629. return skb;
  4630. }
  4631. struct sk_buff *
  4632. ieee80211_beacon_get_template(struct ieee80211_hw *hw,
  4633. struct ieee80211_vif *vif,
  4634. struct ieee80211_mutable_offsets *offs,
  4635. unsigned int link_id)
  4636. {
  4637. return __ieee80211_beacon_get(hw, vif, offs, true, link_id,
  4638. IEEE80211_INCLUDE_ALL_MBSSID_ELEMS, NULL);
  4639. }
  4640. EXPORT_SYMBOL(ieee80211_beacon_get_template);
  4641. struct sk_buff *
  4642. ieee80211_beacon_get_template_ema_index(struct ieee80211_hw *hw,
  4643. struct ieee80211_vif *vif,
  4644. struct ieee80211_mutable_offsets *offs,
  4645. unsigned int link_id, u8 ema_index)
  4646. {
  4647. return __ieee80211_beacon_get(hw, vif, offs, true, link_id, ema_index,
  4648. NULL);
  4649. }
  4650. EXPORT_SYMBOL(ieee80211_beacon_get_template_ema_index);
  4651. void ieee80211_beacon_free_ema_list(struct ieee80211_ema_beacons *ema_beacons)
  4652. {
  4653. u8 i;
  4654. if (!ema_beacons)
  4655. return;
  4656. for (i = 0; i < ema_beacons->cnt; i++)
  4657. kfree_skb(ema_beacons->bcn[i].skb);
  4658. kfree(ema_beacons);
  4659. }
  4660. EXPORT_SYMBOL(ieee80211_beacon_free_ema_list);
  4661. struct ieee80211_ema_beacons *
  4662. ieee80211_beacon_get_template_ema_list(struct ieee80211_hw *hw,
  4663. struct ieee80211_vif *vif,
  4664. unsigned int link_id)
  4665. {
  4666. struct ieee80211_ema_beacons *ema_beacons = NULL;
  4667. WARN_ON(__ieee80211_beacon_get(hw, vif, NULL, true, link_id, 0,
  4668. &ema_beacons));
  4669. return ema_beacons;
  4670. }
  4671. EXPORT_SYMBOL(ieee80211_beacon_get_template_ema_list);
  4672. struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
  4673. struct ieee80211_vif *vif,
  4674. u16 *tim_offset, u16 *tim_length,
  4675. unsigned int link_id)
  4676. {
  4677. struct ieee80211_mutable_offsets offs = {};
  4678. struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false,
  4679. link_id,
  4680. IEEE80211_INCLUDE_ALL_MBSSID_ELEMS,
  4681. NULL);
  4682. struct sk_buff *copy;
  4683. if (!bcn)
  4684. return bcn;
  4685. if (tim_offset)
  4686. *tim_offset = offs.tim_offset;
  4687. if (tim_length)
  4688. *tim_length = offs.tim_length;
  4689. if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
  4690. !hw_to_local(hw)->monitors)
  4691. return bcn;
  4692. /* send a copy to monitor interfaces */
  4693. copy = skb_copy(bcn, GFP_ATOMIC);
  4694. if (!copy)
  4695. return bcn;
  4696. ieee80211_tx_monitor(hw_to_local(hw), copy, 1, false, NULL);
  4697. return bcn;
  4698. }
  4699. EXPORT_SYMBOL(ieee80211_beacon_get_tim);
  4700. struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
  4701. struct ieee80211_vif *vif)
  4702. {
  4703. struct sk_buff *skb = NULL;
  4704. struct probe_resp *presp = NULL;
  4705. struct ieee80211_hdr *hdr;
  4706. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  4707. if (sdata->vif.type != NL80211_IFTYPE_AP)
  4708. return NULL;
  4709. rcu_read_lock();
  4710. presp = rcu_dereference(sdata->deflink.u.ap.probe_resp);
  4711. if (!presp)
  4712. goto out;
  4713. skb = dev_alloc_skb(presp->len);
  4714. if (!skb)
  4715. goto out;
  4716. skb_put_data(skb, presp->data, presp->len);
  4717. hdr = (struct ieee80211_hdr *) skb->data;
  4718. memset(hdr->addr1, 0, sizeof(hdr->addr1));
  4719. out:
  4720. rcu_read_unlock();
  4721. return skb;
  4722. }
  4723. EXPORT_SYMBOL(ieee80211_proberesp_get);
  4724. struct sk_buff *ieee80211_get_fils_discovery_tmpl(struct ieee80211_hw *hw,
  4725. struct ieee80211_vif *vif)
  4726. {
  4727. struct sk_buff *skb = NULL;
  4728. struct fils_discovery_data *tmpl = NULL;
  4729. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  4730. if (sdata->vif.type != NL80211_IFTYPE_AP)
  4731. return NULL;
  4732. rcu_read_lock();
  4733. tmpl = rcu_dereference(sdata->deflink.u.ap.fils_discovery);
  4734. if (!tmpl) {
  4735. rcu_read_unlock();
  4736. return NULL;
  4737. }
  4738. skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len);
  4739. if (skb) {
  4740. skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
  4741. skb_put_data(skb, tmpl->data, tmpl->len);
  4742. }
  4743. rcu_read_unlock();
  4744. return skb;
  4745. }
  4746. EXPORT_SYMBOL(ieee80211_get_fils_discovery_tmpl);
  4747. struct sk_buff *
  4748. ieee80211_get_unsol_bcast_probe_resp_tmpl(struct ieee80211_hw *hw,
  4749. struct ieee80211_vif *vif)
  4750. {
  4751. struct sk_buff *skb = NULL;
  4752. struct unsol_bcast_probe_resp_data *tmpl = NULL;
  4753. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  4754. if (sdata->vif.type != NL80211_IFTYPE_AP)
  4755. return NULL;
  4756. rcu_read_lock();
  4757. tmpl = rcu_dereference(sdata->deflink.u.ap.unsol_bcast_probe_resp);
  4758. if (!tmpl) {
  4759. rcu_read_unlock();
  4760. return NULL;
  4761. }
  4762. skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len);
  4763. if (skb) {
  4764. skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
  4765. skb_put_data(skb, tmpl->data, tmpl->len);
  4766. }
  4767. rcu_read_unlock();
  4768. return skb;
  4769. }
  4770. EXPORT_SYMBOL(ieee80211_get_unsol_bcast_probe_resp_tmpl);
  4771. struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
  4772. struct ieee80211_vif *vif)
  4773. {
  4774. struct ieee80211_sub_if_data *sdata;
  4775. struct ieee80211_pspoll *pspoll;
  4776. struct ieee80211_local *local;
  4777. struct sk_buff *skb;
  4778. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  4779. return NULL;
  4780. sdata = vif_to_sdata(vif);
  4781. local = sdata->local;
  4782. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
  4783. if (!skb)
  4784. return NULL;
  4785. skb_reserve(skb, local->hw.extra_tx_headroom);
  4786. pspoll = skb_put_zero(skb, sizeof(*pspoll));
  4787. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  4788. IEEE80211_STYPE_PSPOLL);
  4789. pspoll->aid = cpu_to_le16(sdata->vif.cfg.aid);
  4790. /* aid in PS-Poll has its two MSBs each set to 1 */
  4791. pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
  4792. memcpy(pspoll->bssid, sdata->deflink.u.mgd.bssid, ETH_ALEN);
  4793. memcpy(pspoll->ta, vif->addr, ETH_ALEN);
  4794. return skb;
  4795. }
  4796. EXPORT_SYMBOL(ieee80211_pspoll_get);
  4797. struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
  4798. struct ieee80211_vif *vif,
  4799. int link_id, bool qos_ok)
  4800. {
  4801. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  4802. struct ieee80211_local *local = sdata->local;
  4803. struct ieee80211_link_data *link = NULL;
  4804. struct ieee80211_hdr_3addr *nullfunc;
  4805. struct sk_buff *skb;
  4806. bool qos = false;
  4807. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  4808. return NULL;
  4809. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  4810. sizeof(*nullfunc) + 2);
  4811. if (!skb)
  4812. return NULL;
  4813. rcu_read_lock();
  4814. if (qos_ok) {
  4815. struct sta_info *sta;
  4816. sta = sta_info_get(sdata, vif->cfg.ap_addr);
  4817. qos = sta && sta->sta.wme;
  4818. }
  4819. if (link_id >= 0) {
  4820. link = rcu_dereference(sdata->link[link_id]);
  4821. if (WARN_ON_ONCE(!link)) {
  4822. rcu_read_unlock();
  4823. kfree_skb(skb);
  4824. return NULL;
  4825. }
  4826. }
  4827. skb_reserve(skb, local->hw.extra_tx_headroom);
  4828. nullfunc = skb_put_zero(skb, sizeof(*nullfunc));
  4829. nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  4830. IEEE80211_STYPE_NULLFUNC |
  4831. IEEE80211_FCTL_TODS);
  4832. if (qos) {
  4833. __le16 qoshdr = cpu_to_le16(7);
  4834. BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC |
  4835. IEEE80211_STYPE_NULLFUNC) !=
  4836. IEEE80211_STYPE_QOS_NULLFUNC);
  4837. nullfunc->frame_control |=
  4838. cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC);
  4839. skb->priority = 7;
  4840. skb_set_queue_mapping(skb, IEEE80211_AC_VO);
  4841. skb_put_data(skb, &qoshdr, sizeof(qoshdr));
  4842. }
  4843. if (link) {
  4844. memcpy(nullfunc->addr1, link->conf->bssid, ETH_ALEN);
  4845. memcpy(nullfunc->addr2, link->conf->addr, ETH_ALEN);
  4846. memcpy(nullfunc->addr3, link->conf->bssid, ETH_ALEN);
  4847. } else {
  4848. memcpy(nullfunc->addr1, vif->cfg.ap_addr, ETH_ALEN);
  4849. memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
  4850. memcpy(nullfunc->addr3, vif->cfg.ap_addr, ETH_ALEN);
  4851. }
  4852. rcu_read_unlock();
  4853. return skb;
  4854. }
  4855. EXPORT_SYMBOL(ieee80211_nullfunc_get);
  4856. struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
  4857. const u8 *src_addr,
  4858. const u8 *ssid, size_t ssid_len,
  4859. size_t tailroom)
  4860. {
  4861. struct ieee80211_local *local = hw_to_local(hw);
  4862. struct ieee80211_hdr_3addr *hdr;
  4863. struct sk_buff *skb;
  4864. size_t ie_ssid_len;
  4865. u8 *pos;
  4866. ie_ssid_len = 2 + ssid_len;
  4867. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
  4868. ie_ssid_len + tailroom);
  4869. if (!skb)
  4870. return NULL;
  4871. skb_reserve(skb, local->hw.extra_tx_headroom);
  4872. hdr = skb_put_zero(skb, sizeof(*hdr));
  4873. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  4874. IEEE80211_STYPE_PROBE_REQ);
  4875. eth_broadcast_addr(hdr->addr1);
  4876. memcpy(hdr->addr2, src_addr, ETH_ALEN);
  4877. eth_broadcast_addr(hdr->addr3);
  4878. pos = skb_put(skb, ie_ssid_len);
  4879. *pos++ = WLAN_EID_SSID;
  4880. *pos++ = ssid_len;
  4881. if (ssid_len)
  4882. memcpy(pos, ssid, ssid_len);
  4883. pos += ssid_len;
  4884. return skb;
  4885. }
  4886. EXPORT_SYMBOL(ieee80211_probereq_get);
  4887. void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4888. const void *frame, size_t frame_len,
  4889. const struct ieee80211_tx_info *frame_txctl,
  4890. struct ieee80211_rts *rts)
  4891. {
  4892. const struct ieee80211_hdr *hdr = frame;
  4893. rts->frame_control =
  4894. cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
  4895. rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
  4896. frame_txctl);
  4897. memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
  4898. memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
  4899. }
  4900. EXPORT_SYMBOL(ieee80211_rts_get);
  4901. void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4902. const void *frame, size_t frame_len,
  4903. const struct ieee80211_tx_info *frame_txctl,
  4904. struct ieee80211_cts *cts)
  4905. {
  4906. const struct ieee80211_hdr *hdr = frame;
  4907. cts->frame_control =
  4908. cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
  4909. cts->duration = ieee80211_ctstoself_duration(hw, vif,
  4910. frame_len, frame_txctl);
  4911. memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
  4912. }
  4913. EXPORT_SYMBOL(ieee80211_ctstoself_get);
  4914. struct sk_buff *
  4915. ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
  4916. struct ieee80211_vif *vif)
  4917. {
  4918. struct ieee80211_local *local = hw_to_local(hw);
  4919. struct sk_buff *skb = NULL;
  4920. struct ieee80211_tx_data tx;
  4921. struct ieee80211_sub_if_data *sdata;
  4922. struct ps_data *ps;
  4923. struct ieee80211_tx_info *info;
  4924. struct ieee80211_chanctx_conf *chanctx_conf;
  4925. sdata = vif_to_sdata(vif);
  4926. rcu_read_lock();
  4927. chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
  4928. if (!chanctx_conf)
  4929. goto out;
  4930. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  4931. struct beacon_data *beacon =
  4932. rcu_dereference(sdata->deflink.u.ap.beacon);
  4933. if (!beacon || !beacon->head)
  4934. goto out;
  4935. ps = &sdata->u.ap.ps;
  4936. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  4937. ps = &sdata->u.mesh.ps;
  4938. } else {
  4939. goto out;
  4940. }
  4941. if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
  4942. goto out; /* send buffered bc/mc only after DTIM beacon */
  4943. while (1) {
  4944. skb = skb_dequeue(&ps->bc_buf);
  4945. if (!skb)
  4946. goto out;
  4947. local->total_ps_buffered--;
  4948. if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
  4949. struct ieee80211_hdr *hdr =
  4950. (struct ieee80211_hdr *) skb->data;
  4951. /* more buffered multicast/broadcast frames ==> set
  4952. * MoreData flag in IEEE 802.11 header to inform PS
  4953. * STAs */
  4954. hdr->frame_control |=
  4955. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  4956. }
  4957. if (sdata->vif.type == NL80211_IFTYPE_AP)
  4958. sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
  4959. if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
  4960. break;
  4961. ieee80211_free_txskb(hw, skb);
  4962. }
  4963. info = IEEE80211_SKB_CB(skb);
  4964. tx.flags |= IEEE80211_TX_PS_BUFFERED;
  4965. info->band = chanctx_conf->def.chan->band;
  4966. if (invoke_tx_handlers(&tx))
  4967. skb = NULL;
  4968. out:
  4969. rcu_read_unlock();
  4970. return skb;
  4971. }
  4972. EXPORT_SYMBOL(ieee80211_get_buffered_bc);
  4973. int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
  4974. {
  4975. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  4976. struct ieee80211_sub_if_data *sdata = sta->sdata;
  4977. struct ieee80211_local *local = sdata->local;
  4978. int ret;
  4979. u32 queues;
  4980. lockdep_assert_wiphy(local->hw.wiphy);
  4981. /* only some cases are supported right now */
  4982. switch (sdata->vif.type) {
  4983. case NL80211_IFTYPE_STATION:
  4984. case NL80211_IFTYPE_AP:
  4985. case NL80211_IFTYPE_AP_VLAN:
  4986. break;
  4987. default:
  4988. WARN_ON(1);
  4989. return -EINVAL;
  4990. }
  4991. if (WARN_ON(tid >= IEEE80211_NUM_UPS))
  4992. return -EINVAL;
  4993. if (sta->reserved_tid == tid) {
  4994. ret = 0;
  4995. goto out;
  4996. }
  4997. if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
  4998. sdata_err(sdata, "TID reservation already active\n");
  4999. ret = -EALREADY;
  5000. goto out;
  5001. }
  5002. ieee80211_stop_vif_queues(sdata->local, sdata,
  5003. IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
  5004. synchronize_net();
  5005. /* Tear down BA sessions so we stop aggregating on this TID */
  5006. if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
  5007. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  5008. __ieee80211_stop_tx_ba_session(sta, tid,
  5009. AGG_STOP_LOCAL_REQUEST);
  5010. }
  5011. queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
  5012. __ieee80211_flush_queues(local, sdata, queues, false);
  5013. sta->reserved_tid = tid;
  5014. ieee80211_wake_vif_queues(local, sdata,
  5015. IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
  5016. if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
  5017. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  5018. ret = 0;
  5019. out:
  5020. return ret;
  5021. }
  5022. EXPORT_SYMBOL(ieee80211_reserve_tid);
  5023. void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
  5024. {
  5025. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  5026. struct ieee80211_sub_if_data *sdata = sta->sdata;
  5027. lockdep_assert_wiphy(sdata->local->hw.wiphy);
  5028. /* only some cases are supported right now */
  5029. switch (sdata->vif.type) {
  5030. case NL80211_IFTYPE_STATION:
  5031. case NL80211_IFTYPE_AP:
  5032. case NL80211_IFTYPE_AP_VLAN:
  5033. break;
  5034. default:
  5035. WARN_ON(1);
  5036. return;
  5037. }
  5038. if (tid != sta->reserved_tid) {
  5039. sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
  5040. return;
  5041. }
  5042. sta->reserved_tid = IEEE80211_TID_UNRESERVED;
  5043. }
  5044. EXPORT_SYMBOL(ieee80211_unreserve_tid);
  5045. void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
  5046. struct sk_buff *skb, int tid, int link_id,
  5047. enum nl80211_band band)
  5048. {
  5049. const struct ieee80211_hdr *hdr = (void *)skb->data;
  5050. int ac = ieee80211_ac_from_tid(tid);
  5051. unsigned int link;
  5052. skb_reset_mac_header(skb);
  5053. skb_set_queue_mapping(skb, ac);
  5054. skb->priority = tid;
  5055. skb->dev = sdata->dev;
  5056. BUILD_BUG_ON(IEEE80211_LINK_UNSPECIFIED < IEEE80211_MLD_MAX_NUM_LINKS);
  5057. BUILD_BUG_ON(!FIELD_FIT(IEEE80211_TX_CTRL_MLO_LINK,
  5058. IEEE80211_LINK_UNSPECIFIED));
  5059. if (!ieee80211_vif_is_mld(&sdata->vif)) {
  5060. link = 0;
  5061. } else if (link_id >= 0) {
  5062. link = link_id;
  5063. } else if (memcmp(sdata->vif.addr, hdr->addr2, ETH_ALEN) == 0) {
  5064. /* address from the MLD */
  5065. link = IEEE80211_LINK_UNSPECIFIED;
  5066. } else {
  5067. /* otherwise must be addressed from a link */
  5068. rcu_read_lock();
  5069. for (link = 0; link < ARRAY_SIZE(sdata->vif.link_conf); link++) {
  5070. struct ieee80211_bss_conf *link_conf;
  5071. link_conf = rcu_dereference(sdata->vif.link_conf[link]);
  5072. if (!link_conf)
  5073. continue;
  5074. if (memcmp(link_conf->addr, hdr->addr2, ETH_ALEN) == 0)
  5075. break;
  5076. }
  5077. rcu_read_unlock();
  5078. if (WARN_ON_ONCE(link == ARRAY_SIZE(sdata->vif.link_conf)))
  5079. link = ffs(sdata->vif.active_links) - 1;
  5080. }
  5081. IEEE80211_SKB_CB(skb)->control.flags |=
  5082. u32_encode_bits(link, IEEE80211_TX_CTRL_MLO_LINK);
  5083. /*
  5084. * The other path calling ieee80211_xmit is from the tasklet,
  5085. * and while we can handle concurrent transmissions locking
  5086. * requirements are that we do not come into tx with bhs on.
  5087. */
  5088. local_bh_disable();
  5089. IEEE80211_SKB_CB(skb)->band = band;
  5090. ieee80211_xmit(sdata, NULL, skb);
  5091. local_bh_enable();
  5092. }
  5093. void ieee80211_tx_skb_tid(struct ieee80211_sub_if_data *sdata,
  5094. struct sk_buff *skb, int tid, int link_id)
  5095. {
  5096. struct ieee80211_chanctx_conf *chanctx_conf;
  5097. enum nl80211_band band;
  5098. rcu_read_lock();
  5099. if (!ieee80211_vif_is_mld(&sdata->vif)) {
  5100. WARN_ON(link_id >= 0);
  5101. chanctx_conf =
  5102. rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
  5103. if (WARN_ON(!chanctx_conf)) {
  5104. rcu_read_unlock();
  5105. kfree_skb(skb);
  5106. return;
  5107. }
  5108. band = chanctx_conf->def.chan->band;
  5109. } else {
  5110. WARN_ON(link_id >= 0 &&
  5111. !(sdata->vif.active_links & BIT(link_id)));
  5112. /* MLD transmissions must not rely on the band */
  5113. band = 0;
  5114. }
  5115. __ieee80211_tx_skb_tid_band(sdata, skb, tid, link_id, band);
  5116. rcu_read_unlock();
  5117. }
  5118. int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
  5119. const u8 *buf, size_t len,
  5120. const u8 *dest, __be16 proto, bool unencrypted,
  5121. int link_id, u64 *cookie)
  5122. {
  5123. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  5124. struct ieee80211_local *local = sdata->local;
  5125. struct sta_info *sta;
  5126. struct sk_buff *skb;
  5127. struct ethhdr *ehdr;
  5128. u32 ctrl_flags = 0;
  5129. u32 flags = 0;
  5130. int err;
  5131. /* mutex lock is only needed for incrementing the cookie counter */
  5132. lockdep_assert_wiphy(local->hw.wiphy);
  5133. /* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE
  5134. * or Pre-Authentication
  5135. */
  5136. if (proto != sdata->control_port_protocol &&
  5137. proto != cpu_to_be16(ETH_P_PREAUTH))
  5138. return -EINVAL;
  5139. if (proto == sdata->control_port_protocol)
  5140. ctrl_flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO |
  5141. IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
  5142. if (unencrypted)
  5143. flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  5144. if (cookie)
  5145. ctrl_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
  5146. flags |= IEEE80211_TX_INTFL_NL80211_FRAME_TX;
  5147. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  5148. sizeof(struct ethhdr) + len);
  5149. if (!skb)
  5150. return -ENOMEM;
  5151. skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr));
  5152. skb_put_data(skb, buf, len);
  5153. ehdr = skb_push(skb, sizeof(struct ethhdr));
  5154. memcpy(ehdr->h_dest, dest, ETH_ALEN);
  5155. /* we may override the SA for MLO STA later */
  5156. if (link_id < 0) {
  5157. ctrl_flags |= u32_encode_bits(IEEE80211_LINK_UNSPECIFIED,
  5158. IEEE80211_TX_CTRL_MLO_LINK);
  5159. memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
  5160. } else {
  5161. struct ieee80211_bss_conf *link_conf;
  5162. ctrl_flags |= u32_encode_bits(link_id,
  5163. IEEE80211_TX_CTRL_MLO_LINK);
  5164. rcu_read_lock();
  5165. link_conf = rcu_dereference(sdata->vif.link_conf[link_id]);
  5166. if (!link_conf) {
  5167. dev_kfree_skb(skb);
  5168. rcu_read_unlock();
  5169. return -ENOLINK;
  5170. }
  5171. memcpy(ehdr->h_source, link_conf->addr, ETH_ALEN);
  5172. rcu_read_unlock();
  5173. }
  5174. ehdr->h_proto = proto;
  5175. skb->dev = dev;
  5176. skb->protocol = proto;
  5177. skb_reset_network_header(skb);
  5178. skb_reset_mac_header(skb);
  5179. if (local->hw.queues < IEEE80211_NUM_ACS)
  5180. goto start_xmit;
  5181. /* update QoS header to prioritize control port frames if possible,
  5182. * priorization also happens for control port frames send over
  5183. * AF_PACKET
  5184. */
  5185. rcu_read_lock();
  5186. err = ieee80211_lookup_ra_sta(sdata, skb, &sta);
  5187. if (err) {
  5188. dev_kfree_skb(skb);
  5189. rcu_read_unlock();
  5190. return err;
  5191. }
  5192. if (!IS_ERR(sta)) {
  5193. u16 queue = ieee80211_select_queue(sdata, sta, skb);
  5194. skb_set_queue_mapping(skb, queue);
  5195. /*
  5196. * for MLO STA, the SA should be the AP MLD address, but
  5197. * the link ID has been selected already
  5198. */
  5199. if (sta && sta->sta.mlo)
  5200. memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
  5201. }
  5202. rcu_read_unlock();
  5203. start_xmit:
  5204. local_bh_disable();
  5205. __ieee80211_subif_start_xmit(skb, skb->dev, flags, ctrl_flags, cookie);
  5206. local_bh_enable();
  5207. return 0;
  5208. }
  5209. int ieee80211_probe_mesh_link(struct wiphy *wiphy, struct net_device *dev,
  5210. const u8 *buf, size_t len)
  5211. {
  5212. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  5213. struct ieee80211_local *local = sdata->local;
  5214. struct sk_buff *skb;
  5215. skb = dev_alloc_skb(local->hw.extra_tx_headroom + len +
  5216. 30 + /* header size */
  5217. 18); /* 11s header size */
  5218. if (!skb)
  5219. return -ENOMEM;
  5220. skb_reserve(skb, local->hw.extra_tx_headroom);
  5221. skb_put_data(skb, buf, len);
  5222. skb->dev = dev;
  5223. skb->protocol = htons(ETH_P_802_3);
  5224. skb_reset_network_header(skb);
  5225. skb_reset_mac_header(skb);
  5226. local_bh_disable();
  5227. __ieee80211_subif_start_xmit(skb, skb->dev, 0,
  5228. IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP,
  5229. NULL);
  5230. local_bh_enable();
  5231. return 0;
  5232. }