tx.c 172 KB

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