tx.c 160 KB

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