tx.c 167 KB

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