mac80211_hwsim.c 162 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
  4. * Copyright (c) 2008, Jouni Malinen <[email protected]>
  5. * Copyright (c) 2011, Javier Lopez <[email protected]>
  6. * Copyright (c) 2016 - 2017 Intel Deutschland GmbH
  7. * Copyright (C) 2018 - 2023 Intel Corporation
  8. */
  9. /*
  10. * TODO:
  11. * - Add TSF sync and fix IBSS beacon transmission by adding
  12. * competition for "air time" at TBTT
  13. * - RX filtering based on filter configuration (data->rx_filter)
  14. */
  15. #include <linux/list.h>
  16. #include <linux/slab.h>
  17. #include <linux/spinlock.h>
  18. #include <net/dst.h>
  19. #include <net/xfrm.h>
  20. #include <net/mac80211.h>
  21. #include <net/ieee80211_radiotap.h>
  22. #include <linux/if_arp.h>
  23. #include <linux/rtnetlink.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/module.h>
  28. #include <linux/ktime.h>
  29. #include <net/genetlink.h>
  30. #include <net/net_namespace.h>
  31. #include <net/netns/generic.h>
  32. #include <linux/rhashtable.h>
  33. #include <linux/nospec.h>
  34. #include <linux/virtio.h>
  35. #include <linux/virtio_ids.h>
  36. #include <linux/virtio_config.h>
  37. #include "mac80211_hwsim.h"
  38. #define WARN_QUEUE 100
  39. #define MAX_QUEUE 200
  40. MODULE_AUTHOR("Jouni Malinen");
  41. MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
  42. MODULE_LICENSE("GPL");
  43. static int radios = 2;
  44. module_param(radios, int, 0444);
  45. MODULE_PARM_DESC(radios, "Number of simulated radios");
  46. static int channels = 1;
  47. module_param(channels, int, 0444);
  48. MODULE_PARM_DESC(channels, "Number of concurrent channels");
  49. static bool paged_rx = false;
  50. module_param(paged_rx, bool, 0644);
  51. MODULE_PARM_DESC(paged_rx, "Use paged SKBs for RX instead of linear ones");
  52. static bool rctbl = false;
  53. module_param(rctbl, bool, 0444);
  54. MODULE_PARM_DESC(rctbl, "Handle rate control table");
  55. static bool support_p2p_device = true;
  56. module_param(support_p2p_device, bool, 0444);
  57. MODULE_PARM_DESC(support_p2p_device, "Support P2P-Device interface type");
  58. static bool mlo;
  59. module_param(mlo, bool, 0444);
  60. MODULE_PARM_DESC(mlo, "Support MLO");
  61. /**
  62. * enum hwsim_regtest - the type of regulatory tests we offer
  63. *
  64. * These are the different values you can use for the regtest
  65. * module parameter. This is useful to help test world roaming
  66. * and the driver regulatory_hint() call and combinations of these.
  67. * If you want to do specific alpha2 regulatory domain tests simply
  68. * use the userspace regulatory request as that will be respected as
  69. * well without the need of this module parameter. This is designed
  70. * only for testing the driver regulatory request, world roaming
  71. * and all possible combinations.
  72. *
  73. * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
  74. * this is the default value.
  75. * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
  76. * hint, only one driver regulatory hint will be sent as such the
  77. * secondary radios are expected to follow.
  78. * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
  79. * request with all radios reporting the same regulatory domain.
  80. * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
  81. * different regulatory domains requests. Expected behaviour is for
  82. * an intersection to occur but each device will still use their
  83. * respective regulatory requested domains. Subsequent radios will
  84. * use the resulting intersection.
  85. * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
  86. * this by using a custom beacon-capable regulatory domain for the first
  87. * radio. All other device world roam.
  88. * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
  89. * domain requests. All radios will adhere to this custom world regulatory
  90. * domain.
  91. * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
  92. * domain requests. The first radio will adhere to the first custom world
  93. * regulatory domain, the second one to the second custom world regulatory
  94. * domain. All other devices will world roam.
  95. * @HWSIM_REGTEST_STRICT_FOLLOW: Used for testing strict regulatory domain
  96. * settings, only the first radio will send a regulatory domain request
  97. * and use strict settings. The rest of the radios are expected to follow.
  98. * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
  99. * settings. All radios will adhere to this.
  100. * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
  101. * domain settings, combined with secondary driver regulatory domain
  102. * settings. The first radio will get a strict regulatory domain setting
  103. * using the first driver regulatory request and the second radio will use
  104. * non-strict settings using the second driver regulatory request. All
  105. * other devices should follow the intersection created between the
  106. * first two.
  107. * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
  108. * at least 6 radios for a complete test. We will test in this order:
  109. * 1 - driver custom world regulatory domain
  110. * 2 - second custom world regulatory domain
  111. * 3 - first driver regulatory domain request
  112. * 4 - second driver regulatory domain request
  113. * 5 - strict regulatory domain settings using the third driver regulatory
  114. * domain request
  115. * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
  116. * regulatory requests.
  117. */
  118. enum hwsim_regtest {
  119. HWSIM_REGTEST_DISABLED = 0,
  120. HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
  121. HWSIM_REGTEST_DRIVER_REG_ALL = 2,
  122. HWSIM_REGTEST_DIFF_COUNTRY = 3,
  123. HWSIM_REGTEST_WORLD_ROAM = 4,
  124. HWSIM_REGTEST_CUSTOM_WORLD = 5,
  125. HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
  126. HWSIM_REGTEST_STRICT_FOLLOW = 7,
  127. HWSIM_REGTEST_STRICT_ALL = 8,
  128. HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
  129. HWSIM_REGTEST_ALL = 10,
  130. };
  131. /* Set to one of the HWSIM_REGTEST_* values above */
  132. static int regtest = HWSIM_REGTEST_DISABLED;
  133. module_param(regtest, int, 0444);
  134. MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
  135. static const char *hwsim_alpha2s[] = {
  136. "FI",
  137. "AL",
  138. "US",
  139. "DE",
  140. "JP",
  141. "AL",
  142. };
  143. static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
  144. .n_reg_rules = 5,
  145. .alpha2 = "99",
  146. .reg_rules = {
  147. REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
  148. REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
  149. REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
  150. REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
  151. REG_RULE(5855-10, 5925+10, 40, 0, 33, 0),
  152. }
  153. };
  154. static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
  155. .n_reg_rules = 3,
  156. .alpha2 = "99",
  157. .reg_rules = {
  158. REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
  159. REG_RULE(5725-10, 5850+10, 40, 0, 30,
  160. NL80211_RRF_NO_IR),
  161. REG_RULE(5855-10, 5925+10, 40, 0, 33, 0),
  162. }
  163. };
  164. static const struct ieee80211_regdomain hwsim_world_regdom_custom_03 = {
  165. .n_reg_rules = 6,
  166. .alpha2 = "99",
  167. .reg_rules = {
  168. REG_RULE(2412 - 10, 2462 + 10, 40, 0, 20, 0),
  169. REG_RULE(2484 - 10, 2484 + 10, 40, 0, 20, 0),
  170. REG_RULE(5150 - 10, 5240 + 10, 40, 0, 30, 0),
  171. REG_RULE(5745 - 10, 5825 + 10, 40, 0, 30, 0),
  172. REG_RULE(5855 - 10, 5925 + 10, 40, 0, 33, 0),
  173. REG_RULE(5955 - 10, 7125 + 10, 320, 0, 33, 0),
  174. }
  175. };
  176. static const struct ieee80211_regdomain *hwsim_world_regdom_custom[] = {
  177. &hwsim_world_regdom_custom_01,
  178. &hwsim_world_regdom_custom_02,
  179. &hwsim_world_regdom_custom_03,
  180. };
  181. struct hwsim_vif_priv {
  182. u32 magic;
  183. u8 bssid[ETH_ALEN];
  184. bool assoc;
  185. bool bcn_en;
  186. u16 aid;
  187. };
  188. #define HWSIM_VIF_MAGIC 0x69537748
  189. static inline void hwsim_check_magic(struct ieee80211_vif *vif)
  190. {
  191. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  192. WARN(vp->magic != HWSIM_VIF_MAGIC,
  193. "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
  194. vif, vp->magic, vif->addr, vif->type, vif->p2p);
  195. }
  196. static inline void hwsim_set_magic(struct ieee80211_vif *vif)
  197. {
  198. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  199. vp->magic = HWSIM_VIF_MAGIC;
  200. }
  201. static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
  202. {
  203. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  204. vp->magic = 0;
  205. }
  206. struct hwsim_sta_priv {
  207. u32 magic;
  208. unsigned int last_link;
  209. u16 active_links_rx;
  210. };
  211. #define HWSIM_STA_MAGIC 0x6d537749
  212. static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
  213. {
  214. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  215. WARN_ON(sp->magic != HWSIM_STA_MAGIC);
  216. }
  217. static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
  218. {
  219. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  220. sp->magic = HWSIM_STA_MAGIC;
  221. }
  222. static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
  223. {
  224. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  225. sp->magic = 0;
  226. }
  227. struct hwsim_chanctx_priv {
  228. u32 magic;
  229. };
  230. #define HWSIM_CHANCTX_MAGIC 0x6d53774a
  231. static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c)
  232. {
  233. struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
  234. WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC);
  235. }
  236. static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c)
  237. {
  238. struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
  239. cp->magic = HWSIM_CHANCTX_MAGIC;
  240. }
  241. static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c)
  242. {
  243. struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
  244. cp->magic = 0;
  245. }
  246. static unsigned int hwsim_net_id;
  247. static DEFINE_IDA(hwsim_netgroup_ida);
  248. struct hwsim_net {
  249. int netgroup;
  250. u32 wmediumd;
  251. };
  252. static inline int hwsim_net_get_netgroup(struct net *net)
  253. {
  254. struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
  255. return hwsim_net->netgroup;
  256. }
  257. static inline int hwsim_net_set_netgroup(struct net *net)
  258. {
  259. struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
  260. hwsim_net->netgroup = ida_alloc(&hwsim_netgroup_ida, GFP_KERNEL);
  261. return hwsim_net->netgroup >= 0 ? 0 : -ENOMEM;
  262. }
  263. static inline u32 hwsim_net_get_wmediumd(struct net *net)
  264. {
  265. struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
  266. return hwsim_net->wmediumd;
  267. }
  268. static inline void hwsim_net_set_wmediumd(struct net *net, u32 portid)
  269. {
  270. struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
  271. hwsim_net->wmediumd = portid;
  272. }
  273. static struct class *hwsim_class;
  274. static struct net_device *hwsim_mon; /* global monitor netdev */
  275. #define CHAN2G(_freq) { \
  276. .band = NL80211_BAND_2GHZ, \
  277. .center_freq = (_freq), \
  278. .hw_value = (_freq), \
  279. }
  280. #define CHAN5G(_freq) { \
  281. .band = NL80211_BAND_5GHZ, \
  282. .center_freq = (_freq), \
  283. .hw_value = (_freq), \
  284. }
  285. #define CHAN6G(_freq) { \
  286. .band = NL80211_BAND_6GHZ, \
  287. .center_freq = (_freq), \
  288. .hw_value = (_freq), \
  289. }
  290. static const struct ieee80211_channel hwsim_channels_2ghz[] = {
  291. CHAN2G(2412), /* Channel 1 */
  292. CHAN2G(2417), /* Channel 2 */
  293. CHAN2G(2422), /* Channel 3 */
  294. CHAN2G(2427), /* Channel 4 */
  295. CHAN2G(2432), /* Channel 5 */
  296. CHAN2G(2437), /* Channel 6 */
  297. CHAN2G(2442), /* Channel 7 */
  298. CHAN2G(2447), /* Channel 8 */
  299. CHAN2G(2452), /* Channel 9 */
  300. CHAN2G(2457), /* Channel 10 */
  301. CHAN2G(2462), /* Channel 11 */
  302. CHAN2G(2467), /* Channel 12 */
  303. CHAN2G(2472), /* Channel 13 */
  304. CHAN2G(2484), /* Channel 14 */
  305. };
  306. static const struct ieee80211_channel hwsim_channels_5ghz[] = {
  307. CHAN5G(5180), /* Channel 36 */
  308. CHAN5G(5200), /* Channel 40 */
  309. CHAN5G(5220), /* Channel 44 */
  310. CHAN5G(5240), /* Channel 48 */
  311. CHAN5G(5260), /* Channel 52 */
  312. CHAN5G(5280), /* Channel 56 */
  313. CHAN5G(5300), /* Channel 60 */
  314. CHAN5G(5320), /* Channel 64 */
  315. CHAN5G(5500), /* Channel 100 */
  316. CHAN5G(5520), /* Channel 104 */
  317. CHAN5G(5540), /* Channel 108 */
  318. CHAN5G(5560), /* Channel 112 */
  319. CHAN5G(5580), /* Channel 116 */
  320. CHAN5G(5600), /* Channel 120 */
  321. CHAN5G(5620), /* Channel 124 */
  322. CHAN5G(5640), /* Channel 128 */
  323. CHAN5G(5660), /* Channel 132 */
  324. CHAN5G(5680), /* Channel 136 */
  325. CHAN5G(5700), /* Channel 140 */
  326. CHAN5G(5745), /* Channel 149 */
  327. CHAN5G(5765), /* Channel 153 */
  328. CHAN5G(5785), /* Channel 157 */
  329. CHAN5G(5805), /* Channel 161 */
  330. CHAN5G(5825), /* Channel 165 */
  331. CHAN5G(5845), /* Channel 169 */
  332. CHAN5G(5855), /* Channel 171 */
  333. CHAN5G(5860), /* Channel 172 */
  334. CHAN5G(5865), /* Channel 173 */
  335. CHAN5G(5870), /* Channel 174 */
  336. CHAN5G(5875), /* Channel 175 */
  337. CHAN5G(5880), /* Channel 176 */
  338. CHAN5G(5885), /* Channel 177 */
  339. CHAN5G(5890), /* Channel 178 */
  340. CHAN5G(5895), /* Channel 179 */
  341. CHAN5G(5900), /* Channel 180 */
  342. CHAN5G(5905), /* Channel 181 */
  343. CHAN5G(5910), /* Channel 182 */
  344. CHAN5G(5915), /* Channel 183 */
  345. CHAN5G(5920), /* Channel 184 */
  346. CHAN5G(5925), /* Channel 185 */
  347. };
  348. static const struct ieee80211_channel hwsim_channels_6ghz[] = {
  349. CHAN6G(5955), /* Channel 1 */
  350. CHAN6G(5975), /* Channel 5 */
  351. CHAN6G(5995), /* Channel 9 */
  352. CHAN6G(6015), /* Channel 13 */
  353. CHAN6G(6035), /* Channel 17 */
  354. CHAN6G(6055), /* Channel 21 */
  355. CHAN6G(6075), /* Channel 25 */
  356. CHAN6G(6095), /* Channel 29 */
  357. CHAN6G(6115), /* Channel 33 */
  358. CHAN6G(6135), /* Channel 37 */
  359. CHAN6G(6155), /* Channel 41 */
  360. CHAN6G(6175), /* Channel 45 */
  361. CHAN6G(6195), /* Channel 49 */
  362. CHAN6G(6215), /* Channel 53 */
  363. CHAN6G(6235), /* Channel 57 */
  364. CHAN6G(6255), /* Channel 61 */
  365. CHAN6G(6275), /* Channel 65 */
  366. CHAN6G(6295), /* Channel 69 */
  367. CHAN6G(6315), /* Channel 73 */
  368. CHAN6G(6335), /* Channel 77 */
  369. CHAN6G(6355), /* Channel 81 */
  370. CHAN6G(6375), /* Channel 85 */
  371. CHAN6G(6395), /* Channel 89 */
  372. CHAN6G(6415), /* Channel 93 */
  373. CHAN6G(6435), /* Channel 97 */
  374. CHAN6G(6455), /* Channel 181 */
  375. CHAN6G(6475), /* Channel 105 */
  376. CHAN6G(6495), /* Channel 109 */
  377. CHAN6G(6515), /* Channel 113 */
  378. CHAN6G(6535), /* Channel 117 */
  379. CHAN6G(6555), /* Channel 121 */
  380. CHAN6G(6575), /* Channel 125 */
  381. CHAN6G(6595), /* Channel 129 */
  382. CHAN6G(6615), /* Channel 133 */
  383. CHAN6G(6635), /* Channel 137 */
  384. CHAN6G(6655), /* Channel 141 */
  385. CHAN6G(6675), /* Channel 145 */
  386. CHAN6G(6695), /* Channel 149 */
  387. CHAN6G(6715), /* Channel 153 */
  388. CHAN6G(6735), /* Channel 157 */
  389. CHAN6G(6755), /* Channel 161 */
  390. CHAN6G(6775), /* Channel 165 */
  391. CHAN6G(6795), /* Channel 169 */
  392. CHAN6G(6815), /* Channel 173 */
  393. CHAN6G(6835), /* Channel 177 */
  394. CHAN6G(6855), /* Channel 181 */
  395. CHAN6G(6875), /* Channel 185 */
  396. CHAN6G(6895), /* Channel 189 */
  397. CHAN6G(6915), /* Channel 193 */
  398. CHAN6G(6935), /* Channel 197 */
  399. CHAN6G(6955), /* Channel 201 */
  400. CHAN6G(6975), /* Channel 205 */
  401. CHAN6G(6995), /* Channel 209 */
  402. CHAN6G(7015), /* Channel 213 */
  403. CHAN6G(7035), /* Channel 217 */
  404. CHAN6G(7055), /* Channel 221 */
  405. CHAN6G(7075), /* Channel 225 */
  406. CHAN6G(7095), /* Channel 229 */
  407. CHAN6G(7115), /* Channel 233 */
  408. };
  409. #define NUM_S1G_CHANS_US 51
  410. static struct ieee80211_channel hwsim_channels_s1g[NUM_S1G_CHANS_US];
  411. static const struct ieee80211_sta_s1g_cap hwsim_s1g_cap = {
  412. .s1g = true,
  413. .cap = { S1G_CAP0_SGI_1MHZ | S1G_CAP0_SGI_2MHZ,
  414. 0,
  415. 0,
  416. S1G_CAP3_MAX_MPDU_LEN,
  417. 0,
  418. S1G_CAP5_AMPDU,
  419. 0,
  420. S1G_CAP7_DUP_1MHZ,
  421. S1G_CAP8_TWT_RESPOND | S1G_CAP8_TWT_REQUEST,
  422. 0},
  423. .nss_mcs = { 0xfc | 1, /* MCS 7 for 1 SS */
  424. /* RX Highest Supported Long GI Data Rate 0:7 */
  425. 0,
  426. /* RX Highest Supported Long GI Data Rate 0:7 */
  427. /* TX S1G MCS Map 0:6 */
  428. 0xfa,
  429. /* TX S1G MCS Map :7 */
  430. /* TX Highest Supported Long GI Data Rate 0:6 */
  431. 0x80,
  432. /* TX Highest Supported Long GI Data Rate 7:8 */
  433. /* Rx Single spatial stream and S1G-MCS Map for 1MHz */
  434. /* Tx Single spatial stream and S1G-MCS Map for 1MHz */
  435. 0 },
  436. };
  437. static void hwsim_init_s1g_channels(struct ieee80211_channel *chans)
  438. {
  439. int ch, freq;
  440. for (ch = 0; ch < NUM_S1G_CHANS_US; ch++) {
  441. freq = 902000 + (ch + 1) * 500;
  442. chans[ch].band = NL80211_BAND_S1GHZ;
  443. chans[ch].center_freq = KHZ_TO_MHZ(freq);
  444. chans[ch].freq_offset = freq % 1000;
  445. chans[ch].hw_value = ch + 1;
  446. }
  447. }
  448. static const struct ieee80211_rate hwsim_rates[] = {
  449. { .bitrate = 10 },
  450. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  451. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  452. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  453. { .bitrate = 60 },
  454. { .bitrate = 90 },
  455. { .bitrate = 120 },
  456. { .bitrate = 180 },
  457. { .bitrate = 240 },
  458. { .bitrate = 360 },
  459. { .bitrate = 480 },
  460. { .bitrate = 540 }
  461. };
  462. #define DEFAULT_RX_RSSI -50
  463. static const u32 hwsim_ciphers[] = {
  464. WLAN_CIPHER_SUITE_WEP40,
  465. WLAN_CIPHER_SUITE_WEP104,
  466. WLAN_CIPHER_SUITE_TKIP,
  467. WLAN_CIPHER_SUITE_CCMP,
  468. WLAN_CIPHER_SUITE_CCMP_256,
  469. WLAN_CIPHER_SUITE_GCMP,
  470. WLAN_CIPHER_SUITE_GCMP_256,
  471. WLAN_CIPHER_SUITE_AES_CMAC,
  472. WLAN_CIPHER_SUITE_BIP_CMAC_256,
  473. WLAN_CIPHER_SUITE_BIP_GMAC_128,
  474. WLAN_CIPHER_SUITE_BIP_GMAC_256,
  475. };
  476. #define OUI_QCA 0x001374
  477. #define QCA_NL80211_SUBCMD_TEST 1
  478. enum qca_nl80211_vendor_subcmds {
  479. QCA_WLAN_VENDOR_ATTR_TEST = 8,
  480. QCA_WLAN_VENDOR_ATTR_MAX = QCA_WLAN_VENDOR_ATTR_TEST
  481. };
  482. static const struct nla_policy
  483. hwsim_vendor_test_policy[QCA_WLAN_VENDOR_ATTR_MAX + 1] = {
  484. [QCA_WLAN_VENDOR_ATTR_MAX] = { .type = NLA_U32 },
  485. };
  486. static int mac80211_hwsim_vendor_cmd_test(struct wiphy *wiphy,
  487. struct wireless_dev *wdev,
  488. const void *data, int data_len)
  489. {
  490. struct sk_buff *skb;
  491. struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_MAX + 1];
  492. int err;
  493. u32 val;
  494. err = nla_parse_deprecated(tb, QCA_WLAN_VENDOR_ATTR_MAX, data,
  495. data_len, hwsim_vendor_test_policy, NULL);
  496. if (err)
  497. return err;
  498. if (!tb[QCA_WLAN_VENDOR_ATTR_TEST])
  499. return -EINVAL;
  500. val = nla_get_u32(tb[QCA_WLAN_VENDOR_ATTR_TEST]);
  501. wiphy_dbg(wiphy, "%s: test=%u\n", __func__, val);
  502. /* Send a vendor event as a test. Note that this would not normally be
  503. * done within a command handler, but rather, based on some other
  504. * trigger. For simplicity, this command is used to trigger the event
  505. * here.
  506. *
  507. * event_idx = 0 (index in mac80211_hwsim_vendor_commands)
  508. */
  509. skb = cfg80211_vendor_event_alloc(wiphy, wdev, 100, 0, GFP_KERNEL);
  510. if (skb) {
  511. /* skb_put() or nla_put() will fill up data within
  512. * NL80211_ATTR_VENDOR_DATA.
  513. */
  514. /* Add vendor data */
  515. nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 1);
  516. /* Send the event - this will call nla_nest_end() */
  517. cfg80211_vendor_event(skb, GFP_KERNEL);
  518. }
  519. /* Send a response to the command */
  520. skb = cfg80211_vendor_cmd_alloc_reply_skb(wiphy, 10);
  521. if (!skb)
  522. return -ENOMEM;
  523. /* skb_put() or nla_put() will fill up data within
  524. * NL80211_ATTR_VENDOR_DATA
  525. */
  526. nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 2);
  527. return cfg80211_vendor_cmd_reply(skb);
  528. }
  529. static struct wiphy_vendor_command mac80211_hwsim_vendor_commands[] = {
  530. {
  531. .info = { .vendor_id = OUI_QCA,
  532. .subcmd = QCA_NL80211_SUBCMD_TEST },
  533. .flags = WIPHY_VENDOR_CMD_NEED_NETDEV,
  534. .doit = mac80211_hwsim_vendor_cmd_test,
  535. .policy = hwsim_vendor_test_policy,
  536. .maxattr = QCA_WLAN_VENDOR_ATTR_MAX,
  537. }
  538. };
  539. /* Advertise support vendor specific events */
  540. static const struct nl80211_vendor_cmd_info mac80211_hwsim_vendor_events[] = {
  541. { .vendor_id = OUI_QCA, .subcmd = 1 },
  542. };
  543. static DEFINE_SPINLOCK(hwsim_radio_lock);
  544. static LIST_HEAD(hwsim_radios);
  545. static struct rhashtable hwsim_radios_rht;
  546. static int hwsim_radio_idx;
  547. static int hwsim_radios_generation = 1;
  548. static struct platform_driver mac80211_hwsim_driver = {
  549. .driver = {
  550. .name = "mac80211_hwsim",
  551. },
  552. };
  553. struct mac80211_hwsim_link_data {
  554. u32 link_id;
  555. u64 beacon_int /* beacon interval in us */;
  556. struct hrtimer beacon_timer;
  557. };
  558. struct mac80211_hwsim_data {
  559. struct list_head list;
  560. struct rhash_head rht;
  561. struct ieee80211_hw *hw;
  562. struct device *dev;
  563. struct ieee80211_supported_band bands[NUM_NL80211_BANDS];
  564. struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
  565. struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
  566. struct ieee80211_channel channels_6ghz[ARRAY_SIZE(hwsim_channels_6ghz)];
  567. struct ieee80211_channel channels_s1g[ARRAY_SIZE(hwsim_channels_s1g)];
  568. struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
  569. struct ieee80211_iface_combination if_combination;
  570. struct ieee80211_iface_limit if_limits[3];
  571. int n_if_limits;
  572. u32 ciphers[ARRAY_SIZE(hwsim_ciphers)];
  573. struct mac_address addresses[2];
  574. int channels, idx;
  575. bool use_chanctx;
  576. bool destroy_on_close;
  577. u32 portid;
  578. char alpha2[2];
  579. const struct ieee80211_regdomain *regd;
  580. struct ieee80211_channel *tmp_chan;
  581. struct ieee80211_channel *roc_chan;
  582. u32 roc_duration;
  583. struct delayed_work roc_start;
  584. struct delayed_work roc_done;
  585. struct delayed_work hw_scan;
  586. struct cfg80211_scan_request *hw_scan_request;
  587. struct ieee80211_vif *hw_scan_vif;
  588. int scan_chan_idx;
  589. u8 scan_addr[ETH_ALEN];
  590. struct {
  591. struct ieee80211_channel *channel;
  592. unsigned long next_start, start, end;
  593. } survey_data[ARRAY_SIZE(hwsim_channels_2ghz) +
  594. ARRAY_SIZE(hwsim_channels_5ghz) +
  595. ARRAY_SIZE(hwsim_channels_6ghz)];
  596. struct ieee80211_channel *channel;
  597. enum nl80211_chan_width bw;
  598. unsigned int rx_filter;
  599. bool started, idle, scanning;
  600. struct mutex mutex;
  601. enum ps_mode {
  602. PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
  603. } ps;
  604. bool ps_poll_pending;
  605. struct dentry *debugfs;
  606. atomic_t pending_cookie;
  607. struct sk_buff_head pending; /* packets pending */
  608. /*
  609. * Only radios in the same group can communicate together (the
  610. * channel has to match too). Each bit represents a group. A
  611. * radio can be in more than one group.
  612. */
  613. u64 group;
  614. /* group shared by radios created in the same netns */
  615. int netgroup;
  616. /* wmediumd portid responsible for netgroup of this radio */
  617. u32 wmediumd;
  618. /* difference between this hw's clock and the real clock, in usecs */
  619. s64 tsf_offset;
  620. s64 bcn_delta;
  621. /* absolute beacon transmission time. Used to cover up "tx" delay. */
  622. u64 abs_bcn_ts;
  623. /* Stats */
  624. u64 tx_pkts;
  625. u64 rx_pkts;
  626. u64 tx_bytes;
  627. u64 rx_bytes;
  628. u64 tx_dropped;
  629. u64 tx_failed;
  630. /* RSSI in rx status of the receiver */
  631. int rx_rssi;
  632. struct mac80211_hwsim_link_data link_data[IEEE80211_MLD_MAX_NUM_LINKS];
  633. };
  634. static const struct rhashtable_params hwsim_rht_params = {
  635. .nelem_hint = 2,
  636. .automatic_shrinking = true,
  637. .key_len = ETH_ALEN,
  638. .key_offset = offsetof(struct mac80211_hwsim_data, addresses[1]),
  639. .head_offset = offsetof(struct mac80211_hwsim_data, rht),
  640. };
  641. struct hwsim_radiotap_hdr {
  642. struct ieee80211_radiotap_header hdr;
  643. __le64 rt_tsft;
  644. u8 rt_flags;
  645. u8 rt_rate;
  646. __le16 rt_channel;
  647. __le16 rt_chbitmask;
  648. } __packed;
  649. struct hwsim_radiotap_ack_hdr {
  650. struct ieee80211_radiotap_header hdr;
  651. u8 rt_flags;
  652. u8 pad;
  653. __le16 rt_channel;
  654. __le16 rt_chbitmask;
  655. } __packed;
  656. /* MAC80211_HWSIM netlink family */
  657. static struct genl_family hwsim_genl_family;
  658. enum hwsim_multicast_groups {
  659. HWSIM_MCGRP_CONFIG,
  660. };
  661. static const struct genl_multicast_group hwsim_mcgrps[] = {
  662. [HWSIM_MCGRP_CONFIG] = { .name = "config", },
  663. };
  664. /* MAC80211_HWSIM netlink policy */
  665. static const struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
  666. [HWSIM_ATTR_ADDR_RECEIVER] = NLA_POLICY_ETH_ADDR_COMPAT,
  667. [HWSIM_ATTR_ADDR_TRANSMITTER] = NLA_POLICY_ETH_ADDR_COMPAT,
  668. [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
  669. .len = IEEE80211_MAX_DATA_LEN },
  670. [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
  671. [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
  672. [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
  673. [HWSIM_ATTR_TX_INFO] = { .type = NLA_BINARY,
  674. .len = IEEE80211_TX_MAX_RATES *
  675. sizeof(struct hwsim_tx_rate)},
  676. [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
  677. [HWSIM_ATTR_CHANNELS] = { .type = NLA_U32 },
  678. [HWSIM_ATTR_RADIO_ID] = { .type = NLA_U32 },
  679. [HWSIM_ATTR_REG_HINT_ALPHA2] = { .type = NLA_STRING, .len = 2 },
  680. [HWSIM_ATTR_REG_CUSTOM_REG] = { .type = NLA_U32 },
  681. [HWSIM_ATTR_REG_STRICT_REG] = { .type = NLA_FLAG },
  682. [HWSIM_ATTR_SUPPORT_P2P_DEVICE] = { .type = NLA_FLAG },
  683. [HWSIM_ATTR_USE_CHANCTX] = { .type = NLA_FLAG },
  684. [HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE] = { .type = NLA_FLAG },
  685. [HWSIM_ATTR_RADIO_NAME] = { .type = NLA_STRING },
  686. [HWSIM_ATTR_NO_VIF] = { .type = NLA_FLAG },
  687. [HWSIM_ATTR_FREQ] = { .type = NLA_U32 },
  688. [HWSIM_ATTR_TX_INFO_FLAGS] = { .type = NLA_BINARY },
  689. [HWSIM_ATTR_PERM_ADDR] = NLA_POLICY_ETH_ADDR_COMPAT,
  690. [HWSIM_ATTR_IFTYPE_SUPPORT] = { .type = NLA_U32 },
  691. [HWSIM_ATTR_CIPHER_SUPPORT] = { .type = NLA_BINARY },
  692. [HWSIM_ATTR_MLO_SUPPORT] = { .type = NLA_FLAG },
  693. };
  694. #if IS_REACHABLE(CONFIG_VIRTIO)
  695. /* MAC80211_HWSIM virtio queues */
  696. static struct virtqueue *hwsim_vqs[HWSIM_NUM_VQS];
  697. static bool hwsim_virtio_enabled;
  698. static DEFINE_SPINLOCK(hwsim_virtio_lock);
  699. static void hwsim_virtio_rx_work(struct work_struct *work);
  700. static DECLARE_WORK(hwsim_virtio_rx, hwsim_virtio_rx_work);
  701. static int hwsim_tx_virtio(struct mac80211_hwsim_data *data,
  702. struct sk_buff *skb)
  703. {
  704. struct scatterlist sg[1];
  705. unsigned long flags;
  706. int err;
  707. spin_lock_irqsave(&hwsim_virtio_lock, flags);
  708. if (!hwsim_virtio_enabled) {
  709. err = -ENODEV;
  710. goto out_free;
  711. }
  712. sg_init_one(sg, skb->head, skb_end_offset(skb));
  713. err = virtqueue_add_outbuf(hwsim_vqs[HWSIM_VQ_TX], sg, 1, skb,
  714. GFP_ATOMIC);
  715. if (err)
  716. goto out_free;
  717. virtqueue_kick(hwsim_vqs[HWSIM_VQ_TX]);
  718. spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
  719. return 0;
  720. out_free:
  721. spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
  722. nlmsg_free(skb);
  723. return err;
  724. }
  725. #else
  726. /* cause a linker error if this ends up being needed */
  727. extern int hwsim_tx_virtio(struct mac80211_hwsim_data *data,
  728. struct sk_buff *skb);
  729. #define hwsim_virtio_enabled false
  730. #endif
  731. static int hwsim_get_chanwidth(enum nl80211_chan_width bw)
  732. {
  733. switch (bw) {
  734. case NL80211_CHAN_WIDTH_20_NOHT:
  735. case NL80211_CHAN_WIDTH_20:
  736. return 20;
  737. case NL80211_CHAN_WIDTH_40:
  738. return 40;
  739. case NL80211_CHAN_WIDTH_80:
  740. return 80;
  741. case NL80211_CHAN_WIDTH_80P80:
  742. case NL80211_CHAN_WIDTH_160:
  743. return 160;
  744. case NL80211_CHAN_WIDTH_320:
  745. return 320;
  746. case NL80211_CHAN_WIDTH_5:
  747. return 5;
  748. case NL80211_CHAN_WIDTH_10:
  749. return 10;
  750. case NL80211_CHAN_WIDTH_1:
  751. return 1;
  752. case NL80211_CHAN_WIDTH_2:
  753. return 2;
  754. case NL80211_CHAN_WIDTH_4:
  755. return 4;
  756. case NL80211_CHAN_WIDTH_8:
  757. return 8;
  758. case NL80211_CHAN_WIDTH_16:
  759. return 16;
  760. }
  761. return INT_MAX;
  762. }
  763. static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
  764. struct sk_buff *skb,
  765. struct ieee80211_channel *chan);
  766. /* sysfs attributes */
  767. static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
  768. {
  769. struct mac80211_hwsim_data *data = dat;
  770. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  771. struct sk_buff *skb;
  772. struct ieee80211_pspoll *pspoll;
  773. if (!vp->assoc)
  774. return;
  775. wiphy_dbg(data->hw->wiphy,
  776. "%s: send PS-Poll to %pM for aid %d\n",
  777. __func__, vp->bssid, vp->aid);
  778. skb = dev_alloc_skb(sizeof(*pspoll));
  779. if (!skb)
  780. return;
  781. pspoll = skb_put(skb, sizeof(*pspoll));
  782. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  783. IEEE80211_STYPE_PSPOLL |
  784. IEEE80211_FCTL_PM);
  785. pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
  786. memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
  787. memcpy(pspoll->ta, mac, ETH_ALEN);
  788. rcu_read_lock();
  789. mac80211_hwsim_tx_frame(data->hw, skb,
  790. rcu_dereference(vif->bss_conf.chanctx_conf)->def.chan);
  791. rcu_read_unlock();
  792. }
  793. static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
  794. struct ieee80211_vif *vif, int ps)
  795. {
  796. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  797. struct sk_buff *skb;
  798. struct ieee80211_hdr *hdr;
  799. struct ieee80211_tx_info *cb;
  800. if (!vp->assoc)
  801. return;
  802. wiphy_dbg(data->hw->wiphy,
  803. "%s: send data::nullfunc to %pM ps=%d\n",
  804. __func__, vp->bssid, ps);
  805. skb = dev_alloc_skb(sizeof(*hdr));
  806. if (!skb)
  807. return;
  808. hdr = skb_put(skb, sizeof(*hdr) - ETH_ALEN);
  809. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  810. IEEE80211_STYPE_NULLFUNC |
  811. IEEE80211_FCTL_TODS |
  812. (ps ? IEEE80211_FCTL_PM : 0));
  813. hdr->duration_id = cpu_to_le16(0);
  814. memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
  815. memcpy(hdr->addr2, mac, ETH_ALEN);
  816. memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
  817. cb = IEEE80211_SKB_CB(skb);
  818. cb->control.rates[0].count = 1;
  819. cb->control.rates[1].idx = -1;
  820. rcu_read_lock();
  821. mac80211_hwsim_tx_frame(data->hw, skb,
  822. rcu_dereference(vif->bss_conf.chanctx_conf)->def.chan);
  823. rcu_read_unlock();
  824. }
  825. static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
  826. struct ieee80211_vif *vif)
  827. {
  828. struct mac80211_hwsim_data *data = dat;
  829. hwsim_send_nullfunc(data, mac, vif, 1);
  830. }
  831. static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
  832. struct ieee80211_vif *vif)
  833. {
  834. struct mac80211_hwsim_data *data = dat;
  835. hwsim_send_nullfunc(data, mac, vif, 0);
  836. }
  837. static int hwsim_fops_ps_read(void *dat, u64 *val)
  838. {
  839. struct mac80211_hwsim_data *data = dat;
  840. *val = data->ps;
  841. return 0;
  842. }
  843. static int hwsim_fops_ps_write(void *dat, u64 val)
  844. {
  845. struct mac80211_hwsim_data *data = dat;
  846. enum ps_mode old_ps;
  847. if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
  848. val != PS_MANUAL_POLL)
  849. return -EINVAL;
  850. if (val == PS_MANUAL_POLL) {
  851. if (data->ps != PS_ENABLED)
  852. return -EINVAL;
  853. local_bh_disable();
  854. ieee80211_iterate_active_interfaces_atomic(
  855. data->hw, IEEE80211_IFACE_ITER_NORMAL,
  856. hwsim_send_ps_poll, data);
  857. local_bh_enable();
  858. return 0;
  859. }
  860. old_ps = data->ps;
  861. data->ps = val;
  862. local_bh_disable();
  863. if (old_ps == PS_DISABLED && val != PS_DISABLED) {
  864. ieee80211_iterate_active_interfaces_atomic(
  865. data->hw, IEEE80211_IFACE_ITER_NORMAL,
  866. hwsim_send_nullfunc_ps, data);
  867. } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
  868. ieee80211_iterate_active_interfaces_atomic(
  869. data->hw, IEEE80211_IFACE_ITER_NORMAL,
  870. hwsim_send_nullfunc_no_ps, data);
  871. }
  872. local_bh_enable();
  873. return 0;
  874. }
  875. DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
  876. "%llu\n");
  877. static int hwsim_write_simulate_radar(void *dat, u64 val)
  878. {
  879. struct mac80211_hwsim_data *data = dat;
  880. ieee80211_radar_detected(data->hw);
  881. return 0;
  882. }
  883. DEFINE_DEBUGFS_ATTRIBUTE(hwsim_simulate_radar, NULL,
  884. hwsim_write_simulate_radar, "%llu\n");
  885. static int hwsim_fops_group_read(void *dat, u64 *val)
  886. {
  887. struct mac80211_hwsim_data *data = dat;
  888. *val = data->group;
  889. return 0;
  890. }
  891. static int hwsim_fops_group_write(void *dat, u64 val)
  892. {
  893. struct mac80211_hwsim_data *data = dat;
  894. data->group = val;
  895. return 0;
  896. }
  897. DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_group,
  898. hwsim_fops_group_read, hwsim_fops_group_write,
  899. "%llx\n");
  900. static int hwsim_fops_rx_rssi_read(void *dat, u64 *val)
  901. {
  902. struct mac80211_hwsim_data *data = dat;
  903. *val = data->rx_rssi;
  904. return 0;
  905. }
  906. static int hwsim_fops_rx_rssi_write(void *dat, u64 val)
  907. {
  908. struct mac80211_hwsim_data *data = dat;
  909. int rssi = (int)val;
  910. if (rssi >= 0 || rssi < -100)
  911. return -EINVAL;
  912. data->rx_rssi = rssi;
  913. return 0;
  914. }
  915. DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_rx_rssi,
  916. hwsim_fops_rx_rssi_read, hwsim_fops_rx_rssi_write,
  917. "%lld\n");
  918. static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
  919. struct net_device *dev)
  920. {
  921. /* TODO: allow packet injection */
  922. dev_kfree_skb(skb);
  923. return NETDEV_TX_OK;
  924. }
  925. static inline u64 mac80211_hwsim_get_tsf_raw(void)
  926. {
  927. return ktime_to_us(ktime_get_real());
  928. }
  929. static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
  930. {
  931. u64 now = mac80211_hwsim_get_tsf_raw();
  932. return cpu_to_le64(now + data->tsf_offset);
  933. }
  934. static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
  935. struct ieee80211_vif *vif)
  936. {
  937. struct mac80211_hwsim_data *data = hw->priv;
  938. return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
  939. }
  940. static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
  941. struct ieee80211_vif *vif, u64 tsf)
  942. {
  943. struct mac80211_hwsim_data *data = hw->priv;
  944. u64 now = mac80211_hwsim_get_tsf(hw, vif);
  945. /* MLD not supported here */
  946. u32 bcn_int = data->link_data[0].beacon_int;
  947. u64 delta = abs(tsf - now);
  948. /* adjust after beaconing with new timestamp at old TBTT */
  949. if (tsf > now) {
  950. data->tsf_offset += delta;
  951. data->bcn_delta = do_div(delta, bcn_int);
  952. } else {
  953. data->tsf_offset -= delta;
  954. data->bcn_delta = -(s64)do_div(delta, bcn_int);
  955. }
  956. }
  957. static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
  958. struct sk_buff *tx_skb,
  959. struct ieee80211_channel *chan)
  960. {
  961. struct mac80211_hwsim_data *data = hw->priv;
  962. struct sk_buff *skb;
  963. struct hwsim_radiotap_hdr *hdr;
  964. u16 flags, bitrate;
  965. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
  966. struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
  967. if (!txrate)
  968. bitrate = 0;
  969. else
  970. bitrate = txrate->bitrate;
  971. if (!netif_running(hwsim_mon))
  972. return;
  973. skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
  974. if (skb == NULL)
  975. return;
  976. hdr = skb_push(skb, sizeof(*hdr));
  977. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  978. hdr->hdr.it_pad = 0;
  979. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  980. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  981. (1 << IEEE80211_RADIOTAP_RATE) |
  982. (1 << IEEE80211_RADIOTAP_TSFT) |
  983. (1 << IEEE80211_RADIOTAP_CHANNEL));
  984. hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
  985. hdr->rt_flags = 0;
  986. hdr->rt_rate = bitrate / 5;
  987. hdr->rt_channel = cpu_to_le16(chan->center_freq);
  988. flags = IEEE80211_CHAN_2GHZ;
  989. if (txrate && txrate->flags & IEEE80211_RATE_ERP_G)
  990. flags |= IEEE80211_CHAN_OFDM;
  991. else
  992. flags |= IEEE80211_CHAN_CCK;
  993. hdr->rt_chbitmask = cpu_to_le16(flags);
  994. skb->dev = hwsim_mon;
  995. skb_reset_mac_header(skb);
  996. skb->ip_summed = CHECKSUM_UNNECESSARY;
  997. skb->pkt_type = PACKET_OTHERHOST;
  998. skb->protocol = htons(ETH_P_802_2);
  999. memset(skb->cb, 0, sizeof(skb->cb));
  1000. netif_rx(skb);
  1001. }
  1002. static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
  1003. const u8 *addr)
  1004. {
  1005. struct sk_buff *skb;
  1006. struct hwsim_radiotap_ack_hdr *hdr;
  1007. u16 flags;
  1008. struct ieee80211_hdr *hdr11;
  1009. if (!netif_running(hwsim_mon))
  1010. return;
  1011. skb = dev_alloc_skb(100);
  1012. if (skb == NULL)
  1013. return;
  1014. hdr = skb_put(skb, sizeof(*hdr));
  1015. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  1016. hdr->hdr.it_pad = 0;
  1017. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  1018. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  1019. (1 << IEEE80211_RADIOTAP_CHANNEL));
  1020. hdr->rt_flags = 0;
  1021. hdr->pad = 0;
  1022. hdr->rt_channel = cpu_to_le16(chan->center_freq);
  1023. flags = IEEE80211_CHAN_2GHZ;
  1024. hdr->rt_chbitmask = cpu_to_le16(flags);
  1025. hdr11 = skb_put(skb, 10);
  1026. hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  1027. IEEE80211_STYPE_ACK);
  1028. hdr11->duration_id = cpu_to_le16(0);
  1029. memcpy(hdr11->addr1, addr, ETH_ALEN);
  1030. skb->dev = hwsim_mon;
  1031. skb_reset_mac_header(skb);
  1032. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1033. skb->pkt_type = PACKET_OTHERHOST;
  1034. skb->protocol = htons(ETH_P_802_2);
  1035. memset(skb->cb, 0, sizeof(skb->cb));
  1036. netif_rx(skb);
  1037. }
  1038. struct mac80211_hwsim_addr_match_data {
  1039. u8 addr[ETH_ALEN];
  1040. bool ret;
  1041. };
  1042. static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
  1043. struct ieee80211_vif *vif)
  1044. {
  1045. int i;
  1046. struct mac80211_hwsim_addr_match_data *md = data;
  1047. if (memcmp(mac, md->addr, ETH_ALEN) == 0) {
  1048. md->ret = true;
  1049. return;
  1050. }
  1051. /* Match the link address */
  1052. for (i = 0; i < ARRAY_SIZE(vif->link_conf); i++) {
  1053. struct ieee80211_bss_conf *conf;
  1054. conf = rcu_dereference(vif->link_conf[i]);
  1055. if (!conf)
  1056. continue;
  1057. if (memcmp(conf->addr, md->addr, ETH_ALEN) == 0) {
  1058. md->ret = true;
  1059. return;
  1060. }
  1061. }
  1062. }
  1063. static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
  1064. const u8 *addr)
  1065. {
  1066. struct mac80211_hwsim_addr_match_data md = {
  1067. .ret = false,
  1068. };
  1069. if (data->scanning && memcmp(addr, data->scan_addr, ETH_ALEN) == 0)
  1070. return true;
  1071. memcpy(md.addr, addr, ETH_ALEN);
  1072. ieee80211_iterate_active_interfaces_atomic(data->hw,
  1073. IEEE80211_IFACE_ITER_NORMAL,
  1074. mac80211_hwsim_addr_iter,
  1075. &md);
  1076. return md.ret;
  1077. }
  1078. static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
  1079. struct sk_buff *skb)
  1080. {
  1081. switch (data->ps) {
  1082. case PS_DISABLED:
  1083. return true;
  1084. case PS_ENABLED:
  1085. return false;
  1086. case PS_AUTO_POLL:
  1087. /* TODO: accept (some) Beacons by default and other frames only
  1088. * if pending PS-Poll has been sent */
  1089. return true;
  1090. case PS_MANUAL_POLL:
  1091. /* Allow unicast frames to own address if there is a pending
  1092. * PS-Poll */
  1093. if (data->ps_poll_pending &&
  1094. mac80211_hwsim_addr_match(data, skb->data + 4)) {
  1095. data->ps_poll_pending = false;
  1096. return true;
  1097. }
  1098. return false;
  1099. }
  1100. return true;
  1101. }
  1102. static int hwsim_unicast_netgroup(struct mac80211_hwsim_data *data,
  1103. struct sk_buff *skb, int portid)
  1104. {
  1105. struct net *net;
  1106. bool found = false;
  1107. int res = -ENOENT;
  1108. rcu_read_lock();
  1109. for_each_net_rcu(net) {
  1110. if (data->netgroup == hwsim_net_get_netgroup(net)) {
  1111. res = genlmsg_unicast(net, skb, portid);
  1112. found = true;
  1113. break;
  1114. }
  1115. }
  1116. rcu_read_unlock();
  1117. if (!found)
  1118. nlmsg_free(skb);
  1119. return res;
  1120. }
  1121. static void mac80211_hwsim_config_mac_nl(struct ieee80211_hw *hw,
  1122. const u8 *addr, bool add)
  1123. {
  1124. struct mac80211_hwsim_data *data = hw->priv;
  1125. u32 _portid = READ_ONCE(data->wmediumd);
  1126. struct sk_buff *skb;
  1127. void *msg_head;
  1128. WARN_ON(!is_valid_ether_addr(addr));
  1129. if (!_portid && !hwsim_virtio_enabled)
  1130. return;
  1131. skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1132. if (!skb)
  1133. return;
  1134. msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
  1135. add ? HWSIM_CMD_ADD_MAC_ADDR :
  1136. HWSIM_CMD_DEL_MAC_ADDR);
  1137. if (!msg_head) {
  1138. pr_debug("mac80211_hwsim: problem with msg_head\n");
  1139. goto nla_put_failure;
  1140. }
  1141. if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
  1142. ETH_ALEN, data->addresses[1].addr))
  1143. goto nla_put_failure;
  1144. if (nla_put(skb, HWSIM_ATTR_ADDR_RECEIVER, ETH_ALEN, addr))
  1145. goto nla_put_failure;
  1146. genlmsg_end(skb, msg_head);
  1147. if (hwsim_virtio_enabled)
  1148. hwsim_tx_virtio(data, skb);
  1149. else
  1150. hwsim_unicast_netgroup(data, skb, _portid);
  1151. return;
  1152. nla_put_failure:
  1153. nlmsg_free(skb);
  1154. }
  1155. static inline u16 trans_tx_rate_flags_ieee2hwsim(struct ieee80211_tx_rate *rate)
  1156. {
  1157. u16 result = 0;
  1158. if (rate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
  1159. result |= MAC80211_HWSIM_TX_RC_USE_RTS_CTS;
  1160. if (rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  1161. result |= MAC80211_HWSIM_TX_RC_USE_CTS_PROTECT;
  1162. if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  1163. result |= MAC80211_HWSIM_TX_RC_USE_SHORT_PREAMBLE;
  1164. if (rate->flags & IEEE80211_TX_RC_MCS)
  1165. result |= MAC80211_HWSIM_TX_RC_MCS;
  1166. if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
  1167. result |= MAC80211_HWSIM_TX_RC_GREEN_FIELD;
  1168. if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  1169. result |= MAC80211_HWSIM_TX_RC_40_MHZ_WIDTH;
  1170. if (rate->flags & IEEE80211_TX_RC_DUP_DATA)
  1171. result |= MAC80211_HWSIM_TX_RC_DUP_DATA;
  1172. if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
  1173. result |= MAC80211_HWSIM_TX_RC_SHORT_GI;
  1174. if (rate->flags & IEEE80211_TX_RC_VHT_MCS)
  1175. result |= MAC80211_HWSIM_TX_RC_VHT_MCS;
  1176. if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
  1177. result |= MAC80211_HWSIM_TX_RC_80_MHZ_WIDTH;
  1178. if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
  1179. result |= MAC80211_HWSIM_TX_RC_160_MHZ_WIDTH;
  1180. return result;
  1181. }
  1182. static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
  1183. struct sk_buff *my_skb,
  1184. int dst_portid,
  1185. struct ieee80211_channel *channel)
  1186. {
  1187. struct sk_buff *skb;
  1188. struct mac80211_hwsim_data *data = hw->priv;
  1189. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
  1190. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
  1191. void *msg_head;
  1192. unsigned int hwsim_flags = 0;
  1193. int i;
  1194. struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
  1195. struct hwsim_tx_rate_flag tx_attempts_flags[IEEE80211_TX_MAX_RATES];
  1196. uintptr_t cookie;
  1197. if (data->ps != PS_DISABLED)
  1198. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  1199. /* If the queue contains MAX_QUEUE skb's drop some */
  1200. if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
  1201. /* Dropping until WARN_QUEUE level */
  1202. while (skb_queue_len(&data->pending) >= WARN_QUEUE) {
  1203. ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
  1204. data->tx_dropped++;
  1205. }
  1206. }
  1207. skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1208. if (skb == NULL)
  1209. goto nla_put_failure;
  1210. msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
  1211. HWSIM_CMD_FRAME);
  1212. if (msg_head == NULL) {
  1213. pr_debug("mac80211_hwsim: problem with msg_head\n");
  1214. goto nla_put_failure;
  1215. }
  1216. if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
  1217. ETH_ALEN, data->addresses[1].addr))
  1218. goto nla_put_failure;
  1219. /* We get the skb->data */
  1220. if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
  1221. goto nla_put_failure;
  1222. /* We get the flags for this transmission, and we translate them to
  1223. wmediumd flags */
  1224. if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
  1225. hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
  1226. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  1227. hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
  1228. if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
  1229. goto nla_put_failure;
  1230. if (nla_put_u32(skb, HWSIM_ATTR_FREQ, channel->center_freq))
  1231. goto nla_put_failure;
  1232. /* We get the tx control (rate and retries) info*/
  1233. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  1234. tx_attempts[i].idx = info->status.rates[i].idx;
  1235. tx_attempts_flags[i].idx = info->status.rates[i].idx;
  1236. tx_attempts[i].count = info->status.rates[i].count;
  1237. tx_attempts_flags[i].flags =
  1238. trans_tx_rate_flags_ieee2hwsim(
  1239. &info->status.rates[i]);
  1240. }
  1241. if (nla_put(skb, HWSIM_ATTR_TX_INFO,
  1242. sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
  1243. tx_attempts))
  1244. goto nla_put_failure;
  1245. if (nla_put(skb, HWSIM_ATTR_TX_INFO_FLAGS,
  1246. sizeof(struct hwsim_tx_rate_flag) * IEEE80211_TX_MAX_RATES,
  1247. tx_attempts_flags))
  1248. goto nla_put_failure;
  1249. /* We create a cookie to identify this skb */
  1250. cookie = atomic_inc_return(&data->pending_cookie);
  1251. info->rate_driver_data[0] = (void *)cookie;
  1252. if (nla_put_u64_64bit(skb, HWSIM_ATTR_COOKIE, cookie, HWSIM_ATTR_PAD))
  1253. goto nla_put_failure;
  1254. genlmsg_end(skb, msg_head);
  1255. if (hwsim_virtio_enabled) {
  1256. if (hwsim_tx_virtio(data, skb))
  1257. goto err_free_txskb;
  1258. } else {
  1259. if (hwsim_unicast_netgroup(data, skb, dst_portid))
  1260. goto err_free_txskb;
  1261. }
  1262. /* Enqueue the packet */
  1263. skb_queue_tail(&data->pending, my_skb);
  1264. data->tx_pkts++;
  1265. data->tx_bytes += my_skb->len;
  1266. return;
  1267. nla_put_failure:
  1268. nlmsg_free(skb);
  1269. err_free_txskb:
  1270. pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
  1271. ieee80211_free_txskb(hw, my_skb);
  1272. data->tx_failed++;
  1273. }
  1274. static bool hwsim_chans_compat(struct ieee80211_channel *c1,
  1275. struct ieee80211_channel *c2)
  1276. {
  1277. if (!c1 || !c2)
  1278. return false;
  1279. return c1->center_freq == c2->center_freq;
  1280. }
  1281. struct tx_iter_data {
  1282. struct ieee80211_channel *channel;
  1283. bool receive;
  1284. };
  1285. static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
  1286. struct ieee80211_vif *vif)
  1287. {
  1288. struct tx_iter_data *data = _data;
  1289. int i;
  1290. for (i = 0; i < ARRAY_SIZE(vif->link_conf); i++) {
  1291. struct ieee80211_bss_conf *conf;
  1292. struct ieee80211_chanctx_conf *chanctx;
  1293. conf = rcu_dereference(vif->link_conf[i]);
  1294. if (!conf)
  1295. continue;
  1296. chanctx = rcu_dereference(conf->chanctx_conf);
  1297. if (!chanctx)
  1298. continue;
  1299. if (!hwsim_chans_compat(data->channel, chanctx->def.chan))
  1300. continue;
  1301. data->receive = true;
  1302. return;
  1303. }
  1304. }
  1305. static void mac80211_hwsim_add_vendor_rtap(struct sk_buff *skb)
  1306. {
  1307. /*
  1308. * To enable this code, #define the HWSIM_RADIOTAP_OUI,
  1309. * e.g. like this:
  1310. * #define HWSIM_RADIOTAP_OUI "\x02\x00\x00"
  1311. * (but you should use a valid OUI, not that)
  1312. *
  1313. * If anyone wants to 'donate' a radiotap OUI/subns code
  1314. * please send a patch removing this #ifdef and changing
  1315. * the values accordingly.
  1316. */
  1317. #ifdef HWSIM_RADIOTAP_OUI
  1318. struct ieee80211_vendor_radiotap *rtap;
  1319. /*
  1320. * Note that this code requires the headroom in the SKB
  1321. * that was allocated earlier.
  1322. */
  1323. rtap = skb_push(skb, sizeof(*rtap) + 8 + 4);
  1324. rtap->oui[0] = HWSIM_RADIOTAP_OUI[0];
  1325. rtap->oui[1] = HWSIM_RADIOTAP_OUI[1];
  1326. rtap->oui[2] = HWSIM_RADIOTAP_OUI[2];
  1327. rtap->subns = 127;
  1328. /*
  1329. * Radiotap vendor namespaces can (and should) also be
  1330. * split into fields by using the standard radiotap
  1331. * presence bitmap mechanism. Use just BIT(0) here for
  1332. * the presence bitmap.
  1333. */
  1334. rtap->present = BIT(0);
  1335. /* We have 8 bytes of (dummy) data */
  1336. rtap->len = 8;
  1337. /* For testing, also require it to be aligned */
  1338. rtap->align = 8;
  1339. /* And also test that padding works, 4 bytes */
  1340. rtap->pad = 4;
  1341. /* push the data */
  1342. memcpy(rtap->data, "ABCDEFGH", 8);
  1343. /* make sure to clear padding, mac80211 doesn't */
  1344. memset(rtap->data + 8, 0, 4);
  1345. IEEE80211_SKB_RXCB(skb)->flag |= RX_FLAG_RADIOTAP_VENDOR_DATA;
  1346. #endif
  1347. }
  1348. static void mac80211_hwsim_rx(struct mac80211_hwsim_data *data,
  1349. struct ieee80211_rx_status *rx_status,
  1350. struct sk_buff *skb)
  1351. {
  1352. struct ieee80211_hdr *hdr = (void *)skb->data;
  1353. if (!ieee80211_has_morefrags(hdr->frame_control) &&
  1354. !is_multicast_ether_addr(hdr->addr1) &&
  1355. (ieee80211_is_mgmt(hdr->frame_control) ||
  1356. ieee80211_is_data(hdr->frame_control))) {
  1357. struct ieee80211_sta *sta;
  1358. unsigned int link_id;
  1359. rcu_read_lock();
  1360. sta = ieee80211_find_sta_by_link_addrs(data->hw, hdr->addr2,
  1361. hdr->addr1, &link_id);
  1362. if (sta) {
  1363. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  1364. if (ieee80211_has_pm(hdr->frame_control))
  1365. sp->active_links_rx &= ~BIT(link_id);
  1366. else
  1367. sp->active_links_rx |= BIT(link_id);
  1368. }
  1369. rcu_read_unlock();
  1370. }
  1371. memcpy(IEEE80211_SKB_RXCB(skb), rx_status, sizeof(*rx_status));
  1372. mac80211_hwsim_add_vendor_rtap(skb);
  1373. data->rx_pkts++;
  1374. data->rx_bytes += skb->len;
  1375. ieee80211_rx_irqsafe(data->hw, skb);
  1376. }
  1377. static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
  1378. struct sk_buff *skb,
  1379. struct ieee80211_channel *chan)
  1380. {
  1381. struct mac80211_hwsim_data *data = hw->priv, *data2;
  1382. bool ack = false;
  1383. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1384. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1385. struct ieee80211_rx_status rx_status;
  1386. u64 now;
  1387. memset(&rx_status, 0, sizeof(rx_status));
  1388. rx_status.flag |= RX_FLAG_MACTIME_START;
  1389. rx_status.freq = chan->center_freq;
  1390. rx_status.freq_offset = chan->freq_offset ? 1 : 0;
  1391. rx_status.band = chan->band;
  1392. if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
  1393. rx_status.rate_idx =
  1394. ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
  1395. rx_status.nss =
  1396. ieee80211_rate_get_vht_nss(&info->control.rates[0]);
  1397. rx_status.encoding = RX_ENC_VHT;
  1398. } else {
  1399. rx_status.rate_idx = info->control.rates[0].idx;
  1400. if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
  1401. rx_status.encoding = RX_ENC_HT;
  1402. }
  1403. if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  1404. rx_status.bw = RATE_INFO_BW_40;
  1405. else if (info->control.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
  1406. rx_status.bw = RATE_INFO_BW_80;
  1407. else if (info->control.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
  1408. rx_status.bw = RATE_INFO_BW_160;
  1409. else
  1410. rx_status.bw = RATE_INFO_BW_20;
  1411. if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
  1412. rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
  1413. /* TODO: simulate optional packet loss */
  1414. rx_status.signal = data->rx_rssi;
  1415. if (info->control.vif)
  1416. rx_status.signal += info->control.vif->bss_conf.txpower;
  1417. if (data->ps != PS_DISABLED)
  1418. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  1419. /* release the skb's source info */
  1420. skb_orphan(skb);
  1421. skb_dst_drop(skb);
  1422. skb->mark = 0;
  1423. skb_ext_reset(skb);
  1424. nf_reset_ct(skb);
  1425. /*
  1426. * Get absolute mactime here so all HWs RX at the "same time", and
  1427. * absolute TX time for beacon mactime so the timestamp matches.
  1428. * Giving beacons a different mactime than non-beacons looks messy, but
  1429. * it helps the Toffset be exact and a ~10us mactime discrepancy
  1430. * probably doesn't really matter.
  1431. */
  1432. if (ieee80211_is_beacon(hdr->frame_control) ||
  1433. ieee80211_is_probe_resp(hdr->frame_control)) {
  1434. rx_status.boottime_ns = ktime_get_boottime_ns();
  1435. now = data->abs_bcn_ts;
  1436. } else {
  1437. now = mac80211_hwsim_get_tsf_raw();
  1438. }
  1439. /* Copy skb to all enabled radios that are on the current frequency */
  1440. spin_lock(&hwsim_radio_lock);
  1441. list_for_each_entry(data2, &hwsim_radios, list) {
  1442. struct sk_buff *nskb;
  1443. struct tx_iter_data tx_iter_data = {
  1444. .receive = false,
  1445. .channel = chan,
  1446. };
  1447. if (data == data2)
  1448. continue;
  1449. if (!data2->started || (data2->idle && !data2->tmp_chan) ||
  1450. !hwsim_ps_rx_ok(data2, skb))
  1451. continue;
  1452. if (!(data->group & data2->group))
  1453. continue;
  1454. if (data->netgroup != data2->netgroup)
  1455. continue;
  1456. if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
  1457. !hwsim_chans_compat(chan, data2->channel)) {
  1458. ieee80211_iterate_active_interfaces_atomic(
  1459. data2->hw, IEEE80211_IFACE_ITER_NORMAL,
  1460. mac80211_hwsim_tx_iter, &tx_iter_data);
  1461. if (!tx_iter_data.receive)
  1462. continue;
  1463. }
  1464. /*
  1465. * reserve some space for our vendor and the normal
  1466. * radiotap header, since we're copying anyway
  1467. */
  1468. if (skb->len < PAGE_SIZE && paged_rx) {
  1469. struct page *page = alloc_page(GFP_ATOMIC);
  1470. if (!page)
  1471. continue;
  1472. nskb = dev_alloc_skb(128);
  1473. if (!nskb) {
  1474. __free_page(page);
  1475. continue;
  1476. }
  1477. memcpy(page_address(page), skb->data, skb->len);
  1478. skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
  1479. } else {
  1480. nskb = skb_copy(skb, GFP_ATOMIC);
  1481. if (!nskb)
  1482. continue;
  1483. }
  1484. if (mac80211_hwsim_addr_match(data2, hdr->addr1))
  1485. ack = true;
  1486. rx_status.mactime = now + data2->tsf_offset;
  1487. mac80211_hwsim_rx(data2, &rx_status, nskb);
  1488. }
  1489. spin_unlock(&hwsim_radio_lock);
  1490. return ack;
  1491. }
  1492. static struct ieee80211_bss_conf *
  1493. mac80211_hwsim_select_tx_link(struct mac80211_hwsim_data *data,
  1494. struct ieee80211_vif *vif,
  1495. struct ieee80211_sta *sta,
  1496. struct ieee80211_hdr *hdr,
  1497. struct ieee80211_link_sta **link_sta)
  1498. {
  1499. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  1500. int i;
  1501. if (!vif->valid_links)
  1502. return &vif->bss_conf;
  1503. WARN_ON(is_multicast_ether_addr(hdr->addr1));
  1504. if (WARN_ON_ONCE(!sta || !sta->valid_links))
  1505. return &vif->bss_conf;
  1506. for (i = 0; i < ARRAY_SIZE(vif->link_conf); i++) {
  1507. struct ieee80211_bss_conf *bss_conf;
  1508. unsigned int link_id;
  1509. /* round-robin the available link IDs */
  1510. link_id = (sp->last_link + i + 1) % ARRAY_SIZE(vif->link_conf);
  1511. if (!(vif->active_links & BIT(link_id)))
  1512. continue;
  1513. if (!(sp->active_links_rx & BIT(link_id)))
  1514. continue;
  1515. *link_sta = rcu_dereference(sta->link[link_id]);
  1516. if (!*link_sta)
  1517. continue;
  1518. bss_conf = rcu_dereference(vif->link_conf[link_id]);
  1519. if (WARN_ON_ONCE(!bss_conf))
  1520. continue;
  1521. /* can happen while switching links */
  1522. if (!rcu_access_pointer(bss_conf->chanctx_conf))
  1523. continue;
  1524. sp->last_link = link_id;
  1525. return bss_conf;
  1526. }
  1527. return NULL;
  1528. }
  1529. static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
  1530. struct ieee80211_tx_control *control,
  1531. struct sk_buff *skb)
  1532. {
  1533. struct mac80211_hwsim_data *data = hw->priv;
  1534. struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
  1535. struct ieee80211_hdr *hdr = (void *)skb->data;
  1536. struct ieee80211_chanctx_conf *chanctx_conf;
  1537. struct ieee80211_channel *channel;
  1538. bool ack;
  1539. enum nl80211_chan_width confbw = NL80211_CHAN_WIDTH_20_NOHT;
  1540. u32 _portid, i;
  1541. if (WARN_ON(skb->len < 10)) {
  1542. /* Should not happen; just a sanity check for addr1 use */
  1543. ieee80211_free_txskb(hw, skb);
  1544. return;
  1545. }
  1546. if (!data->use_chanctx) {
  1547. channel = data->channel;
  1548. confbw = data->bw;
  1549. } else if (txi->hw_queue == 4) {
  1550. channel = data->tmp_chan;
  1551. } else {
  1552. u8 link = u32_get_bits(IEEE80211_SKB_CB(skb)->control.flags,
  1553. IEEE80211_TX_CTRL_MLO_LINK);
  1554. struct ieee80211_vif *vif = txi->control.vif;
  1555. struct ieee80211_link_sta *link_sta = NULL;
  1556. struct ieee80211_sta *sta = control->sta;
  1557. struct ieee80211_bss_conf *bss_conf;
  1558. if (link != IEEE80211_LINK_UNSPECIFIED) {
  1559. bss_conf = rcu_dereference(txi->control.vif->link_conf[link]);
  1560. if (sta)
  1561. link_sta = rcu_dereference(sta->link[link]);
  1562. } else {
  1563. bss_conf = mac80211_hwsim_select_tx_link(data, vif, sta,
  1564. hdr, &link_sta);
  1565. }
  1566. if (WARN_ON(!bss_conf)) {
  1567. ieee80211_free_txskb(hw, skb);
  1568. return;
  1569. }
  1570. if (sta && sta->mlo) {
  1571. if (WARN_ON(!link_sta)) {
  1572. ieee80211_free_txskb(hw, skb);
  1573. return;
  1574. }
  1575. /* address translation to link addresses on TX */
  1576. ether_addr_copy(hdr->addr1, link_sta->addr);
  1577. ether_addr_copy(hdr->addr2, bss_conf->addr);
  1578. /* translate A3 only if it's the BSSID */
  1579. if (!ieee80211_has_tods(hdr->frame_control) &&
  1580. !ieee80211_has_fromds(hdr->frame_control)) {
  1581. if (ether_addr_equal(hdr->addr3, sta->addr))
  1582. ether_addr_copy(hdr->addr3, link_sta->addr);
  1583. else if (ether_addr_equal(hdr->addr3, vif->addr))
  1584. ether_addr_copy(hdr->addr3, bss_conf->addr);
  1585. }
  1586. /* no need to look at A4, if present it's SA */
  1587. }
  1588. chanctx_conf = rcu_dereference(bss_conf->chanctx_conf);
  1589. if (chanctx_conf) {
  1590. channel = chanctx_conf->def.chan;
  1591. confbw = chanctx_conf->def.width;
  1592. } else {
  1593. channel = NULL;
  1594. }
  1595. }
  1596. if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
  1597. ieee80211_free_txskb(hw, skb);
  1598. return;
  1599. }
  1600. if (data->idle && !data->tmp_chan) {
  1601. wiphy_dbg(hw->wiphy, "Trying to TX when idle - reject\n");
  1602. ieee80211_free_txskb(hw, skb);
  1603. return;
  1604. }
  1605. if (txi->control.vif)
  1606. hwsim_check_magic(txi->control.vif);
  1607. if (control->sta)
  1608. hwsim_check_sta_magic(control->sta);
  1609. if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
  1610. ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
  1611. txi->control.rates,
  1612. ARRAY_SIZE(txi->control.rates));
  1613. for (i = 0; i < ARRAY_SIZE(txi->control.rates); i++) {
  1614. u16 rflags = txi->control.rates[i].flags;
  1615. /* initialize to data->bw for 5/10 MHz handling */
  1616. enum nl80211_chan_width bw = data->bw;
  1617. if (txi->control.rates[i].idx == -1)
  1618. break;
  1619. if (rflags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  1620. bw = NL80211_CHAN_WIDTH_40;
  1621. else if (rflags & IEEE80211_TX_RC_80_MHZ_WIDTH)
  1622. bw = NL80211_CHAN_WIDTH_80;
  1623. else if (rflags & IEEE80211_TX_RC_160_MHZ_WIDTH)
  1624. bw = NL80211_CHAN_WIDTH_160;
  1625. if (WARN_ON(hwsim_get_chanwidth(bw) > hwsim_get_chanwidth(confbw)))
  1626. return;
  1627. }
  1628. if (skb->len >= 24 + 8 &&
  1629. ieee80211_is_probe_resp(hdr->frame_control)) {
  1630. /* fake header transmission time */
  1631. struct ieee80211_mgmt *mgmt;
  1632. struct ieee80211_rate *txrate;
  1633. /* TODO: get MCS */
  1634. int bitrate = 100;
  1635. u64 ts;
  1636. mgmt = (struct ieee80211_mgmt *)skb->data;
  1637. txrate = ieee80211_get_tx_rate(hw, txi);
  1638. if (txrate)
  1639. bitrate = txrate->bitrate;
  1640. ts = mac80211_hwsim_get_tsf_raw();
  1641. mgmt->u.probe_resp.timestamp =
  1642. cpu_to_le64(ts + data->tsf_offset +
  1643. 24 * 8 * 10 / bitrate);
  1644. }
  1645. mac80211_hwsim_monitor_rx(hw, skb, channel);
  1646. /* wmediumd mode check */
  1647. _portid = READ_ONCE(data->wmediumd);
  1648. if (_portid || hwsim_virtio_enabled)
  1649. return mac80211_hwsim_tx_frame_nl(hw, skb, _portid, channel);
  1650. /* NO wmediumd detected, perfect medium simulation */
  1651. data->tx_pkts++;
  1652. data->tx_bytes += skb->len;
  1653. ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
  1654. if (ack && skb->len >= 16)
  1655. mac80211_hwsim_monitor_ack(channel, hdr->addr2);
  1656. ieee80211_tx_info_clear_status(txi);
  1657. /* frame was transmitted at most favorable rate at first attempt */
  1658. txi->control.rates[0].count = 1;
  1659. txi->control.rates[1].idx = -1;
  1660. if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
  1661. txi->flags |= IEEE80211_TX_STAT_ACK;
  1662. ieee80211_tx_status_irqsafe(hw, skb);
  1663. }
  1664. static int mac80211_hwsim_start(struct ieee80211_hw *hw)
  1665. {
  1666. struct mac80211_hwsim_data *data = hw->priv;
  1667. wiphy_dbg(hw->wiphy, "%s\n", __func__);
  1668. data->started = true;
  1669. return 0;
  1670. }
  1671. static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
  1672. {
  1673. struct mac80211_hwsim_data *data = hw->priv;
  1674. int i;
  1675. data->started = false;
  1676. for (i = 0; i < ARRAY_SIZE(data->link_data); i++)
  1677. hrtimer_cancel(&data->link_data[i].beacon_timer);
  1678. while (!skb_queue_empty(&data->pending))
  1679. ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
  1680. wiphy_dbg(hw->wiphy, "%s\n", __func__);
  1681. }
  1682. static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
  1683. struct ieee80211_vif *vif)
  1684. {
  1685. wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  1686. __func__, ieee80211_vif_type_p2p(vif),
  1687. vif->addr);
  1688. hwsim_set_magic(vif);
  1689. if (vif->type != NL80211_IFTYPE_MONITOR)
  1690. mac80211_hwsim_config_mac_nl(hw, vif->addr, true);
  1691. vif->cab_queue = 0;
  1692. vif->hw_queue[IEEE80211_AC_VO] = 0;
  1693. vif->hw_queue[IEEE80211_AC_VI] = 1;
  1694. vif->hw_queue[IEEE80211_AC_BE] = 2;
  1695. vif->hw_queue[IEEE80211_AC_BK] = 3;
  1696. return 0;
  1697. }
  1698. static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
  1699. struct ieee80211_vif *vif,
  1700. enum nl80211_iftype newtype,
  1701. bool newp2p)
  1702. {
  1703. newtype = ieee80211_iftype_p2p(newtype, newp2p);
  1704. wiphy_dbg(hw->wiphy,
  1705. "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
  1706. __func__, ieee80211_vif_type_p2p(vif),
  1707. newtype, vif->addr);
  1708. hwsim_check_magic(vif);
  1709. /*
  1710. * interface may change from non-AP to AP in
  1711. * which case this needs to be set up again
  1712. */
  1713. vif->cab_queue = 0;
  1714. return 0;
  1715. }
  1716. static void mac80211_hwsim_remove_interface(
  1717. struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1718. {
  1719. wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  1720. __func__, ieee80211_vif_type_p2p(vif),
  1721. vif->addr);
  1722. hwsim_check_magic(vif);
  1723. hwsim_clear_magic(vif);
  1724. if (vif->type != NL80211_IFTYPE_MONITOR)
  1725. mac80211_hwsim_config_mac_nl(hw, vif->addr, false);
  1726. }
  1727. static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
  1728. struct sk_buff *skb,
  1729. struct ieee80211_channel *chan)
  1730. {
  1731. struct mac80211_hwsim_data *data = hw->priv;
  1732. u32 _pid = READ_ONCE(data->wmediumd);
  1733. if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE)) {
  1734. struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
  1735. ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
  1736. txi->control.rates,
  1737. ARRAY_SIZE(txi->control.rates));
  1738. }
  1739. mac80211_hwsim_monitor_rx(hw, skb, chan);
  1740. if (_pid || hwsim_virtio_enabled)
  1741. return mac80211_hwsim_tx_frame_nl(hw, skb, _pid, chan);
  1742. data->tx_pkts++;
  1743. data->tx_bytes += skb->len;
  1744. mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
  1745. dev_kfree_skb(skb);
  1746. }
  1747. static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
  1748. struct ieee80211_vif *vif)
  1749. {
  1750. struct mac80211_hwsim_link_data *link_data = arg;
  1751. u32 link_id = link_data->link_id;
  1752. struct ieee80211_bss_conf *link_conf;
  1753. struct mac80211_hwsim_data *data =
  1754. container_of(link_data, struct mac80211_hwsim_data,
  1755. link_data[link_id]);
  1756. struct ieee80211_hw *hw = data->hw;
  1757. struct ieee80211_tx_info *info;
  1758. struct ieee80211_rate *txrate;
  1759. struct ieee80211_mgmt *mgmt;
  1760. struct sk_buff *skb;
  1761. /* TODO: get MCS */
  1762. int bitrate = 100;
  1763. hwsim_check_magic(vif);
  1764. link_conf = rcu_dereference(vif->link_conf[link_id]);
  1765. if (!link_conf)
  1766. return;
  1767. if (vif->type != NL80211_IFTYPE_AP &&
  1768. vif->type != NL80211_IFTYPE_MESH_POINT &&
  1769. vif->type != NL80211_IFTYPE_ADHOC &&
  1770. vif->type != NL80211_IFTYPE_OCB)
  1771. return;
  1772. skb = ieee80211_beacon_get(hw, vif, link_data->link_id);
  1773. if (skb == NULL)
  1774. return;
  1775. info = IEEE80211_SKB_CB(skb);
  1776. if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
  1777. ieee80211_get_tx_rates(vif, NULL, skb,
  1778. info->control.rates,
  1779. ARRAY_SIZE(info->control.rates));
  1780. txrate = ieee80211_get_tx_rate(hw, info);
  1781. if (txrate)
  1782. bitrate = txrate->bitrate;
  1783. mgmt = (struct ieee80211_mgmt *) skb->data;
  1784. /* fake header transmission time */
  1785. data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
  1786. if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
  1787. struct ieee80211_ext *ext = (void *) mgmt;
  1788. ext->u.s1g_beacon.timestamp = cpu_to_le32(data->abs_bcn_ts +
  1789. data->tsf_offset +
  1790. 10 * 8 * 10 /
  1791. bitrate);
  1792. } else {
  1793. mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
  1794. data->tsf_offset +
  1795. 24 * 8 * 10 /
  1796. bitrate);
  1797. }
  1798. mac80211_hwsim_tx_frame(hw, skb,
  1799. rcu_dereference(link_conf->chanctx_conf)->def.chan);
  1800. while ((skb = ieee80211_get_buffered_bc(hw, vif)) != NULL) {
  1801. mac80211_hwsim_tx_frame(hw, skb,
  1802. rcu_dereference(link_conf->chanctx_conf)->def.chan);
  1803. }
  1804. if (link_conf->csa_active && ieee80211_beacon_cntdwn_is_complete(vif))
  1805. ieee80211_csa_finish(vif);
  1806. }
  1807. static enum hrtimer_restart
  1808. mac80211_hwsim_beacon(struct hrtimer *timer)
  1809. {
  1810. struct mac80211_hwsim_link_data *link_data =
  1811. container_of(timer, struct mac80211_hwsim_link_data, beacon_timer);
  1812. struct mac80211_hwsim_data *data =
  1813. container_of(link_data, struct mac80211_hwsim_data,
  1814. link_data[link_data->link_id]);
  1815. struct ieee80211_hw *hw = data->hw;
  1816. u64 bcn_int = link_data->beacon_int;
  1817. if (!data->started)
  1818. return HRTIMER_NORESTART;
  1819. ieee80211_iterate_active_interfaces_atomic(
  1820. hw, IEEE80211_IFACE_ITER_NORMAL,
  1821. mac80211_hwsim_beacon_tx, link_data);
  1822. /* beacon at new TBTT + beacon interval */
  1823. if (data->bcn_delta) {
  1824. bcn_int -= data->bcn_delta;
  1825. data->bcn_delta = 0;
  1826. }
  1827. hrtimer_forward_now(&link_data->beacon_timer,
  1828. ns_to_ktime(bcn_int * NSEC_PER_USEC));
  1829. return HRTIMER_RESTART;
  1830. }
  1831. static const char * const hwsim_chanwidths[] = {
  1832. [NL80211_CHAN_WIDTH_5] = "ht5",
  1833. [NL80211_CHAN_WIDTH_10] = "ht10",
  1834. [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
  1835. [NL80211_CHAN_WIDTH_20] = "ht20",
  1836. [NL80211_CHAN_WIDTH_40] = "ht40",
  1837. [NL80211_CHAN_WIDTH_80] = "vht80",
  1838. [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
  1839. [NL80211_CHAN_WIDTH_160] = "vht160",
  1840. [NL80211_CHAN_WIDTH_1] = "1MHz",
  1841. [NL80211_CHAN_WIDTH_2] = "2MHz",
  1842. [NL80211_CHAN_WIDTH_4] = "4MHz",
  1843. [NL80211_CHAN_WIDTH_8] = "8MHz",
  1844. [NL80211_CHAN_WIDTH_16] = "16MHz",
  1845. };
  1846. static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
  1847. {
  1848. struct mac80211_hwsim_data *data = hw->priv;
  1849. struct ieee80211_conf *conf = &hw->conf;
  1850. static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
  1851. [IEEE80211_SMPS_AUTOMATIC] = "auto",
  1852. [IEEE80211_SMPS_OFF] = "off",
  1853. [IEEE80211_SMPS_STATIC] = "static",
  1854. [IEEE80211_SMPS_DYNAMIC] = "dynamic",
  1855. };
  1856. int idx;
  1857. if (conf->chandef.chan)
  1858. wiphy_dbg(hw->wiphy,
  1859. "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
  1860. __func__,
  1861. conf->chandef.chan->center_freq,
  1862. conf->chandef.center_freq1,
  1863. conf->chandef.center_freq2,
  1864. hwsim_chanwidths[conf->chandef.width],
  1865. !!(conf->flags & IEEE80211_CONF_IDLE),
  1866. !!(conf->flags & IEEE80211_CONF_PS),
  1867. smps_modes[conf->smps_mode]);
  1868. else
  1869. wiphy_dbg(hw->wiphy,
  1870. "%s (freq=0 idle=%d ps=%d smps=%s)\n",
  1871. __func__,
  1872. !!(conf->flags & IEEE80211_CONF_IDLE),
  1873. !!(conf->flags & IEEE80211_CONF_PS),
  1874. smps_modes[conf->smps_mode]);
  1875. data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
  1876. WARN_ON(conf->chandef.chan && data->use_chanctx);
  1877. mutex_lock(&data->mutex);
  1878. if (data->scanning && conf->chandef.chan) {
  1879. for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
  1880. if (data->survey_data[idx].channel == data->channel) {
  1881. data->survey_data[idx].start =
  1882. data->survey_data[idx].next_start;
  1883. data->survey_data[idx].end = jiffies;
  1884. break;
  1885. }
  1886. }
  1887. data->channel = conf->chandef.chan;
  1888. data->bw = conf->chandef.width;
  1889. for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
  1890. if (data->survey_data[idx].channel &&
  1891. data->survey_data[idx].channel != data->channel)
  1892. continue;
  1893. data->survey_data[idx].channel = data->channel;
  1894. data->survey_data[idx].next_start = jiffies;
  1895. break;
  1896. }
  1897. } else {
  1898. data->channel = conf->chandef.chan;
  1899. data->bw = conf->chandef.width;
  1900. }
  1901. mutex_unlock(&data->mutex);
  1902. for (idx = 0; idx < ARRAY_SIZE(data->link_data); idx++) {
  1903. struct mac80211_hwsim_link_data *link_data =
  1904. &data->link_data[idx];
  1905. if (!data->started || !link_data->beacon_int) {
  1906. hrtimer_cancel(&link_data->beacon_timer);
  1907. } else if (!hrtimer_is_queued(&link_data->beacon_timer)) {
  1908. u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
  1909. u32 bcn_int = link_data->beacon_int;
  1910. u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
  1911. hrtimer_start(&link_data->beacon_timer,
  1912. ns_to_ktime(until_tbtt * NSEC_PER_USEC),
  1913. HRTIMER_MODE_REL_SOFT);
  1914. }
  1915. }
  1916. return 0;
  1917. }
  1918. static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
  1919. unsigned int changed_flags,
  1920. unsigned int *total_flags,u64 multicast)
  1921. {
  1922. struct mac80211_hwsim_data *data = hw->priv;
  1923. wiphy_dbg(hw->wiphy, "%s\n", __func__);
  1924. data->rx_filter = 0;
  1925. if (*total_flags & FIF_ALLMULTI)
  1926. data->rx_filter |= FIF_ALLMULTI;
  1927. if (*total_flags & FIF_MCAST_ACTION)
  1928. data->rx_filter |= FIF_MCAST_ACTION;
  1929. *total_flags = data->rx_filter;
  1930. }
  1931. static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
  1932. struct ieee80211_vif *vif)
  1933. {
  1934. unsigned int *count = data;
  1935. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1936. if (vp->bcn_en)
  1937. (*count)++;
  1938. }
  1939. static void mac80211_hwsim_vif_info_changed(struct ieee80211_hw *hw,
  1940. struct ieee80211_vif *vif,
  1941. u64 changed)
  1942. {
  1943. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1944. hwsim_check_magic(vif);
  1945. wiphy_dbg(hw->wiphy, "%s(changed=0x%llx vif->addr=%pM)\n",
  1946. __func__, changed, vif->addr);
  1947. if (changed & BSS_CHANGED_ASSOC) {
  1948. wiphy_dbg(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
  1949. vif->cfg.assoc, vif->cfg.aid);
  1950. vp->assoc = vif->cfg.assoc;
  1951. vp->aid = vif->cfg.aid;
  1952. }
  1953. }
  1954. static void mac80211_hwsim_link_info_changed(struct ieee80211_hw *hw,
  1955. struct ieee80211_vif *vif,
  1956. struct ieee80211_bss_conf *info,
  1957. u64 changed)
  1958. {
  1959. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1960. struct mac80211_hwsim_data *data = hw->priv;
  1961. unsigned int link_id = info->link_id;
  1962. struct mac80211_hwsim_link_data *link_data = &data->link_data[link_id];
  1963. hwsim_check_magic(vif);
  1964. wiphy_dbg(hw->wiphy, "%s(changed=0x%llx vif->addr=%pM, link id %u)\n",
  1965. __func__, (unsigned long long)changed, vif->addr, link_id);
  1966. if (changed & BSS_CHANGED_BSSID) {
  1967. wiphy_dbg(hw->wiphy, "%s: BSSID changed: %pM\n",
  1968. __func__, info->bssid);
  1969. memcpy(vp->bssid, info->bssid, ETH_ALEN);
  1970. }
  1971. if (changed & BSS_CHANGED_BEACON_ENABLED) {
  1972. wiphy_dbg(hw->wiphy, " BCN EN: %d (BI=%u)\n",
  1973. info->enable_beacon, info->beacon_int);
  1974. vp->bcn_en = info->enable_beacon;
  1975. if (data->started &&
  1976. !hrtimer_is_queued(&link_data->beacon_timer) &&
  1977. info->enable_beacon) {
  1978. u64 tsf, until_tbtt;
  1979. u32 bcn_int;
  1980. link_data->beacon_int = info->beacon_int * 1024;
  1981. tsf = mac80211_hwsim_get_tsf(hw, vif);
  1982. bcn_int = link_data->beacon_int;
  1983. until_tbtt = bcn_int - do_div(tsf, bcn_int);
  1984. hrtimer_start(&link_data->beacon_timer,
  1985. ns_to_ktime(until_tbtt * NSEC_PER_USEC),
  1986. HRTIMER_MODE_REL_SOFT);
  1987. } else if (!info->enable_beacon) {
  1988. unsigned int count = 0;
  1989. ieee80211_iterate_active_interfaces_atomic(
  1990. data->hw, IEEE80211_IFACE_ITER_NORMAL,
  1991. mac80211_hwsim_bcn_en_iter, &count);
  1992. wiphy_dbg(hw->wiphy, " beaconing vifs remaining: %u",
  1993. count);
  1994. if (count == 0) {
  1995. hrtimer_cancel(&link_data->beacon_timer);
  1996. link_data->beacon_int = 0;
  1997. }
  1998. }
  1999. }
  2000. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  2001. wiphy_dbg(hw->wiphy, " ERP_CTS_PROT: %d\n",
  2002. info->use_cts_prot);
  2003. }
  2004. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  2005. wiphy_dbg(hw->wiphy, " ERP_PREAMBLE: %d\n",
  2006. info->use_short_preamble);
  2007. }
  2008. if (changed & BSS_CHANGED_ERP_SLOT) {
  2009. wiphy_dbg(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
  2010. }
  2011. if (changed & BSS_CHANGED_HT) {
  2012. wiphy_dbg(hw->wiphy, " HT: op_mode=0x%x\n",
  2013. info->ht_operation_mode);
  2014. }
  2015. if (changed & BSS_CHANGED_BASIC_RATES) {
  2016. wiphy_dbg(hw->wiphy, " BASIC_RATES: 0x%llx\n",
  2017. (unsigned long long) info->basic_rates);
  2018. }
  2019. if (changed & BSS_CHANGED_TXPOWER)
  2020. wiphy_dbg(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
  2021. }
  2022. static void
  2023. mac80211_hwsim_sta_rc_update(struct ieee80211_hw *hw,
  2024. struct ieee80211_vif *vif,
  2025. struct ieee80211_sta *sta,
  2026. u32 changed)
  2027. {
  2028. struct mac80211_hwsim_data *data = hw->priv;
  2029. u32 bw = U32_MAX;
  2030. int link_id;
  2031. rcu_read_lock();
  2032. for (link_id = 0;
  2033. link_id < ARRAY_SIZE(vif->link_conf);
  2034. link_id++) {
  2035. enum nl80211_chan_width confbw = NL80211_CHAN_WIDTH_20_NOHT;
  2036. struct ieee80211_bss_conf *vif_conf;
  2037. struct ieee80211_link_sta *link_sta;
  2038. link_sta = rcu_dereference(sta->link[link_id]);
  2039. if (!link_sta)
  2040. continue;
  2041. switch (link_sta->bandwidth) {
  2042. #define C(_bw) case IEEE80211_STA_RX_BW_##_bw: bw = _bw; break
  2043. C(20);
  2044. C(40);
  2045. C(80);
  2046. C(160);
  2047. C(320);
  2048. #undef C
  2049. }
  2050. if (!data->use_chanctx) {
  2051. confbw = data->bw;
  2052. } else {
  2053. struct ieee80211_chanctx_conf *chanctx_conf;
  2054. vif_conf = rcu_dereference(vif->link_conf[link_id]);
  2055. if (WARN_ON(!vif_conf))
  2056. continue;
  2057. chanctx_conf = rcu_dereference(vif_conf->chanctx_conf);
  2058. if (!WARN_ON(!chanctx_conf))
  2059. confbw = chanctx_conf->def.width;
  2060. }
  2061. WARN(bw > hwsim_get_chanwidth(confbw),
  2062. "intf %pM [link=%d]: bad STA %pM bandwidth %d MHz (%d) > channel config %d MHz (%d)\n",
  2063. vif->addr, link_id, sta->addr, bw, sta->deflink.bandwidth,
  2064. hwsim_get_chanwidth(data->bw), data->bw);
  2065. }
  2066. rcu_read_unlock();
  2067. }
  2068. static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
  2069. struct ieee80211_vif *vif,
  2070. struct ieee80211_sta *sta)
  2071. {
  2072. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  2073. hwsim_check_magic(vif);
  2074. hwsim_set_sta_magic(sta);
  2075. mac80211_hwsim_sta_rc_update(hw, vif, sta, 0);
  2076. if (sta->valid_links) {
  2077. WARN(hweight16(sta->valid_links) > 1,
  2078. "expect to add STA with single link, have 0x%x\n",
  2079. sta->valid_links);
  2080. sp->active_links_rx = sta->valid_links;
  2081. }
  2082. return 0;
  2083. }
  2084. static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
  2085. struct ieee80211_vif *vif,
  2086. struct ieee80211_sta *sta)
  2087. {
  2088. hwsim_check_magic(vif);
  2089. hwsim_clear_sta_magic(sta);
  2090. return 0;
  2091. }
  2092. static int mac80211_hwsim_sta_state(struct ieee80211_hw *hw,
  2093. struct ieee80211_vif *vif,
  2094. struct ieee80211_sta *sta,
  2095. enum ieee80211_sta_state old_state,
  2096. enum ieee80211_sta_state new_state)
  2097. {
  2098. if (new_state == IEEE80211_STA_NOTEXIST)
  2099. return mac80211_hwsim_sta_remove(hw, vif, sta);
  2100. if (old_state == IEEE80211_STA_NOTEXIST)
  2101. return mac80211_hwsim_sta_add(hw, vif, sta);
  2102. /*
  2103. * when client is authorized (AP station marked as such),
  2104. * enable all links
  2105. */
  2106. if (vif->type == NL80211_IFTYPE_STATION &&
  2107. new_state == IEEE80211_STA_AUTHORIZED && !sta->tdls)
  2108. ieee80211_set_active_links_async(vif, vif->valid_links);
  2109. return 0;
  2110. }
  2111. static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
  2112. struct ieee80211_vif *vif,
  2113. enum sta_notify_cmd cmd,
  2114. struct ieee80211_sta *sta)
  2115. {
  2116. hwsim_check_magic(vif);
  2117. switch (cmd) {
  2118. case STA_NOTIFY_SLEEP:
  2119. case STA_NOTIFY_AWAKE:
  2120. /* TODO: make good use of these flags */
  2121. break;
  2122. default:
  2123. WARN(1, "Invalid sta notify: %d\n", cmd);
  2124. break;
  2125. }
  2126. }
  2127. static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
  2128. struct ieee80211_sta *sta,
  2129. bool set)
  2130. {
  2131. hwsim_check_sta_magic(sta);
  2132. return 0;
  2133. }
  2134. static int mac80211_hwsim_conf_tx(struct ieee80211_hw *hw,
  2135. struct ieee80211_vif *vif,
  2136. unsigned int link_id, u16 queue,
  2137. const struct ieee80211_tx_queue_params *params)
  2138. {
  2139. wiphy_dbg(hw->wiphy,
  2140. "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
  2141. __func__, queue,
  2142. params->txop, params->cw_min,
  2143. params->cw_max, params->aifs);
  2144. return 0;
  2145. }
  2146. static int mac80211_hwsim_get_survey(struct ieee80211_hw *hw, int idx,
  2147. struct survey_info *survey)
  2148. {
  2149. struct mac80211_hwsim_data *hwsim = hw->priv;
  2150. if (idx < 0 || idx >= ARRAY_SIZE(hwsim->survey_data))
  2151. return -ENOENT;
  2152. mutex_lock(&hwsim->mutex);
  2153. survey->channel = hwsim->survey_data[idx].channel;
  2154. if (!survey->channel) {
  2155. mutex_unlock(&hwsim->mutex);
  2156. return -ENOENT;
  2157. }
  2158. /*
  2159. * Magically conjured dummy values --- this is only ok for simulated hardware.
  2160. *
  2161. * A real driver which cannot determine real values noise MUST NOT
  2162. * report any, especially not a magically conjured ones :-)
  2163. */
  2164. survey->filled = SURVEY_INFO_NOISE_DBM |
  2165. SURVEY_INFO_TIME |
  2166. SURVEY_INFO_TIME_BUSY;
  2167. survey->noise = -92;
  2168. survey->time =
  2169. jiffies_to_msecs(hwsim->survey_data[idx].end -
  2170. hwsim->survey_data[idx].start);
  2171. /* report 12.5% of channel time is used */
  2172. survey->time_busy = survey->time/8;
  2173. mutex_unlock(&hwsim->mutex);
  2174. return 0;
  2175. }
  2176. #ifdef CONFIG_NL80211_TESTMODE
  2177. /*
  2178. * This section contains example code for using netlink
  2179. * attributes with the testmode command in nl80211.
  2180. */
  2181. /* These enums need to be kept in sync with userspace */
  2182. enum hwsim_testmode_attr {
  2183. __HWSIM_TM_ATTR_INVALID = 0,
  2184. HWSIM_TM_ATTR_CMD = 1,
  2185. HWSIM_TM_ATTR_PS = 2,
  2186. /* keep last */
  2187. __HWSIM_TM_ATTR_AFTER_LAST,
  2188. HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
  2189. };
  2190. enum hwsim_testmode_cmd {
  2191. HWSIM_TM_CMD_SET_PS = 0,
  2192. HWSIM_TM_CMD_GET_PS = 1,
  2193. HWSIM_TM_CMD_STOP_QUEUES = 2,
  2194. HWSIM_TM_CMD_WAKE_QUEUES = 3,
  2195. };
  2196. static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
  2197. [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
  2198. [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
  2199. };
  2200. static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
  2201. struct ieee80211_vif *vif,
  2202. void *data, int len)
  2203. {
  2204. struct mac80211_hwsim_data *hwsim = hw->priv;
  2205. struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
  2206. struct sk_buff *skb;
  2207. int err, ps;
  2208. err = nla_parse_deprecated(tb, HWSIM_TM_ATTR_MAX, data, len,
  2209. hwsim_testmode_policy, NULL);
  2210. if (err)
  2211. return err;
  2212. if (!tb[HWSIM_TM_ATTR_CMD])
  2213. return -EINVAL;
  2214. switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
  2215. case HWSIM_TM_CMD_SET_PS:
  2216. if (!tb[HWSIM_TM_ATTR_PS])
  2217. return -EINVAL;
  2218. ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
  2219. return hwsim_fops_ps_write(hwsim, ps);
  2220. case HWSIM_TM_CMD_GET_PS:
  2221. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  2222. nla_total_size(sizeof(u32)));
  2223. if (!skb)
  2224. return -ENOMEM;
  2225. if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
  2226. goto nla_put_failure;
  2227. return cfg80211_testmode_reply(skb);
  2228. case HWSIM_TM_CMD_STOP_QUEUES:
  2229. ieee80211_stop_queues(hw);
  2230. return 0;
  2231. case HWSIM_TM_CMD_WAKE_QUEUES:
  2232. ieee80211_wake_queues(hw);
  2233. return 0;
  2234. default:
  2235. return -EOPNOTSUPP;
  2236. }
  2237. nla_put_failure:
  2238. kfree_skb(skb);
  2239. return -ENOBUFS;
  2240. }
  2241. #endif
  2242. static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
  2243. struct ieee80211_vif *vif,
  2244. struct ieee80211_ampdu_params *params)
  2245. {
  2246. struct ieee80211_sta *sta = params->sta;
  2247. enum ieee80211_ampdu_mlme_action action = params->action;
  2248. u16 tid = params->tid;
  2249. switch (action) {
  2250. case IEEE80211_AMPDU_TX_START:
  2251. return IEEE80211_AMPDU_TX_START_IMMEDIATE;
  2252. case IEEE80211_AMPDU_TX_STOP_CONT:
  2253. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  2254. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  2255. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  2256. break;
  2257. case IEEE80211_AMPDU_TX_OPERATIONAL:
  2258. break;
  2259. case IEEE80211_AMPDU_RX_START:
  2260. case IEEE80211_AMPDU_RX_STOP:
  2261. break;
  2262. default:
  2263. return -EOPNOTSUPP;
  2264. }
  2265. return 0;
  2266. }
  2267. static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
  2268. struct ieee80211_vif *vif,
  2269. u32 queues, bool drop)
  2270. {
  2271. /* Not implemented, queues only on kernel side */
  2272. }
  2273. static void hw_scan_work(struct work_struct *work)
  2274. {
  2275. struct mac80211_hwsim_data *hwsim =
  2276. container_of(work, struct mac80211_hwsim_data, hw_scan.work);
  2277. struct cfg80211_scan_request *req = hwsim->hw_scan_request;
  2278. int dwell, i;
  2279. mutex_lock(&hwsim->mutex);
  2280. if (hwsim->scan_chan_idx >= req->n_channels) {
  2281. struct cfg80211_scan_info info = {
  2282. .aborted = false,
  2283. };
  2284. wiphy_dbg(hwsim->hw->wiphy, "hw scan complete\n");
  2285. ieee80211_scan_completed(hwsim->hw, &info);
  2286. hwsim->hw_scan_request = NULL;
  2287. hwsim->hw_scan_vif = NULL;
  2288. hwsim->tmp_chan = NULL;
  2289. mutex_unlock(&hwsim->mutex);
  2290. mac80211_hwsim_config_mac_nl(hwsim->hw, hwsim->scan_addr,
  2291. false);
  2292. return;
  2293. }
  2294. wiphy_dbg(hwsim->hw->wiphy, "hw scan %d MHz\n",
  2295. req->channels[hwsim->scan_chan_idx]->center_freq);
  2296. hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
  2297. if (hwsim->tmp_chan->flags & (IEEE80211_CHAN_NO_IR |
  2298. IEEE80211_CHAN_RADAR) ||
  2299. !req->n_ssids) {
  2300. dwell = 120;
  2301. } else {
  2302. dwell = 30;
  2303. /* send probes */
  2304. for (i = 0; i < req->n_ssids; i++) {
  2305. struct sk_buff *probe;
  2306. struct ieee80211_mgmt *mgmt;
  2307. probe = ieee80211_probereq_get(hwsim->hw,
  2308. hwsim->scan_addr,
  2309. req->ssids[i].ssid,
  2310. req->ssids[i].ssid_len,
  2311. req->ie_len);
  2312. if (!probe)
  2313. continue;
  2314. mgmt = (struct ieee80211_mgmt *) probe->data;
  2315. memcpy(mgmt->da, req->bssid, ETH_ALEN);
  2316. memcpy(mgmt->bssid, req->bssid, ETH_ALEN);
  2317. if (req->ie_len)
  2318. skb_put_data(probe, req->ie, req->ie_len);
  2319. rcu_read_lock();
  2320. if (!ieee80211_tx_prepare_skb(hwsim->hw,
  2321. hwsim->hw_scan_vif,
  2322. probe,
  2323. hwsim->tmp_chan->band,
  2324. NULL)) {
  2325. rcu_read_unlock();
  2326. kfree_skb(probe);
  2327. continue;
  2328. }
  2329. local_bh_disable();
  2330. mac80211_hwsim_tx_frame(hwsim->hw, probe,
  2331. hwsim->tmp_chan);
  2332. rcu_read_unlock();
  2333. local_bh_enable();
  2334. }
  2335. }
  2336. ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
  2337. msecs_to_jiffies(dwell));
  2338. hwsim->survey_data[hwsim->scan_chan_idx].channel = hwsim->tmp_chan;
  2339. hwsim->survey_data[hwsim->scan_chan_idx].start = jiffies;
  2340. hwsim->survey_data[hwsim->scan_chan_idx].end =
  2341. jiffies + msecs_to_jiffies(dwell);
  2342. hwsim->scan_chan_idx++;
  2343. mutex_unlock(&hwsim->mutex);
  2344. }
  2345. static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
  2346. struct ieee80211_vif *vif,
  2347. struct ieee80211_scan_request *hw_req)
  2348. {
  2349. struct mac80211_hwsim_data *hwsim = hw->priv;
  2350. struct cfg80211_scan_request *req = &hw_req->req;
  2351. mutex_lock(&hwsim->mutex);
  2352. if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
  2353. mutex_unlock(&hwsim->mutex);
  2354. return -EBUSY;
  2355. }
  2356. hwsim->hw_scan_request = req;
  2357. hwsim->hw_scan_vif = vif;
  2358. hwsim->scan_chan_idx = 0;
  2359. if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
  2360. get_random_mask_addr(hwsim->scan_addr,
  2361. hw_req->req.mac_addr,
  2362. hw_req->req.mac_addr_mask);
  2363. else
  2364. memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
  2365. memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
  2366. mutex_unlock(&hwsim->mutex);
  2367. mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, true);
  2368. wiphy_dbg(hw->wiphy, "hwsim hw_scan request\n");
  2369. ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
  2370. return 0;
  2371. }
  2372. static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
  2373. struct ieee80211_vif *vif)
  2374. {
  2375. struct mac80211_hwsim_data *hwsim = hw->priv;
  2376. struct cfg80211_scan_info info = {
  2377. .aborted = true,
  2378. };
  2379. wiphy_dbg(hw->wiphy, "hwsim cancel_hw_scan\n");
  2380. cancel_delayed_work_sync(&hwsim->hw_scan);
  2381. mutex_lock(&hwsim->mutex);
  2382. ieee80211_scan_completed(hwsim->hw, &info);
  2383. hwsim->tmp_chan = NULL;
  2384. hwsim->hw_scan_request = NULL;
  2385. hwsim->hw_scan_vif = NULL;
  2386. mutex_unlock(&hwsim->mutex);
  2387. }
  2388. static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
  2389. struct ieee80211_vif *vif,
  2390. const u8 *mac_addr)
  2391. {
  2392. struct mac80211_hwsim_data *hwsim = hw->priv;
  2393. mutex_lock(&hwsim->mutex);
  2394. if (hwsim->scanning) {
  2395. pr_debug("two hwsim sw_scans detected!\n");
  2396. goto out;
  2397. }
  2398. pr_debug("hwsim sw_scan request, prepping stuff\n");
  2399. memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
  2400. mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, true);
  2401. hwsim->scanning = true;
  2402. memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
  2403. out:
  2404. mutex_unlock(&hwsim->mutex);
  2405. }
  2406. static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
  2407. struct ieee80211_vif *vif)
  2408. {
  2409. struct mac80211_hwsim_data *hwsim = hw->priv;
  2410. mutex_lock(&hwsim->mutex);
  2411. pr_debug("hwsim sw_scan_complete\n");
  2412. hwsim->scanning = false;
  2413. mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, false);
  2414. eth_zero_addr(hwsim->scan_addr);
  2415. mutex_unlock(&hwsim->mutex);
  2416. }
  2417. static void hw_roc_start(struct work_struct *work)
  2418. {
  2419. struct mac80211_hwsim_data *hwsim =
  2420. container_of(work, struct mac80211_hwsim_data, roc_start.work);
  2421. mutex_lock(&hwsim->mutex);
  2422. wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC begins\n");
  2423. hwsim->tmp_chan = hwsim->roc_chan;
  2424. ieee80211_ready_on_channel(hwsim->hw);
  2425. ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
  2426. msecs_to_jiffies(hwsim->roc_duration));
  2427. mutex_unlock(&hwsim->mutex);
  2428. }
  2429. static void hw_roc_done(struct work_struct *work)
  2430. {
  2431. struct mac80211_hwsim_data *hwsim =
  2432. container_of(work, struct mac80211_hwsim_data, roc_done.work);
  2433. mutex_lock(&hwsim->mutex);
  2434. ieee80211_remain_on_channel_expired(hwsim->hw);
  2435. hwsim->tmp_chan = NULL;
  2436. mutex_unlock(&hwsim->mutex);
  2437. wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC expired\n");
  2438. }
  2439. static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
  2440. struct ieee80211_vif *vif,
  2441. struct ieee80211_channel *chan,
  2442. int duration,
  2443. enum ieee80211_roc_type type)
  2444. {
  2445. struct mac80211_hwsim_data *hwsim = hw->priv;
  2446. mutex_lock(&hwsim->mutex);
  2447. if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
  2448. mutex_unlock(&hwsim->mutex);
  2449. return -EBUSY;
  2450. }
  2451. hwsim->roc_chan = chan;
  2452. hwsim->roc_duration = duration;
  2453. mutex_unlock(&hwsim->mutex);
  2454. wiphy_dbg(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
  2455. chan->center_freq, duration);
  2456. ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
  2457. return 0;
  2458. }
  2459. static int mac80211_hwsim_croc(struct ieee80211_hw *hw,
  2460. struct ieee80211_vif *vif)
  2461. {
  2462. struct mac80211_hwsim_data *hwsim = hw->priv;
  2463. cancel_delayed_work_sync(&hwsim->roc_start);
  2464. cancel_delayed_work_sync(&hwsim->roc_done);
  2465. mutex_lock(&hwsim->mutex);
  2466. hwsim->tmp_chan = NULL;
  2467. mutex_unlock(&hwsim->mutex);
  2468. wiphy_dbg(hw->wiphy, "hwsim ROC canceled\n");
  2469. return 0;
  2470. }
  2471. static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
  2472. struct ieee80211_chanctx_conf *ctx)
  2473. {
  2474. hwsim_set_chanctx_magic(ctx);
  2475. wiphy_dbg(hw->wiphy,
  2476. "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
  2477. ctx->def.chan->center_freq, ctx->def.width,
  2478. ctx->def.center_freq1, ctx->def.center_freq2);
  2479. return 0;
  2480. }
  2481. static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
  2482. struct ieee80211_chanctx_conf *ctx)
  2483. {
  2484. wiphy_dbg(hw->wiphy,
  2485. "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
  2486. ctx->def.chan->center_freq, ctx->def.width,
  2487. ctx->def.center_freq1, ctx->def.center_freq2);
  2488. hwsim_check_chanctx_magic(ctx);
  2489. hwsim_clear_chanctx_magic(ctx);
  2490. }
  2491. static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
  2492. struct ieee80211_chanctx_conf *ctx,
  2493. u32 changed)
  2494. {
  2495. hwsim_check_chanctx_magic(ctx);
  2496. wiphy_dbg(hw->wiphy,
  2497. "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
  2498. ctx->def.chan->center_freq, ctx->def.width,
  2499. ctx->def.center_freq1, ctx->def.center_freq2);
  2500. }
  2501. static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
  2502. struct ieee80211_vif *vif,
  2503. struct ieee80211_bss_conf *link_conf,
  2504. struct ieee80211_chanctx_conf *ctx)
  2505. {
  2506. hwsim_check_magic(vif);
  2507. hwsim_check_chanctx_magic(ctx);
  2508. /* if we activate a link while already associated wake it up */
  2509. if (vif->type == NL80211_IFTYPE_STATION && vif->cfg.assoc) {
  2510. struct sk_buff *skb;
  2511. skb = ieee80211_nullfunc_get(hw, vif, link_conf->link_id, true);
  2512. if (skb) {
  2513. local_bh_disable();
  2514. mac80211_hwsim_tx_frame(hw, skb, ctx->def.chan);
  2515. local_bh_enable();
  2516. }
  2517. }
  2518. return 0;
  2519. }
  2520. static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
  2521. struct ieee80211_vif *vif,
  2522. struct ieee80211_bss_conf *link_conf,
  2523. struct ieee80211_chanctx_conf *ctx)
  2524. {
  2525. hwsim_check_magic(vif);
  2526. hwsim_check_chanctx_magic(ctx);
  2527. /* if we deactivate a link while associated suspend it first */
  2528. if (vif->type == NL80211_IFTYPE_STATION && vif->cfg.assoc) {
  2529. struct sk_buff *skb;
  2530. skb = ieee80211_nullfunc_get(hw, vif, link_conf->link_id, true);
  2531. if (skb) {
  2532. struct ieee80211_hdr *hdr = (void *)skb->data;
  2533. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  2534. local_bh_disable();
  2535. mac80211_hwsim_tx_frame(hw, skb, ctx->def.chan);
  2536. local_bh_enable();
  2537. }
  2538. }
  2539. }
  2540. static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
  2541. "tx_pkts_nic",
  2542. "tx_bytes_nic",
  2543. "rx_pkts_nic",
  2544. "rx_bytes_nic",
  2545. "d_tx_dropped",
  2546. "d_tx_failed",
  2547. "d_ps_mode",
  2548. "d_group",
  2549. };
  2550. #define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
  2551. static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
  2552. struct ieee80211_vif *vif,
  2553. u32 sset, u8 *data)
  2554. {
  2555. if (sset == ETH_SS_STATS)
  2556. memcpy(data, mac80211_hwsim_gstrings_stats,
  2557. sizeof(mac80211_hwsim_gstrings_stats));
  2558. }
  2559. static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
  2560. struct ieee80211_vif *vif, int sset)
  2561. {
  2562. if (sset == ETH_SS_STATS)
  2563. return MAC80211_HWSIM_SSTATS_LEN;
  2564. return 0;
  2565. }
  2566. static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
  2567. struct ieee80211_vif *vif,
  2568. struct ethtool_stats *stats, u64 *data)
  2569. {
  2570. struct mac80211_hwsim_data *ar = hw->priv;
  2571. int i = 0;
  2572. data[i++] = ar->tx_pkts;
  2573. data[i++] = ar->tx_bytes;
  2574. data[i++] = ar->rx_pkts;
  2575. data[i++] = ar->rx_bytes;
  2576. data[i++] = ar->tx_dropped;
  2577. data[i++] = ar->tx_failed;
  2578. data[i++] = ar->ps;
  2579. data[i++] = ar->group;
  2580. WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
  2581. }
  2582. static int mac80211_hwsim_tx_last_beacon(struct ieee80211_hw *hw)
  2583. {
  2584. return 1;
  2585. }
  2586. static int mac80211_hwsim_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  2587. {
  2588. return -EOPNOTSUPP;
  2589. }
  2590. static int mac80211_hwsim_change_vif_links(struct ieee80211_hw *hw,
  2591. struct ieee80211_vif *vif,
  2592. u16 old_links, u16 new_links,
  2593. struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS])
  2594. {
  2595. unsigned long rem = old_links & ~new_links;
  2596. unsigned long add = new_links & ~old_links;
  2597. int i;
  2598. if (!old_links)
  2599. rem |= BIT(0);
  2600. if (!new_links)
  2601. add |= BIT(0);
  2602. for_each_set_bit(i, &rem, IEEE80211_MLD_MAX_NUM_LINKS)
  2603. mac80211_hwsim_config_mac_nl(hw, old[i]->addr, false);
  2604. for_each_set_bit(i, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
  2605. struct ieee80211_bss_conf *link_conf;
  2606. link_conf = link_conf_dereference_protected(vif, i);
  2607. if (WARN_ON(!link_conf))
  2608. continue;
  2609. mac80211_hwsim_config_mac_nl(hw, link_conf->addr, true);
  2610. }
  2611. return 0;
  2612. }
  2613. static int mac80211_hwsim_change_sta_links(struct ieee80211_hw *hw,
  2614. struct ieee80211_vif *vif,
  2615. struct ieee80211_sta *sta,
  2616. u16 old_links, u16 new_links)
  2617. {
  2618. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  2619. hwsim_check_sta_magic(sta);
  2620. if (vif->type == NL80211_IFTYPE_STATION)
  2621. sp->active_links_rx = new_links;
  2622. return 0;
  2623. }
  2624. #define HWSIM_COMMON_OPS \
  2625. .tx = mac80211_hwsim_tx, \
  2626. .start = mac80211_hwsim_start, \
  2627. .stop = mac80211_hwsim_stop, \
  2628. .add_interface = mac80211_hwsim_add_interface, \
  2629. .change_interface = mac80211_hwsim_change_interface, \
  2630. .remove_interface = mac80211_hwsim_remove_interface, \
  2631. .config = mac80211_hwsim_config, \
  2632. .configure_filter = mac80211_hwsim_configure_filter, \
  2633. .vif_cfg_changed = mac80211_hwsim_vif_info_changed, \
  2634. .link_info_changed = mac80211_hwsim_link_info_changed, \
  2635. .tx_last_beacon = mac80211_hwsim_tx_last_beacon, \
  2636. .sta_notify = mac80211_hwsim_sta_notify, \
  2637. .sta_rc_update = mac80211_hwsim_sta_rc_update, \
  2638. .conf_tx = mac80211_hwsim_conf_tx, \
  2639. .get_survey = mac80211_hwsim_get_survey, \
  2640. CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd) \
  2641. .ampdu_action = mac80211_hwsim_ampdu_action, \
  2642. .flush = mac80211_hwsim_flush, \
  2643. .get_et_sset_count = mac80211_hwsim_get_et_sset_count, \
  2644. .get_et_stats = mac80211_hwsim_get_et_stats, \
  2645. .get_et_strings = mac80211_hwsim_get_et_strings,
  2646. #define HWSIM_NON_MLO_OPS \
  2647. .sta_add = mac80211_hwsim_sta_add, \
  2648. .sta_remove = mac80211_hwsim_sta_remove, \
  2649. .set_tim = mac80211_hwsim_set_tim, \
  2650. .get_tsf = mac80211_hwsim_get_tsf, \
  2651. .set_tsf = mac80211_hwsim_set_tsf,
  2652. static const struct ieee80211_ops mac80211_hwsim_ops = {
  2653. HWSIM_COMMON_OPS
  2654. HWSIM_NON_MLO_OPS
  2655. .sw_scan_start = mac80211_hwsim_sw_scan,
  2656. .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
  2657. };
  2658. #define HWSIM_CHANCTX_OPS \
  2659. .hw_scan = mac80211_hwsim_hw_scan, \
  2660. .cancel_hw_scan = mac80211_hwsim_cancel_hw_scan, \
  2661. .remain_on_channel = mac80211_hwsim_roc, \
  2662. .cancel_remain_on_channel = mac80211_hwsim_croc, \
  2663. .add_chanctx = mac80211_hwsim_add_chanctx, \
  2664. .remove_chanctx = mac80211_hwsim_remove_chanctx, \
  2665. .change_chanctx = mac80211_hwsim_change_chanctx, \
  2666. .assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,\
  2667. .unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx,
  2668. static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
  2669. HWSIM_COMMON_OPS
  2670. HWSIM_NON_MLO_OPS
  2671. HWSIM_CHANCTX_OPS
  2672. };
  2673. static const struct ieee80211_ops mac80211_hwsim_mlo_ops = {
  2674. HWSIM_COMMON_OPS
  2675. HWSIM_CHANCTX_OPS
  2676. .set_rts_threshold = mac80211_hwsim_set_rts_threshold,
  2677. .change_vif_links = mac80211_hwsim_change_vif_links,
  2678. .change_sta_links = mac80211_hwsim_change_sta_links,
  2679. .sta_state = mac80211_hwsim_sta_state,
  2680. };
  2681. struct hwsim_new_radio_params {
  2682. unsigned int channels;
  2683. const char *reg_alpha2;
  2684. const struct ieee80211_regdomain *regd;
  2685. bool reg_strict;
  2686. bool p2p_device;
  2687. bool use_chanctx;
  2688. bool destroy_on_close;
  2689. const char *hwname;
  2690. bool no_vif;
  2691. const u8 *perm_addr;
  2692. u32 iftypes;
  2693. u32 *ciphers;
  2694. u8 n_ciphers;
  2695. bool mlo;
  2696. };
  2697. static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
  2698. struct genl_info *info)
  2699. {
  2700. if (info)
  2701. genl_notify(&hwsim_genl_family, mcast_skb, info,
  2702. HWSIM_MCGRP_CONFIG, GFP_KERNEL);
  2703. else
  2704. genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
  2705. HWSIM_MCGRP_CONFIG, GFP_KERNEL);
  2706. }
  2707. static int append_radio_msg(struct sk_buff *skb, int id,
  2708. struct hwsim_new_radio_params *param)
  2709. {
  2710. int ret;
  2711. ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
  2712. if (ret < 0)
  2713. return ret;
  2714. if (param->channels) {
  2715. ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
  2716. if (ret < 0)
  2717. return ret;
  2718. }
  2719. if (param->reg_alpha2) {
  2720. ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
  2721. param->reg_alpha2);
  2722. if (ret < 0)
  2723. return ret;
  2724. }
  2725. if (param->regd) {
  2726. int i;
  2727. for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
  2728. if (hwsim_world_regdom_custom[i] != param->regd)
  2729. continue;
  2730. ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
  2731. if (ret < 0)
  2732. return ret;
  2733. break;
  2734. }
  2735. }
  2736. if (param->reg_strict) {
  2737. ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
  2738. if (ret < 0)
  2739. return ret;
  2740. }
  2741. if (param->p2p_device) {
  2742. ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
  2743. if (ret < 0)
  2744. return ret;
  2745. }
  2746. if (param->use_chanctx) {
  2747. ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
  2748. if (ret < 0)
  2749. return ret;
  2750. }
  2751. if (param->hwname) {
  2752. ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
  2753. strlen(param->hwname), param->hwname);
  2754. if (ret < 0)
  2755. return ret;
  2756. }
  2757. return 0;
  2758. }
  2759. static void hwsim_mcast_new_radio(int id, struct genl_info *info,
  2760. struct hwsim_new_radio_params *param)
  2761. {
  2762. struct sk_buff *mcast_skb;
  2763. void *data;
  2764. mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  2765. if (!mcast_skb)
  2766. return;
  2767. data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
  2768. HWSIM_CMD_NEW_RADIO);
  2769. if (!data)
  2770. goto out_err;
  2771. if (append_radio_msg(mcast_skb, id, param) < 0)
  2772. goto out_err;
  2773. genlmsg_end(mcast_skb, data);
  2774. hwsim_mcast_config_msg(mcast_skb, info);
  2775. return;
  2776. out_err:
  2777. nlmsg_free(mcast_skb);
  2778. }
  2779. static const struct ieee80211_sband_iftype_data sband_capa_2ghz[] = {
  2780. {
  2781. .types_mask = BIT(NL80211_IFTYPE_STATION),
  2782. .he_cap = {
  2783. .has_he = true,
  2784. .he_cap_elem = {
  2785. .mac_cap_info[0] =
  2786. IEEE80211_HE_MAC_CAP0_HTC_HE,
  2787. .mac_cap_info[1] =
  2788. IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
  2789. IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
  2790. .mac_cap_info[2] =
  2791. IEEE80211_HE_MAC_CAP2_BSR |
  2792. IEEE80211_HE_MAC_CAP2_MU_CASCADING |
  2793. IEEE80211_HE_MAC_CAP2_ACK_EN,
  2794. .mac_cap_info[3] =
  2795. IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
  2796. IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
  2797. .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
  2798. .phy_cap_info[1] =
  2799. IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
  2800. IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
  2801. IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
  2802. IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
  2803. .phy_cap_info[2] =
  2804. IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
  2805. IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
  2806. IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
  2807. IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
  2808. IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
  2809. /* Leave all the other PHY capability bytes
  2810. * unset, as DCM, beam forming, RU and PPE
  2811. * threshold information are not supported
  2812. */
  2813. },
  2814. .he_mcs_nss_supp = {
  2815. .rx_mcs_80 = cpu_to_le16(0xfffa),
  2816. .tx_mcs_80 = cpu_to_le16(0xfffa),
  2817. .rx_mcs_160 = cpu_to_le16(0xffff),
  2818. .tx_mcs_160 = cpu_to_le16(0xffff),
  2819. .rx_mcs_80p80 = cpu_to_le16(0xffff),
  2820. .tx_mcs_80p80 = cpu_to_le16(0xffff),
  2821. },
  2822. },
  2823. .eht_cap = {
  2824. .has_eht = true,
  2825. .eht_cap_elem = {
  2826. .mac_cap_info[0] =
  2827. IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
  2828. IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
  2829. IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
  2830. .phy_cap_info[0] =
  2831. IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
  2832. IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
  2833. IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
  2834. IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
  2835. IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE,
  2836. .phy_cap_info[3] =
  2837. IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
  2838. IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
  2839. IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
  2840. IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
  2841. IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
  2842. IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
  2843. IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
  2844. .phy_cap_info[4] =
  2845. IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
  2846. IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
  2847. IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
  2848. IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
  2849. IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
  2850. .phy_cap_info[5] =
  2851. IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
  2852. IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
  2853. IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
  2854. IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
  2855. IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
  2856. IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
  2857. .phy_cap_info[6] =
  2858. IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
  2859. IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK,
  2860. .phy_cap_info[7] =
  2861. IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW,
  2862. },
  2863. /* For all MCS and bandwidth, set 8 NSS for both Tx and
  2864. * Rx
  2865. */
  2866. .eht_mcs_nss_supp = {
  2867. /*
  2868. * Since B0, B1, B2 and B3 are not set in
  2869. * the supported channel width set field in the
  2870. * HE PHY capabilities information field the
  2871. * device is a 20MHz only device on 2.4GHz band.
  2872. */
  2873. .only_20mhz = {
  2874. .rx_tx_mcs7_max_nss = 0x88,
  2875. .rx_tx_mcs9_max_nss = 0x88,
  2876. .rx_tx_mcs11_max_nss = 0x88,
  2877. .rx_tx_mcs13_max_nss = 0x88,
  2878. },
  2879. },
  2880. /* PPE threshold information is not supported */
  2881. },
  2882. },
  2883. {
  2884. .types_mask = BIT(NL80211_IFTYPE_AP),
  2885. .he_cap = {
  2886. .has_he = true,
  2887. .he_cap_elem = {
  2888. .mac_cap_info[0] =
  2889. IEEE80211_HE_MAC_CAP0_HTC_HE,
  2890. .mac_cap_info[1] =
  2891. IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
  2892. IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
  2893. .mac_cap_info[2] =
  2894. IEEE80211_HE_MAC_CAP2_BSR |
  2895. IEEE80211_HE_MAC_CAP2_MU_CASCADING |
  2896. IEEE80211_HE_MAC_CAP2_ACK_EN,
  2897. .mac_cap_info[3] =
  2898. IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
  2899. IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
  2900. .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
  2901. .phy_cap_info[1] =
  2902. IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
  2903. IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
  2904. IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
  2905. IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
  2906. .phy_cap_info[2] =
  2907. IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
  2908. IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
  2909. IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
  2910. IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
  2911. IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
  2912. /* Leave all the other PHY capability bytes
  2913. * unset, as DCM, beam forming, RU and PPE
  2914. * threshold information are not supported
  2915. */
  2916. },
  2917. .he_mcs_nss_supp = {
  2918. .rx_mcs_80 = cpu_to_le16(0xfffa),
  2919. .tx_mcs_80 = cpu_to_le16(0xfffa),
  2920. .rx_mcs_160 = cpu_to_le16(0xffff),
  2921. .tx_mcs_160 = cpu_to_le16(0xffff),
  2922. .rx_mcs_80p80 = cpu_to_le16(0xffff),
  2923. .tx_mcs_80p80 = cpu_to_le16(0xffff),
  2924. },
  2925. },
  2926. .eht_cap = {
  2927. .has_eht = true,
  2928. .eht_cap_elem = {
  2929. .mac_cap_info[0] =
  2930. IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
  2931. IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
  2932. IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
  2933. .phy_cap_info[0] =
  2934. IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
  2935. IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
  2936. IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
  2937. IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
  2938. IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE,
  2939. .phy_cap_info[3] =
  2940. IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
  2941. IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
  2942. IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
  2943. IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
  2944. IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
  2945. IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
  2946. IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
  2947. .phy_cap_info[4] =
  2948. IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
  2949. IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
  2950. IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
  2951. IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
  2952. IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
  2953. .phy_cap_info[5] =
  2954. IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
  2955. IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
  2956. IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
  2957. IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
  2958. IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
  2959. IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
  2960. .phy_cap_info[6] =
  2961. IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
  2962. IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK,
  2963. .phy_cap_info[7] =
  2964. IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW,
  2965. },
  2966. /* For all MCS and bandwidth, set 8 NSS for both Tx and
  2967. * Rx
  2968. */
  2969. .eht_mcs_nss_supp = {
  2970. /*
  2971. * Since B0, B1, B2 and B3 are not set in
  2972. * the supported channel width set field in the
  2973. * HE PHY capabilities information field the
  2974. * device is a 20MHz only device on 2.4GHz band.
  2975. */
  2976. .only_20mhz = {
  2977. .rx_tx_mcs7_max_nss = 0x88,
  2978. .rx_tx_mcs9_max_nss = 0x88,
  2979. .rx_tx_mcs11_max_nss = 0x88,
  2980. .rx_tx_mcs13_max_nss = 0x88,
  2981. },
  2982. },
  2983. /* PPE threshold information is not supported */
  2984. },
  2985. },
  2986. #ifdef CONFIG_MAC80211_MESH
  2987. {
  2988. .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
  2989. .he_cap = {
  2990. .has_he = true,
  2991. .he_cap_elem = {
  2992. .mac_cap_info[0] =
  2993. IEEE80211_HE_MAC_CAP0_HTC_HE,
  2994. .mac_cap_info[1] =
  2995. IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
  2996. .mac_cap_info[2] =
  2997. IEEE80211_HE_MAC_CAP2_ACK_EN,
  2998. .mac_cap_info[3] =
  2999. IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
  3000. IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
  3001. .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
  3002. .phy_cap_info[1] =
  3003. IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
  3004. IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
  3005. IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
  3006. IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
  3007. .phy_cap_info[2] = 0,
  3008. /* Leave all the other PHY capability bytes
  3009. * unset, as DCM, beam forming, RU and PPE
  3010. * threshold information are not supported
  3011. */
  3012. },
  3013. .he_mcs_nss_supp = {
  3014. .rx_mcs_80 = cpu_to_le16(0xfffa),
  3015. .tx_mcs_80 = cpu_to_le16(0xfffa),
  3016. .rx_mcs_160 = cpu_to_le16(0xffff),
  3017. .tx_mcs_160 = cpu_to_le16(0xffff),
  3018. .rx_mcs_80p80 = cpu_to_le16(0xffff),
  3019. .tx_mcs_80p80 = cpu_to_le16(0xffff),
  3020. },
  3021. },
  3022. },
  3023. #endif
  3024. };
  3025. static const struct ieee80211_sband_iftype_data sband_capa_5ghz[] = {
  3026. {
  3027. /* TODO: should we support other types, e.g., P2P? */
  3028. .types_mask = BIT(NL80211_IFTYPE_STATION),
  3029. .he_cap = {
  3030. .has_he = true,
  3031. .he_cap_elem = {
  3032. .mac_cap_info[0] =
  3033. IEEE80211_HE_MAC_CAP0_HTC_HE,
  3034. .mac_cap_info[1] =
  3035. IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
  3036. IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
  3037. .mac_cap_info[2] =
  3038. IEEE80211_HE_MAC_CAP2_BSR |
  3039. IEEE80211_HE_MAC_CAP2_MU_CASCADING |
  3040. IEEE80211_HE_MAC_CAP2_ACK_EN,
  3041. .mac_cap_info[3] =
  3042. IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
  3043. IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
  3044. .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
  3045. .phy_cap_info[0] =
  3046. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
  3047. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
  3048. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
  3049. .phy_cap_info[1] =
  3050. IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
  3051. IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
  3052. IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
  3053. IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
  3054. .phy_cap_info[2] =
  3055. IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
  3056. IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
  3057. IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
  3058. IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
  3059. IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
  3060. /* Leave all the other PHY capability bytes
  3061. * unset, as DCM, beam forming, RU and PPE
  3062. * threshold information are not supported
  3063. */
  3064. },
  3065. .he_mcs_nss_supp = {
  3066. .rx_mcs_80 = cpu_to_le16(0xfffa),
  3067. .tx_mcs_80 = cpu_to_le16(0xfffa),
  3068. .rx_mcs_160 = cpu_to_le16(0xfffa),
  3069. .tx_mcs_160 = cpu_to_le16(0xfffa),
  3070. .rx_mcs_80p80 = cpu_to_le16(0xfffa),
  3071. .tx_mcs_80p80 = cpu_to_le16(0xfffa),
  3072. },
  3073. },
  3074. .eht_cap = {
  3075. .has_eht = true,
  3076. .eht_cap_elem = {
  3077. .mac_cap_info[0] =
  3078. IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
  3079. IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
  3080. IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
  3081. .phy_cap_info[0] =
  3082. IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
  3083. IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
  3084. IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
  3085. IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
  3086. IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE |
  3087. IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK,
  3088. .phy_cap_info[1] =
  3089. IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK |
  3090. IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK,
  3091. .phy_cap_info[2] =
  3092. IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK |
  3093. IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK,
  3094. .phy_cap_info[3] =
  3095. IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
  3096. IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
  3097. IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
  3098. IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
  3099. IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
  3100. IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
  3101. IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
  3102. .phy_cap_info[4] =
  3103. IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
  3104. IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
  3105. IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
  3106. IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
  3107. IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
  3108. .phy_cap_info[5] =
  3109. IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
  3110. IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
  3111. IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
  3112. IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
  3113. IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
  3114. IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
  3115. .phy_cap_info[6] =
  3116. IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
  3117. IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK,
  3118. .phy_cap_info[7] =
  3119. IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW |
  3120. IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
  3121. IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
  3122. IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
  3123. IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ,
  3124. },
  3125. /* For all MCS and bandwidth, set 8 NSS for both Tx and
  3126. * Rx
  3127. */
  3128. .eht_mcs_nss_supp = {
  3129. /*
  3130. * As B1 and B2 are set in the supported
  3131. * channel width set field in the HE PHY
  3132. * capabilities information field include all
  3133. * the following MCS/NSS.
  3134. */
  3135. .bw._80 = {
  3136. .rx_tx_mcs9_max_nss = 0x88,
  3137. .rx_tx_mcs11_max_nss = 0x88,
  3138. .rx_tx_mcs13_max_nss = 0x88,
  3139. },
  3140. .bw._160 = {
  3141. .rx_tx_mcs9_max_nss = 0x88,
  3142. .rx_tx_mcs11_max_nss = 0x88,
  3143. .rx_tx_mcs13_max_nss = 0x88,
  3144. },
  3145. },
  3146. /* PPE threshold information is not supported */
  3147. },
  3148. },
  3149. {
  3150. .types_mask = BIT(NL80211_IFTYPE_AP),
  3151. .he_cap = {
  3152. .has_he = true,
  3153. .he_cap_elem = {
  3154. .mac_cap_info[0] =
  3155. IEEE80211_HE_MAC_CAP0_HTC_HE,
  3156. .mac_cap_info[1] =
  3157. IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
  3158. IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
  3159. .mac_cap_info[2] =
  3160. IEEE80211_HE_MAC_CAP2_BSR |
  3161. IEEE80211_HE_MAC_CAP2_MU_CASCADING |
  3162. IEEE80211_HE_MAC_CAP2_ACK_EN,
  3163. .mac_cap_info[3] =
  3164. IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
  3165. IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
  3166. .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
  3167. .phy_cap_info[0] =
  3168. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
  3169. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
  3170. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
  3171. .phy_cap_info[1] =
  3172. IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
  3173. IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
  3174. IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
  3175. IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
  3176. .phy_cap_info[2] =
  3177. IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
  3178. IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
  3179. IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
  3180. IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
  3181. IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
  3182. /* Leave all the other PHY capability bytes
  3183. * unset, as DCM, beam forming, RU and PPE
  3184. * threshold information are not supported
  3185. */
  3186. },
  3187. .he_mcs_nss_supp = {
  3188. .rx_mcs_80 = cpu_to_le16(0xfffa),
  3189. .tx_mcs_80 = cpu_to_le16(0xfffa),
  3190. .rx_mcs_160 = cpu_to_le16(0xfffa),
  3191. .tx_mcs_160 = cpu_to_le16(0xfffa),
  3192. .rx_mcs_80p80 = cpu_to_le16(0xfffa),
  3193. .tx_mcs_80p80 = cpu_to_le16(0xfffa),
  3194. },
  3195. },
  3196. .eht_cap = {
  3197. .has_eht = true,
  3198. .eht_cap_elem = {
  3199. .mac_cap_info[0] =
  3200. IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
  3201. IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
  3202. IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
  3203. .phy_cap_info[0] =
  3204. IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
  3205. IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
  3206. IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
  3207. IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
  3208. IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE |
  3209. IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK,
  3210. .phy_cap_info[1] =
  3211. IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK |
  3212. IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK,
  3213. .phy_cap_info[2] =
  3214. IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK |
  3215. IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK,
  3216. .phy_cap_info[3] =
  3217. IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
  3218. IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
  3219. IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
  3220. IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
  3221. IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
  3222. IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
  3223. IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
  3224. .phy_cap_info[4] =
  3225. IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
  3226. IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
  3227. IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
  3228. IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
  3229. IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
  3230. .phy_cap_info[5] =
  3231. IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
  3232. IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
  3233. IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
  3234. IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
  3235. IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
  3236. IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
  3237. .phy_cap_info[6] =
  3238. IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
  3239. IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK,
  3240. .phy_cap_info[7] =
  3241. IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW |
  3242. IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
  3243. IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
  3244. IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
  3245. IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ,
  3246. },
  3247. /* For all MCS and bandwidth, set 8 NSS for both Tx and
  3248. * Rx
  3249. */
  3250. .eht_mcs_nss_supp = {
  3251. /*
  3252. * As B1 and B2 are set in the supported
  3253. * channel width set field in the HE PHY
  3254. * capabilities information field include all
  3255. * the following MCS/NSS.
  3256. */
  3257. .bw._80 = {
  3258. .rx_tx_mcs9_max_nss = 0x88,
  3259. .rx_tx_mcs11_max_nss = 0x88,
  3260. .rx_tx_mcs13_max_nss = 0x88,
  3261. },
  3262. .bw._160 = {
  3263. .rx_tx_mcs9_max_nss = 0x88,
  3264. .rx_tx_mcs11_max_nss = 0x88,
  3265. .rx_tx_mcs13_max_nss = 0x88,
  3266. },
  3267. },
  3268. /* PPE threshold information is not supported */
  3269. },
  3270. },
  3271. #ifdef CONFIG_MAC80211_MESH
  3272. {
  3273. /* TODO: should we support other types, e.g., IBSS?*/
  3274. .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
  3275. .he_cap = {
  3276. .has_he = true,
  3277. .he_cap_elem = {
  3278. .mac_cap_info[0] =
  3279. IEEE80211_HE_MAC_CAP0_HTC_HE,
  3280. .mac_cap_info[1] =
  3281. IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
  3282. .mac_cap_info[2] =
  3283. IEEE80211_HE_MAC_CAP2_ACK_EN,
  3284. .mac_cap_info[3] =
  3285. IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
  3286. IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
  3287. .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
  3288. .phy_cap_info[0] =
  3289. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
  3290. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
  3291. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
  3292. .phy_cap_info[1] =
  3293. IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
  3294. IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
  3295. IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
  3296. IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
  3297. .phy_cap_info[2] = 0,
  3298. /* Leave all the other PHY capability bytes
  3299. * unset, as DCM, beam forming, RU and PPE
  3300. * threshold information are not supported
  3301. */
  3302. },
  3303. .he_mcs_nss_supp = {
  3304. .rx_mcs_80 = cpu_to_le16(0xfffa),
  3305. .tx_mcs_80 = cpu_to_le16(0xfffa),
  3306. .rx_mcs_160 = cpu_to_le16(0xfffa),
  3307. .tx_mcs_160 = cpu_to_le16(0xfffa),
  3308. .rx_mcs_80p80 = cpu_to_le16(0xfffa),
  3309. .tx_mcs_80p80 = cpu_to_le16(0xfffa),
  3310. },
  3311. },
  3312. },
  3313. #endif
  3314. };
  3315. static const struct ieee80211_sband_iftype_data sband_capa_6ghz[] = {
  3316. {
  3317. /* TODO: should we support other types, e.g., P2P? */
  3318. .types_mask = BIT(NL80211_IFTYPE_STATION),
  3319. .he_6ghz_capa = {
  3320. .capa = cpu_to_le16(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START |
  3321. IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP |
  3322. IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN |
  3323. IEEE80211_HE_6GHZ_CAP_SM_PS |
  3324. IEEE80211_HE_6GHZ_CAP_RD_RESPONDER |
  3325. IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS |
  3326. IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS),
  3327. },
  3328. .he_cap = {
  3329. .has_he = true,
  3330. .he_cap_elem = {
  3331. .mac_cap_info[0] =
  3332. IEEE80211_HE_MAC_CAP0_HTC_HE,
  3333. .mac_cap_info[1] =
  3334. IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
  3335. IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
  3336. .mac_cap_info[2] =
  3337. IEEE80211_HE_MAC_CAP2_BSR |
  3338. IEEE80211_HE_MAC_CAP2_MU_CASCADING |
  3339. IEEE80211_HE_MAC_CAP2_ACK_EN,
  3340. .mac_cap_info[3] =
  3341. IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
  3342. IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
  3343. .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
  3344. .phy_cap_info[0] =
  3345. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
  3346. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
  3347. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
  3348. .phy_cap_info[1] =
  3349. IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
  3350. IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
  3351. IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
  3352. IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
  3353. .phy_cap_info[2] =
  3354. IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
  3355. IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
  3356. IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
  3357. IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
  3358. IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
  3359. /* Leave all the other PHY capability bytes
  3360. * unset, as DCM, beam forming, RU and PPE
  3361. * threshold information are not supported
  3362. */
  3363. },
  3364. .he_mcs_nss_supp = {
  3365. .rx_mcs_80 = cpu_to_le16(0xfffa),
  3366. .tx_mcs_80 = cpu_to_le16(0xfffa),
  3367. .rx_mcs_160 = cpu_to_le16(0xfffa),
  3368. .tx_mcs_160 = cpu_to_le16(0xfffa),
  3369. .rx_mcs_80p80 = cpu_to_le16(0xfffa),
  3370. .tx_mcs_80p80 = cpu_to_le16(0xfffa),
  3371. },
  3372. },
  3373. .eht_cap = {
  3374. .has_eht = true,
  3375. .eht_cap_elem = {
  3376. .mac_cap_info[0] =
  3377. IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
  3378. IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
  3379. IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
  3380. .phy_cap_info[0] =
  3381. IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ |
  3382. IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
  3383. IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
  3384. IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
  3385. IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
  3386. IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE |
  3387. IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK,
  3388. .phy_cap_info[1] =
  3389. IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK |
  3390. IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK |
  3391. IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_320MHZ_MASK,
  3392. .phy_cap_info[2] =
  3393. IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK |
  3394. IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK |
  3395. IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_320MHZ_MASK,
  3396. .phy_cap_info[3] =
  3397. IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
  3398. IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
  3399. IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
  3400. IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
  3401. IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
  3402. IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
  3403. IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
  3404. .phy_cap_info[4] =
  3405. IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
  3406. IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
  3407. IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
  3408. IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
  3409. IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
  3410. .phy_cap_info[5] =
  3411. IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
  3412. IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
  3413. IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
  3414. IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
  3415. IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
  3416. IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
  3417. .phy_cap_info[6] =
  3418. IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
  3419. IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK |
  3420. IEEE80211_EHT_PHY_CAP6_EHT_DUP_6GHZ_SUPP,
  3421. .phy_cap_info[7] =
  3422. IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW |
  3423. IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
  3424. IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
  3425. IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
  3426. IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
  3427. IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
  3428. IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ,
  3429. },
  3430. /* For all MCS and bandwidth, set 8 NSS for both Tx and
  3431. * Rx
  3432. */
  3433. .eht_mcs_nss_supp = {
  3434. /*
  3435. * As B1 and B2 are set in the supported
  3436. * channel width set field in the HE PHY
  3437. * capabilities information field and 320MHz in
  3438. * 6GHz is supported include all the following
  3439. * MCS/NSS.
  3440. */
  3441. .bw._80 = {
  3442. .rx_tx_mcs9_max_nss = 0x88,
  3443. .rx_tx_mcs11_max_nss = 0x88,
  3444. .rx_tx_mcs13_max_nss = 0x88,
  3445. },
  3446. .bw._160 = {
  3447. .rx_tx_mcs9_max_nss = 0x88,
  3448. .rx_tx_mcs11_max_nss = 0x88,
  3449. .rx_tx_mcs13_max_nss = 0x88,
  3450. },
  3451. .bw._320 = {
  3452. .rx_tx_mcs9_max_nss = 0x88,
  3453. .rx_tx_mcs11_max_nss = 0x88,
  3454. .rx_tx_mcs13_max_nss = 0x88,
  3455. },
  3456. },
  3457. /* PPE threshold information is not supported */
  3458. },
  3459. },
  3460. {
  3461. .types_mask = BIT(NL80211_IFTYPE_AP),
  3462. .he_6ghz_capa = {
  3463. .capa = cpu_to_le16(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START |
  3464. IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP |
  3465. IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN |
  3466. IEEE80211_HE_6GHZ_CAP_SM_PS |
  3467. IEEE80211_HE_6GHZ_CAP_RD_RESPONDER |
  3468. IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS |
  3469. IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS),
  3470. },
  3471. .he_cap = {
  3472. .has_he = true,
  3473. .he_cap_elem = {
  3474. .mac_cap_info[0] =
  3475. IEEE80211_HE_MAC_CAP0_HTC_HE,
  3476. .mac_cap_info[1] =
  3477. IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
  3478. IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
  3479. .mac_cap_info[2] =
  3480. IEEE80211_HE_MAC_CAP2_BSR |
  3481. IEEE80211_HE_MAC_CAP2_MU_CASCADING |
  3482. IEEE80211_HE_MAC_CAP2_ACK_EN,
  3483. .mac_cap_info[3] =
  3484. IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
  3485. IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
  3486. .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
  3487. .phy_cap_info[0] =
  3488. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
  3489. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
  3490. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
  3491. .phy_cap_info[1] =
  3492. IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
  3493. IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
  3494. IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
  3495. IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
  3496. .phy_cap_info[2] =
  3497. IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
  3498. IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
  3499. IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
  3500. IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
  3501. IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
  3502. /* Leave all the other PHY capability bytes
  3503. * unset, as DCM, beam forming, RU and PPE
  3504. * threshold information are not supported
  3505. */
  3506. },
  3507. .he_mcs_nss_supp = {
  3508. .rx_mcs_80 = cpu_to_le16(0xfffa),
  3509. .tx_mcs_80 = cpu_to_le16(0xfffa),
  3510. .rx_mcs_160 = cpu_to_le16(0xfffa),
  3511. .tx_mcs_160 = cpu_to_le16(0xfffa),
  3512. .rx_mcs_80p80 = cpu_to_le16(0xfffa),
  3513. .tx_mcs_80p80 = cpu_to_le16(0xfffa),
  3514. },
  3515. },
  3516. .eht_cap = {
  3517. .has_eht = true,
  3518. .eht_cap_elem = {
  3519. .mac_cap_info[0] =
  3520. IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
  3521. IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
  3522. IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
  3523. .phy_cap_info[0] =
  3524. IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ |
  3525. IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
  3526. IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
  3527. IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
  3528. IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
  3529. IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE |
  3530. IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK,
  3531. .phy_cap_info[1] =
  3532. IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK |
  3533. IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK |
  3534. IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_320MHZ_MASK,
  3535. .phy_cap_info[2] =
  3536. IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK |
  3537. IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK |
  3538. IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_320MHZ_MASK,
  3539. .phy_cap_info[3] =
  3540. IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
  3541. IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
  3542. IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
  3543. IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
  3544. IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
  3545. IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
  3546. IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
  3547. .phy_cap_info[4] =
  3548. IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
  3549. IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
  3550. IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
  3551. IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
  3552. IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
  3553. .phy_cap_info[5] =
  3554. IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
  3555. IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
  3556. IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
  3557. IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
  3558. IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
  3559. IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
  3560. .phy_cap_info[6] =
  3561. IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
  3562. IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK |
  3563. IEEE80211_EHT_PHY_CAP6_EHT_DUP_6GHZ_SUPP,
  3564. .phy_cap_info[7] =
  3565. IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW |
  3566. IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
  3567. IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
  3568. IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
  3569. IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
  3570. IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
  3571. IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ,
  3572. },
  3573. /* For all MCS and bandwidth, set 8 NSS for both Tx and
  3574. * Rx
  3575. */
  3576. .eht_mcs_nss_supp = {
  3577. /*
  3578. * As B1 and B2 are set in the supported
  3579. * channel width set field in the HE PHY
  3580. * capabilities information field and 320MHz in
  3581. * 6GHz is supported include all the following
  3582. * MCS/NSS.
  3583. */
  3584. .bw._80 = {
  3585. .rx_tx_mcs9_max_nss = 0x88,
  3586. .rx_tx_mcs11_max_nss = 0x88,
  3587. .rx_tx_mcs13_max_nss = 0x88,
  3588. },
  3589. .bw._160 = {
  3590. .rx_tx_mcs9_max_nss = 0x88,
  3591. .rx_tx_mcs11_max_nss = 0x88,
  3592. .rx_tx_mcs13_max_nss = 0x88,
  3593. },
  3594. .bw._320 = {
  3595. .rx_tx_mcs9_max_nss = 0x88,
  3596. .rx_tx_mcs11_max_nss = 0x88,
  3597. .rx_tx_mcs13_max_nss = 0x88,
  3598. },
  3599. },
  3600. /* PPE threshold information is not supported */
  3601. },
  3602. },
  3603. #ifdef CONFIG_MAC80211_MESH
  3604. {
  3605. /* TODO: should we support other types, e.g., IBSS?*/
  3606. .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
  3607. .he_6ghz_capa = {
  3608. .capa = cpu_to_le16(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START |
  3609. IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP |
  3610. IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN |
  3611. IEEE80211_HE_6GHZ_CAP_SM_PS |
  3612. IEEE80211_HE_6GHZ_CAP_RD_RESPONDER |
  3613. IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS |
  3614. IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS),
  3615. },
  3616. .he_cap = {
  3617. .has_he = true,
  3618. .he_cap_elem = {
  3619. .mac_cap_info[0] =
  3620. IEEE80211_HE_MAC_CAP0_HTC_HE,
  3621. .mac_cap_info[1] =
  3622. IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
  3623. .mac_cap_info[2] =
  3624. IEEE80211_HE_MAC_CAP2_ACK_EN,
  3625. .mac_cap_info[3] =
  3626. IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
  3627. IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
  3628. .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
  3629. .phy_cap_info[0] =
  3630. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
  3631. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
  3632. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
  3633. .phy_cap_info[1] =
  3634. IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
  3635. IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
  3636. IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
  3637. IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
  3638. .phy_cap_info[2] = 0,
  3639. /* Leave all the other PHY capability bytes
  3640. * unset, as DCM, beam forming, RU and PPE
  3641. * threshold information are not supported
  3642. */
  3643. },
  3644. .he_mcs_nss_supp = {
  3645. .rx_mcs_80 = cpu_to_le16(0xfffa),
  3646. .tx_mcs_80 = cpu_to_le16(0xfffa),
  3647. .rx_mcs_160 = cpu_to_le16(0xfffa),
  3648. .tx_mcs_160 = cpu_to_le16(0xfffa),
  3649. .rx_mcs_80p80 = cpu_to_le16(0xfffa),
  3650. .tx_mcs_80p80 = cpu_to_le16(0xfffa),
  3651. },
  3652. },
  3653. },
  3654. #endif
  3655. };
  3656. static void mac80211_hwsim_sband_capab(struct ieee80211_supported_band *sband)
  3657. {
  3658. u16 n_iftype_data;
  3659. if (sband->band == NL80211_BAND_2GHZ) {
  3660. n_iftype_data = ARRAY_SIZE(sband_capa_2ghz);
  3661. sband->iftype_data =
  3662. (struct ieee80211_sband_iftype_data *)sband_capa_2ghz;
  3663. } else if (sband->band == NL80211_BAND_5GHZ) {
  3664. n_iftype_data = ARRAY_SIZE(sband_capa_5ghz);
  3665. sband->iftype_data =
  3666. (struct ieee80211_sband_iftype_data *)sband_capa_5ghz;
  3667. } else if (sband->band == NL80211_BAND_6GHZ) {
  3668. n_iftype_data = ARRAY_SIZE(sband_capa_6ghz);
  3669. sband->iftype_data =
  3670. (struct ieee80211_sband_iftype_data *)sband_capa_6ghz;
  3671. } else {
  3672. return;
  3673. }
  3674. sband->n_iftype_data = n_iftype_data;
  3675. }
  3676. #ifdef CONFIG_MAC80211_MESH
  3677. #define HWSIM_MESH_BIT BIT(NL80211_IFTYPE_MESH_POINT)
  3678. #else
  3679. #define HWSIM_MESH_BIT 0
  3680. #endif
  3681. #define HWSIM_DEFAULT_IF_LIMIT \
  3682. (BIT(NL80211_IFTYPE_STATION) | \
  3683. BIT(NL80211_IFTYPE_P2P_CLIENT) | \
  3684. BIT(NL80211_IFTYPE_AP) | \
  3685. BIT(NL80211_IFTYPE_P2P_GO) | \
  3686. HWSIM_MESH_BIT)
  3687. #define HWSIM_IFTYPE_SUPPORT_MASK \
  3688. (BIT(NL80211_IFTYPE_STATION) | \
  3689. BIT(NL80211_IFTYPE_AP) | \
  3690. BIT(NL80211_IFTYPE_P2P_CLIENT) | \
  3691. BIT(NL80211_IFTYPE_P2P_GO) | \
  3692. BIT(NL80211_IFTYPE_ADHOC) | \
  3693. BIT(NL80211_IFTYPE_MESH_POINT) | \
  3694. BIT(NL80211_IFTYPE_OCB))
  3695. static int mac80211_hwsim_new_radio(struct genl_info *info,
  3696. struct hwsim_new_radio_params *param)
  3697. {
  3698. int err;
  3699. u8 addr[ETH_ALEN];
  3700. struct mac80211_hwsim_data *data;
  3701. struct ieee80211_hw *hw;
  3702. enum nl80211_band band;
  3703. const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
  3704. struct net *net;
  3705. int idx, i;
  3706. int n_limits = 0;
  3707. if (WARN_ON(param->channels > 1 && !param->use_chanctx))
  3708. return -EINVAL;
  3709. spin_lock_bh(&hwsim_radio_lock);
  3710. idx = hwsim_radio_idx++;
  3711. spin_unlock_bh(&hwsim_radio_lock);
  3712. if (param->mlo)
  3713. ops = &mac80211_hwsim_mlo_ops;
  3714. else if (param->use_chanctx)
  3715. ops = &mac80211_hwsim_mchan_ops;
  3716. hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
  3717. if (!hw) {
  3718. pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
  3719. err = -ENOMEM;
  3720. goto failed;
  3721. }
  3722. /* ieee80211_alloc_hw_nm may have used a default name */
  3723. param->hwname = wiphy_name(hw->wiphy);
  3724. if (info)
  3725. net = genl_info_net(info);
  3726. else
  3727. net = &init_net;
  3728. wiphy_net_set(hw->wiphy, net);
  3729. data = hw->priv;
  3730. data->hw = hw;
  3731. data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
  3732. if (IS_ERR(data->dev)) {
  3733. printk(KERN_DEBUG
  3734. "mac80211_hwsim: device_create failed (%ld)\n",
  3735. PTR_ERR(data->dev));
  3736. err = -ENOMEM;
  3737. goto failed_drvdata;
  3738. }
  3739. data->dev->driver = &mac80211_hwsim_driver.driver;
  3740. err = device_bind_driver(data->dev);
  3741. if (err != 0) {
  3742. pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
  3743. err);
  3744. goto failed_bind;
  3745. }
  3746. skb_queue_head_init(&data->pending);
  3747. SET_IEEE80211_DEV(hw, data->dev);
  3748. if (!param->perm_addr) {
  3749. eth_zero_addr(addr);
  3750. addr[0] = 0x02;
  3751. addr[3] = idx >> 8;
  3752. addr[4] = idx;
  3753. memcpy(data->addresses[0].addr, addr, ETH_ALEN);
  3754. /* Why need here second address ? */
  3755. memcpy(data->addresses[1].addr, addr, ETH_ALEN);
  3756. data->addresses[1].addr[0] |= 0x40;
  3757. hw->wiphy->n_addresses = 2;
  3758. hw->wiphy->addresses = data->addresses;
  3759. /* possible address clash is checked at hash table insertion */
  3760. } else {
  3761. memcpy(data->addresses[0].addr, param->perm_addr, ETH_ALEN);
  3762. /* compatibility with automatically generated mac addr */
  3763. memcpy(data->addresses[1].addr, param->perm_addr, ETH_ALEN);
  3764. hw->wiphy->n_addresses = 2;
  3765. hw->wiphy->addresses = data->addresses;
  3766. }
  3767. data->channels = param->channels;
  3768. data->use_chanctx = param->use_chanctx;
  3769. data->idx = idx;
  3770. data->destroy_on_close = param->destroy_on_close;
  3771. if (info)
  3772. data->portid = info->snd_portid;
  3773. /* setup interface limits, only on interface types we support */
  3774. if (param->iftypes & BIT(NL80211_IFTYPE_ADHOC)) {
  3775. data->if_limits[n_limits].max = 1;
  3776. data->if_limits[n_limits].types = BIT(NL80211_IFTYPE_ADHOC);
  3777. n_limits++;
  3778. }
  3779. if (param->iftypes & HWSIM_DEFAULT_IF_LIMIT) {
  3780. data->if_limits[n_limits].max = 2048;
  3781. /*
  3782. * For this case, we may only support a subset of
  3783. * HWSIM_DEFAULT_IF_LIMIT, therefore we only want to add the
  3784. * bits that both param->iftype & HWSIM_DEFAULT_IF_LIMIT have.
  3785. */
  3786. data->if_limits[n_limits].types =
  3787. HWSIM_DEFAULT_IF_LIMIT & param->iftypes;
  3788. n_limits++;
  3789. }
  3790. if (param->iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
  3791. data->if_limits[n_limits].max = 1;
  3792. data->if_limits[n_limits].types =
  3793. BIT(NL80211_IFTYPE_P2P_DEVICE);
  3794. n_limits++;
  3795. }
  3796. if (data->use_chanctx) {
  3797. hw->wiphy->max_scan_ssids = 255;
  3798. hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  3799. hw->wiphy->max_remain_on_channel_duration = 1000;
  3800. data->if_combination.radar_detect_widths = 0;
  3801. data->if_combination.num_different_channels = data->channels;
  3802. } else {
  3803. data->if_combination.num_different_channels = 1;
  3804. data->if_combination.radar_detect_widths =
  3805. BIT(NL80211_CHAN_WIDTH_5) |
  3806. BIT(NL80211_CHAN_WIDTH_10) |
  3807. BIT(NL80211_CHAN_WIDTH_20_NOHT) |
  3808. BIT(NL80211_CHAN_WIDTH_20) |
  3809. BIT(NL80211_CHAN_WIDTH_40) |
  3810. BIT(NL80211_CHAN_WIDTH_80) |
  3811. BIT(NL80211_CHAN_WIDTH_160);
  3812. }
  3813. if (!n_limits) {
  3814. err = -EINVAL;
  3815. goto failed_hw;
  3816. }
  3817. data->if_combination.max_interfaces = 0;
  3818. for (i = 0; i < n_limits; i++)
  3819. data->if_combination.max_interfaces +=
  3820. data->if_limits[i].max;
  3821. data->if_combination.n_limits = n_limits;
  3822. data->if_combination.limits = data->if_limits;
  3823. /*
  3824. * If we actually were asked to support combinations,
  3825. * advertise them - if there's only a single thing like
  3826. * only IBSS then don't advertise it as combinations.
  3827. */
  3828. if (data->if_combination.max_interfaces > 1) {
  3829. hw->wiphy->iface_combinations = &data->if_combination;
  3830. hw->wiphy->n_iface_combinations = 1;
  3831. }
  3832. if (param->ciphers) {
  3833. memcpy(data->ciphers, param->ciphers,
  3834. param->n_ciphers * sizeof(u32));
  3835. hw->wiphy->cipher_suites = data->ciphers;
  3836. hw->wiphy->n_cipher_suites = param->n_ciphers;
  3837. }
  3838. data->rx_rssi = DEFAULT_RX_RSSI;
  3839. INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
  3840. INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
  3841. INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
  3842. hw->queues = 5;
  3843. hw->offchannel_tx_hw_queue = 4;
  3844. ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
  3845. ieee80211_hw_set(hw, CHANCTX_STA_CSA);
  3846. ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
  3847. ieee80211_hw_set(hw, QUEUE_CONTROL);
  3848. ieee80211_hw_set(hw, WANT_MONITOR_VIF);
  3849. ieee80211_hw_set(hw, AMPDU_AGGREGATION);
  3850. ieee80211_hw_set(hw, MFP_CAPABLE);
  3851. ieee80211_hw_set(hw, SIGNAL_DBM);
  3852. ieee80211_hw_set(hw, SUPPORTS_PS);
  3853. ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
  3854. ieee80211_hw_set(hw, TDLS_WIDER_BW);
  3855. ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
  3856. if (param->mlo) {
  3857. hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
  3858. ieee80211_hw_set(hw, HAS_RATE_CONTROL);
  3859. ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
  3860. ieee80211_hw_set(hw, CONNECTION_MONITOR);
  3861. ieee80211_hw_set(hw, AP_LINK_PS);
  3862. } else {
  3863. ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
  3864. ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
  3865. if (rctbl)
  3866. ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
  3867. }
  3868. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  3869. hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
  3870. WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
  3871. WIPHY_FLAG_AP_UAPSD |
  3872. WIPHY_FLAG_SUPPORTS_5_10_MHZ |
  3873. WIPHY_FLAG_HAS_CHANNEL_SWITCH;
  3874. hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
  3875. NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
  3876. NL80211_FEATURE_STATIC_SMPS |
  3877. NL80211_FEATURE_DYNAMIC_SMPS |
  3878. NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
  3879. wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
  3880. wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
  3881. wiphy_ext_feature_set(hw->wiphy,
  3882. NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS);
  3883. wiphy_ext_feature_set(hw->wiphy,
  3884. NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
  3885. hw->wiphy->interface_modes = param->iftypes;
  3886. /* ask mac80211 to reserve space for magic */
  3887. hw->vif_data_size = sizeof(struct hwsim_vif_priv);
  3888. hw->sta_data_size = sizeof(struct hwsim_sta_priv);
  3889. hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
  3890. memcpy(data->channels_2ghz, hwsim_channels_2ghz,
  3891. sizeof(hwsim_channels_2ghz));
  3892. memcpy(data->channels_5ghz, hwsim_channels_5ghz,
  3893. sizeof(hwsim_channels_5ghz));
  3894. memcpy(data->channels_6ghz, hwsim_channels_6ghz,
  3895. sizeof(hwsim_channels_6ghz));
  3896. memcpy(data->channels_s1g, hwsim_channels_s1g,
  3897. sizeof(hwsim_channels_s1g));
  3898. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  3899. for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
  3900. struct ieee80211_supported_band *sband = &data->bands[band];
  3901. sband->band = band;
  3902. switch (band) {
  3903. case NL80211_BAND_2GHZ:
  3904. sband->channels = data->channels_2ghz;
  3905. sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
  3906. sband->bitrates = data->rates;
  3907. sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
  3908. break;
  3909. case NL80211_BAND_5GHZ:
  3910. sband->channels = data->channels_5ghz;
  3911. sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
  3912. sband->bitrates = data->rates + 4;
  3913. sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
  3914. sband->vht_cap.vht_supported = true;
  3915. sband->vht_cap.cap =
  3916. IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
  3917. IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
  3918. IEEE80211_VHT_CAP_RXLDPC |
  3919. IEEE80211_VHT_CAP_SHORT_GI_80 |
  3920. IEEE80211_VHT_CAP_SHORT_GI_160 |
  3921. IEEE80211_VHT_CAP_TXSTBC |
  3922. IEEE80211_VHT_CAP_RXSTBC_4 |
  3923. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
  3924. sband->vht_cap.vht_mcs.rx_mcs_map =
  3925. cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
  3926. IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
  3927. IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
  3928. IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
  3929. IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
  3930. IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
  3931. IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
  3932. IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
  3933. sband->vht_cap.vht_mcs.tx_mcs_map =
  3934. sband->vht_cap.vht_mcs.rx_mcs_map;
  3935. break;
  3936. case NL80211_BAND_6GHZ:
  3937. sband->channels = data->channels_6ghz;
  3938. sband->n_channels = ARRAY_SIZE(hwsim_channels_6ghz);
  3939. sband->bitrates = data->rates + 4;
  3940. sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
  3941. break;
  3942. case NL80211_BAND_S1GHZ:
  3943. memcpy(&sband->s1g_cap, &hwsim_s1g_cap,
  3944. sizeof(sband->s1g_cap));
  3945. sband->channels = data->channels_s1g;
  3946. sband->n_channels = ARRAY_SIZE(hwsim_channels_s1g);
  3947. break;
  3948. default:
  3949. continue;
  3950. }
  3951. if (band != NL80211_BAND_6GHZ){
  3952. sband->ht_cap.ht_supported = true;
  3953. sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  3954. IEEE80211_HT_CAP_GRN_FLD |
  3955. IEEE80211_HT_CAP_SGI_20 |
  3956. IEEE80211_HT_CAP_SGI_40 |
  3957. IEEE80211_HT_CAP_DSSSCCK40;
  3958. sband->ht_cap.ampdu_factor = 0x3;
  3959. sband->ht_cap.ampdu_density = 0x6;
  3960. memset(&sband->ht_cap.mcs, 0,
  3961. sizeof(sband->ht_cap.mcs));
  3962. sband->ht_cap.mcs.rx_mask[0] = 0xff;
  3963. sband->ht_cap.mcs.rx_mask[1] = 0xff;
  3964. sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  3965. }
  3966. mac80211_hwsim_sband_capab(sband);
  3967. hw->wiphy->bands[band] = sband;
  3968. }
  3969. /* By default all radios belong to the first group */
  3970. data->group = 1;
  3971. mutex_init(&data->mutex);
  3972. data->netgroup = hwsim_net_get_netgroup(net);
  3973. data->wmediumd = hwsim_net_get_wmediumd(net);
  3974. /* Enable frame retransmissions for lossy channels */
  3975. hw->max_rates = 4;
  3976. hw->max_rate_tries = 11;
  3977. hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
  3978. hw->wiphy->n_vendor_commands =
  3979. ARRAY_SIZE(mac80211_hwsim_vendor_commands);
  3980. hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
  3981. hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
  3982. if (param->reg_strict)
  3983. hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
  3984. if (param->regd) {
  3985. data->regd = param->regd;
  3986. hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
  3987. wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
  3988. /* give the regulatory workqueue a chance to run */
  3989. schedule_timeout_interruptible(1);
  3990. }
  3991. if (param->no_vif)
  3992. ieee80211_hw_set(hw, NO_AUTO_VIF);
  3993. wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
  3994. for (i = 0; i < ARRAY_SIZE(data->link_data); i++) {
  3995. hrtimer_init(&data->link_data[i].beacon_timer, CLOCK_MONOTONIC,
  3996. HRTIMER_MODE_ABS_SOFT);
  3997. data->link_data[i].beacon_timer.function =
  3998. mac80211_hwsim_beacon;
  3999. data->link_data[i].link_id = i;
  4000. }
  4001. err = ieee80211_register_hw(hw);
  4002. if (err < 0) {
  4003. pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
  4004. err);
  4005. goto failed_hw;
  4006. }
  4007. wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
  4008. if (param->reg_alpha2) {
  4009. data->alpha2[0] = param->reg_alpha2[0];
  4010. data->alpha2[1] = param->reg_alpha2[1];
  4011. regulatory_hint(hw->wiphy, param->reg_alpha2);
  4012. }
  4013. data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
  4014. debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
  4015. debugfs_create_file("group", 0666, data->debugfs, data,
  4016. &hwsim_fops_group);
  4017. debugfs_create_file("rx_rssi", 0666, data->debugfs, data,
  4018. &hwsim_fops_rx_rssi);
  4019. if (!data->use_chanctx)
  4020. debugfs_create_file("dfs_simulate_radar", 0222,
  4021. data->debugfs,
  4022. data, &hwsim_simulate_radar);
  4023. spin_lock_bh(&hwsim_radio_lock);
  4024. err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
  4025. hwsim_rht_params);
  4026. if (err < 0) {
  4027. if (info) {
  4028. GENL_SET_ERR_MSG(info, "perm addr already present");
  4029. NL_SET_BAD_ATTR(info->extack,
  4030. info->attrs[HWSIM_ATTR_PERM_ADDR]);
  4031. }
  4032. spin_unlock_bh(&hwsim_radio_lock);
  4033. goto failed_final_insert;
  4034. }
  4035. list_add_tail(&data->list, &hwsim_radios);
  4036. hwsim_radios_generation++;
  4037. spin_unlock_bh(&hwsim_radio_lock);
  4038. hwsim_mcast_new_radio(idx, info, param);
  4039. return idx;
  4040. failed_final_insert:
  4041. debugfs_remove_recursive(data->debugfs);
  4042. ieee80211_unregister_hw(data->hw);
  4043. failed_hw:
  4044. device_release_driver(data->dev);
  4045. failed_bind:
  4046. device_unregister(data->dev);
  4047. failed_drvdata:
  4048. ieee80211_free_hw(hw);
  4049. failed:
  4050. return err;
  4051. }
  4052. static void hwsim_mcast_del_radio(int id, const char *hwname,
  4053. struct genl_info *info)
  4054. {
  4055. struct sk_buff *skb;
  4056. void *data;
  4057. int ret;
  4058. skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  4059. if (!skb)
  4060. return;
  4061. data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
  4062. HWSIM_CMD_DEL_RADIO);
  4063. if (!data)
  4064. goto error;
  4065. ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
  4066. if (ret < 0)
  4067. goto error;
  4068. ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
  4069. hwname);
  4070. if (ret < 0)
  4071. goto error;
  4072. genlmsg_end(skb, data);
  4073. hwsim_mcast_config_msg(skb, info);
  4074. return;
  4075. error:
  4076. nlmsg_free(skb);
  4077. }
  4078. static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
  4079. const char *hwname,
  4080. struct genl_info *info)
  4081. {
  4082. hwsim_mcast_del_radio(data->idx, hwname, info);
  4083. debugfs_remove_recursive(data->debugfs);
  4084. ieee80211_unregister_hw(data->hw);
  4085. device_release_driver(data->dev);
  4086. device_unregister(data->dev);
  4087. ieee80211_free_hw(data->hw);
  4088. }
  4089. static int mac80211_hwsim_get_radio(struct sk_buff *skb,
  4090. struct mac80211_hwsim_data *data,
  4091. u32 portid, u32 seq,
  4092. struct netlink_callback *cb, int flags)
  4093. {
  4094. void *hdr;
  4095. struct hwsim_new_radio_params param = { };
  4096. int res = -EMSGSIZE;
  4097. hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
  4098. HWSIM_CMD_GET_RADIO);
  4099. if (!hdr)
  4100. return -EMSGSIZE;
  4101. if (cb)
  4102. genl_dump_check_consistent(cb, hdr);
  4103. if (data->alpha2[0] && data->alpha2[1])
  4104. param.reg_alpha2 = data->alpha2;
  4105. param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
  4106. REGULATORY_STRICT_REG);
  4107. param.p2p_device = !!(data->hw->wiphy->interface_modes &
  4108. BIT(NL80211_IFTYPE_P2P_DEVICE));
  4109. param.use_chanctx = data->use_chanctx;
  4110. param.regd = data->regd;
  4111. param.channels = data->channels;
  4112. param.hwname = wiphy_name(data->hw->wiphy);
  4113. res = append_radio_msg(skb, data->idx, &param);
  4114. if (res < 0)
  4115. goto out_err;
  4116. genlmsg_end(skb, hdr);
  4117. return 0;
  4118. out_err:
  4119. genlmsg_cancel(skb, hdr);
  4120. return res;
  4121. }
  4122. static void mac80211_hwsim_free(void)
  4123. {
  4124. struct mac80211_hwsim_data *data;
  4125. spin_lock_bh(&hwsim_radio_lock);
  4126. while ((data = list_first_entry_or_null(&hwsim_radios,
  4127. struct mac80211_hwsim_data,
  4128. list))) {
  4129. list_del(&data->list);
  4130. spin_unlock_bh(&hwsim_radio_lock);
  4131. mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
  4132. NULL);
  4133. spin_lock_bh(&hwsim_radio_lock);
  4134. }
  4135. spin_unlock_bh(&hwsim_radio_lock);
  4136. class_destroy(hwsim_class);
  4137. }
  4138. static const struct net_device_ops hwsim_netdev_ops = {
  4139. .ndo_start_xmit = hwsim_mon_xmit,
  4140. .ndo_set_mac_address = eth_mac_addr,
  4141. .ndo_validate_addr = eth_validate_addr,
  4142. };
  4143. static void hwsim_mon_setup(struct net_device *dev)
  4144. {
  4145. u8 addr[ETH_ALEN];
  4146. dev->netdev_ops = &hwsim_netdev_ops;
  4147. dev->needs_free_netdev = true;
  4148. ether_setup(dev);
  4149. dev->priv_flags |= IFF_NO_QUEUE;
  4150. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  4151. eth_zero_addr(addr);
  4152. addr[0] = 0x12;
  4153. eth_hw_addr_set(dev, addr);
  4154. }
  4155. static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
  4156. {
  4157. return rhashtable_lookup_fast(&hwsim_radios_rht,
  4158. addr,
  4159. hwsim_rht_params);
  4160. }
  4161. static void hwsim_register_wmediumd(struct net *net, u32 portid)
  4162. {
  4163. struct mac80211_hwsim_data *data;
  4164. hwsim_net_set_wmediumd(net, portid);
  4165. spin_lock_bh(&hwsim_radio_lock);
  4166. list_for_each_entry(data, &hwsim_radios, list) {
  4167. if (data->netgroup == hwsim_net_get_netgroup(net))
  4168. data->wmediumd = portid;
  4169. }
  4170. spin_unlock_bh(&hwsim_radio_lock);
  4171. }
  4172. static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
  4173. struct genl_info *info)
  4174. {
  4175. struct ieee80211_hdr *hdr;
  4176. struct mac80211_hwsim_data *data2;
  4177. struct ieee80211_tx_info *txi;
  4178. struct hwsim_tx_rate *tx_attempts;
  4179. u64 ret_skb_cookie;
  4180. struct sk_buff *skb, *tmp;
  4181. const u8 *src;
  4182. unsigned int hwsim_flags;
  4183. int i;
  4184. unsigned long flags;
  4185. bool found = false;
  4186. if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
  4187. !info->attrs[HWSIM_ATTR_FLAGS] ||
  4188. !info->attrs[HWSIM_ATTR_COOKIE] ||
  4189. !info->attrs[HWSIM_ATTR_SIGNAL] ||
  4190. !info->attrs[HWSIM_ATTR_TX_INFO])
  4191. goto out;
  4192. src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
  4193. hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
  4194. ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
  4195. data2 = get_hwsim_data_ref_from_addr(src);
  4196. if (!data2)
  4197. goto out;
  4198. if (!hwsim_virtio_enabled) {
  4199. if (hwsim_net_get_netgroup(genl_info_net(info)) !=
  4200. data2->netgroup)
  4201. goto out;
  4202. if (info->snd_portid != data2->wmediumd)
  4203. goto out;
  4204. }
  4205. /* look for the skb matching the cookie passed back from user */
  4206. spin_lock_irqsave(&data2->pending.lock, flags);
  4207. skb_queue_walk_safe(&data2->pending, skb, tmp) {
  4208. uintptr_t skb_cookie;
  4209. txi = IEEE80211_SKB_CB(skb);
  4210. skb_cookie = (uintptr_t)txi->rate_driver_data[0];
  4211. if (skb_cookie == ret_skb_cookie) {
  4212. __skb_unlink(skb, &data2->pending);
  4213. found = true;
  4214. break;
  4215. }
  4216. }
  4217. spin_unlock_irqrestore(&data2->pending.lock, flags);
  4218. /* not found */
  4219. if (!found)
  4220. goto out;
  4221. /* Tx info received because the frame was broadcasted on user space,
  4222. so we get all the necessary info: tx attempts and skb control buff */
  4223. tx_attempts = (struct hwsim_tx_rate *)nla_data(
  4224. info->attrs[HWSIM_ATTR_TX_INFO]);
  4225. /* now send back TX status */
  4226. txi = IEEE80211_SKB_CB(skb);
  4227. ieee80211_tx_info_clear_status(txi);
  4228. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  4229. txi->status.rates[i].idx = tx_attempts[i].idx;
  4230. txi->status.rates[i].count = tx_attempts[i].count;
  4231. }
  4232. txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
  4233. if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
  4234. (hwsim_flags & HWSIM_TX_STAT_ACK)) {
  4235. if (skb->len >= 16) {
  4236. hdr = (struct ieee80211_hdr *) skb->data;
  4237. mac80211_hwsim_monitor_ack(data2->channel,
  4238. hdr->addr2);
  4239. }
  4240. txi->flags |= IEEE80211_TX_STAT_ACK;
  4241. }
  4242. if (hwsim_flags & HWSIM_TX_CTL_NO_ACK)
  4243. txi->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
  4244. ieee80211_tx_status_irqsafe(data2->hw, skb);
  4245. return 0;
  4246. out:
  4247. return -EINVAL;
  4248. }
  4249. static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
  4250. struct genl_info *info)
  4251. {
  4252. struct mac80211_hwsim_data *data2;
  4253. struct ieee80211_rx_status rx_status;
  4254. struct ieee80211_hdr *hdr;
  4255. const u8 *dst;
  4256. int frame_data_len;
  4257. void *frame_data;
  4258. struct sk_buff *skb = NULL;
  4259. struct ieee80211_channel *channel = NULL;
  4260. if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
  4261. !info->attrs[HWSIM_ATTR_FRAME] ||
  4262. !info->attrs[HWSIM_ATTR_RX_RATE] ||
  4263. !info->attrs[HWSIM_ATTR_SIGNAL])
  4264. goto out;
  4265. dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
  4266. frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
  4267. frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
  4268. if (frame_data_len < sizeof(struct ieee80211_hdr_3addr) ||
  4269. frame_data_len > IEEE80211_MAX_DATA_LEN)
  4270. goto err;
  4271. /* Allocate new skb here */
  4272. skb = alloc_skb(frame_data_len, GFP_KERNEL);
  4273. if (skb == NULL)
  4274. goto err;
  4275. /* Copy the data */
  4276. skb_put_data(skb, frame_data, frame_data_len);
  4277. data2 = get_hwsim_data_ref_from_addr(dst);
  4278. if (!data2)
  4279. goto out;
  4280. if (data2->use_chanctx) {
  4281. if (data2->tmp_chan)
  4282. channel = data2->tmp_chan;
  4283. } else {
  4284. channel = data2->channel;
  4285. }
  4286. if (!hwsim_virtio_enabled) {
  4287. if (hwsim_net_get_netgroup(genl_info_net(info)) !=
  4288. data2->netgroup)
  4289. goto out;
  4290. if (info->snd_portid != data2->wmediumd)
  4291. goto out;
  4292. }
  4293. /* check if radio is configured properly */
  4294. if ((data2->idle && !data2->tmp_chan) || !data2->started)
  4295. goto out;
  4296. /* A frame is received from user space */
  4297. memset(&rx_status, 0, sizeof(rx_status));
  4298. if (info->attrs[HWSIM_ATTR_FREQ]) {
  4299. struct tx_iter_data iter_data = {};
  4300. /* throw away off-channel packets, but allow both the temporary
  4301. * ("hw" scan/remain-on-channel), regular channels and links,
  4302. * since the internal datapath also allows this
  4303. */
  4304. rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
  4305. iter_data.channel = ieee80211_get_channel(data2->hw->wiphy,
  4306. rx_status.freq);
  4307. if (!iter_data.channel)
  4308. goto out;
  4309. rx_status.band = iter_data.channel->band;
  4310. mutex_lock(&data2->mutex);
  4311. if (!hwsim_chans_compat(iter_data.channel, channel)) {
  4312. ieee80211_iterate_active_interfaces_atomic(
  4313. data2->hw, IEEE80211_IFACE_ITER_NORMAL,
  4314. mac80211_hwsim_tx_iter, &iter_data);
  4315. if (!iter_data.receive) {
  4316. mutex_unlock(&data2->mutex);
  4317. goto out;
  4318. }
  4319. }
  4320. mutex_unlock(&data2->mutex);
  4321. } else if (!channel) {
  4322. goto out;
  4323. } else {
  4324. rx_status.freq = channel->center_freq;
  4325. rx_status.band = channel->band;
  4326. }
  4327. rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
  4328. if (rx_status.rate_idx >= data2->hw->wiphy->bands[rx_status.band]->n_bitrates)
  4329. goto out;
  4330. rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
  4331. hdr = (void *)skb->data;
  4332. if (ieee80211_is_beacon(hdr->frame_control) ||
  4333. ieee80211_is_probe_resp(hdr->frame_control))
  4334. rx_status.boottime_ns = ktime_get_boottime_ns();
  4335. mac80211_hwsim_rx(data2, &rx_status, skb);
  4336. return 0;
  4337. err:
  4338. pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
  4339. out:
  4340. dev_kfree_skb(skb);
  4341. return -EINVAL;
  4342. }
  4343. static int hwsim_register_received_nl(struct sk_buff *skb_2,
  4344. struct genl_info *info)
  4345. {
  4346. struct net *net = genl_info_net(info);
  4347. struct mac80211_hwsim_data *data;
  4348. int chans = 1;
  4349. spin_lock_bh(&hwsim_radio_lock);
  4350. list_for_each_entry(data, &hwsim_radios, list)
  4351. chans = max(chans, data->channels);
  4352. spin_unlock_bh(&hwsim_radio_lock);
  4353. /* In the future we should revise the userspace API and allow it
  4354. * to set a flag that it does support multi-channel, then we can
  4355. * let this pass conditionally on the flag.
  4356. * For current userspace, prohibit it since it won't work right.
  4357. */
  4358. if (chans > 1)
  4359. return -EOPNOTSUPP;
  4360. if (hwsim_net_get_wmediumd(net))
  4361. return -EBUSY;
  4362. hwsim_register_wmediumd(net, info->snd_portid);
  4363. pr_debug("mac80211_hwsim: received a REGISTER, "
  4364. "switching to wmediumd mode with pid %d\n", info->snd_portid);
  4365. return 0;
  4366. }
  4367. /* ensures ciphers only include ciphers listed in 'hwsim_ciphers' array */
  4368. static bool hwsim_known_ciphers(const u32 *ciphers, int n_ciphers)
  4369. {
  4370. int i;
  4371. for (i = 0; i < n_ciphers; i++) {
  4372. int j;
  4373. int found = 0;
  4374. for (j = 0; j < ARRAY_SIZE(hwsim_ciphers); j++) {
  4375. if (ciphers[i] == hwsim_ciphers[j]) {
  4376. found = 1;
  4377. break;
  4378. }
  4379. }
  4380. if (!found)
  4381. return false;
  4382. }
  4383. return true;
  4384. }
  4385. static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
  4386. {
  4387. struct hwsim_new_radio_params param = { 0 };
  4388. const char *hwname = NULL;
  4389. int ret;
  4390. param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
  4391. param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
  4392. param.channels = channels;
  4393. param.destroy_on_close =
  4394. info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
  4395. if (info->attrs[HWSIM_ATTR_CHANNELS])
  4396. param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
  4397. if (param.channels < 1) {
  4398. GENL_SET_ERR_MSG(info, "must have at least one channel");
  4399. return -EINVAL;
  4400. }
  4401. if (info->attrs[HWSIM_ATTR_NO_VIF])
  4402. param.no_vif = true;
  4403. if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
  4404. param.use_chanctx = true;
  4405. else
  4406. param.use_chanctx = (param.channels > 1);
  4407. if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
  4408. param.reg_alpha2 =
  4409. nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
  4410. if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
  4411. u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
  4412. if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom))
  4413. return -EINVAL;
  4414. idx = array_index_nospec(idx,
  4415. ARRAY_SIZE(hwsim_world_regdom_custom));
  4416. param.regd = hwsim_world_regdom_custom[idx];
  4417. }
  4418. if (info->attrs[HWSIM_ATTR_PERM_ADDR]) {
  4419. if (!is_valid_ether_addr(
  4420. nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]))) {
  4421. GENL_SET_ERR_MSG(info,"MAC is no valid source addr");
  4422. NL_SET_BAD_ATTR(info->extack,
  4423. info->attrs[HWSIM_ATTR_PERM_ADDR]);
  4424. return -EINVAL;
  4425. }
  4426. param.perm_addr = nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]);
  4427. }
  4428. if (info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]) {
  4429. param.iftypes =
  4430. nla_get_u32(info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]);
  4431. if (param.iftypes & ~HWSIM_IFTYPE_SUPPORT_MASK) {
  4432. NL_SET_ERR_MSG_ATTR(info->extack,
  4433. info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT],
  4434. "cannot support more iftypes than kernel");
  4435. return -EINVAL;
  4436. }
  4437. } else {
  4438. param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
  4439. }
  4440. /* ensure both flag and iftype support is honored */
  4441. if (param.p2p_device ||
  4442. param.iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
  4443. param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
  4444. param.p2p_device = true;
  4445. }
  4446. if (info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]) {
  4447. u32 len = nla_len(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
  4448. param.ciphers =
  4449. nla_data(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
  4450. if (len % sizeof(u32)) {
  4451. NL_SET_ERR_MSG_ATTR(info->extack,
  4452. info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
  4453. "bad cipher list length");
  4454. return -EINVAL;
  4455. }
  4456. param.n_ciphers = len / sizeof(u32);
  4457. if (param.n_ciphers > ARRAY_SIZE(hwsim_ciphers)) {
  4458. NL_SET_ERR_MSG_ATTR(info->extack,
  4459. info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
  4460. "too many ciphers specified");
  4461. return -EINVAL;
  4462. }
  4463. if (!hwsim_known_ciphers(param.ciphers, param.n_ciphers)) {
  4464. NL_SET_ERR_MSG_ATTR(info->extack,
  4465. info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
  4466. "unsupported ciphers specified");
  4467. return -EINVAL;
  4468. }
  4469. }
  4470. param.mlo = info->attrs[HWSIM_ATTR_MLO_SUPPORT];
  4471. if (param.mlo)
  4472. param.use_chanctx = true;
  4473. if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
  4474. hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
  4475. nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
  4476. GFP_KERNEL);
  4477. if (!hwname)
  4478. return -ENOMEM;
  4479. param.hwname = hwname;
  4480. }
  4481. ret = mac80211_hwsim_new_radio(info, &param);
  4482. kfree(hwname);
  4483. return ret;
  4484. }
  4485. static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
  4486. {
  4487. struct mac80211_hwsim_data *data;
  4488. s64 idx = -1;
  4489. const char *hwname = NULL;
  4490. if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
  4491. idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
  4492. } else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
  4493. hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
  4494. nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
  4495. GFP_KERNEL);
  4496. if (!hwname)
  4497. return -ENOMEM;
  4498. } else
  4499. return -EINVAL;
  4500. spin_lock_bh(&hwsim_radio_lock);
  4501. list_for_each_entry(data, &hwsim_radios, list) {
  4502. if (idx >= 0) {
  4503. if (data->idx != idx)
  4504. continue;
  4505. } else {
  4506. if (!hwname ||
  4507. strcmp(hwname, wiphy_name(data->hw->wiphy)))
  4508. continue;
  4509. }
  4510. if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
  4511. continue;
  4512. list_del(&data->list);
  4513. rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
  4514. hwsim_rht_params);
  4515. hwsim_radios_generation++;
  4516. spin_unlock_bh(&hwsim_radio_lock);
  4517. mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
  4518. info);
  4519. kfree(hwname);
  4520. return 0;
  4521. }
  4522. spin_unlock_bh(&hwsim_radio_lock);
  4523. kfree(hwname);
  4524. return -ENODEV;
  4525. }
  4526. static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
  4527. {
  4528. struct mac80211_hwsim_data *data;
  4529. struct sk_buff *skb;
  4530. int idx, res = -ENODEV;
  4531. if (!info->attrs[HWSIM_ATTR_RADIO_ID])
  4532. return -EINVAL;
  4533. idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
  4534. spin_lock_bh(&hwsim_radio_lock);
  4535. list_for_each_entry(data, &hwsim_radios, list) {
  4536. if (data->idx != idx)
  4537. continue;
  4538. if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
  4539. continue;
  4540. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  4541. if (!skb) {
  4542. res = -ENOMEM;
  4543. goto out_err;
  4544. }
  4545. res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
  4546. info->snd_seq, NULL, 0);
  4547. if (res < 0) {
  4548. nlmsg_free(skb);
  4549. goto out_err;
  4550. }
  4551. res = genlmsg_reply(skb, info);
  4552. break;
  4553. }
  4554. out_err:
  4555. spin_unlock_bh(&hwsim_radio_lock);
  4556. return res;
  4557. }
  4558. static int hwsim_dump_radio_nl(struct sk_buff *skb,
  4559. struct netlink_callback *cb)
  4560. {
  4561. int last_idx = cb->args[0] - 1;
  4562. struct mac80211_hwsim_data *data = NULL;
  4563. int res = 0;
  4564. void *hdr;
  4565. spin_lock_bh(&hwsim_radio_lock);
  4566. cb->seq = hwsim_radios_generation;
  4567. if (last_idx >= hwsim_radio_idx-1)
  4568. goto done;
  4569. list_for_each_entry(data, &hwsim_radios, list) {
  4570. if (data->idx <= last_idx)
  4571. continue;
  4572. if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
  4573. continue;
  4574. res = mac80211_hwsim_get_radio(skb, data,
  4575. NETLINK_CB(cb->skb).portid,
  4576. cb->nlh->nlmsg_seq, cb,
  4577. NLM_F_MULTI);
  4578. if (res < 0)
  4579. break;
  4580. last_idx = data->idx;
  4581. }
  4582. cb->args[0] = last_idx + 1;
  4583. /* list changed, but no new element sent, set interrupted flag */
  4584. if (skb->len == 0 && cb->prev_seq && cb->seq != cb->prev_seq) {
  4585. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
  4586. cb->nlh->nlmsg_seq, &hwsim_genl_family,
  4587. NLM_F_MULTI, HWSIM_CMD_GET_RADIO);
  4588. if (hdr) {
  4589. genl_dump_check_consistent(cb, hdr);
  4590. genlmsg_end(skb, hdr);
  4591. } else {
  4592. res = -EMSGSIZE;
  4593. }
  4594. }
  4595. done:
  4596. spin_unlock_bh(&hwsim_radio_lock);
  4597. return res ?: skb->len;
  4598. }
  4599. /* Generic Netlink operations array */
  4600. static const struct genl_small_ops hwsim_ops[] = {
  4601. {
  4602. .cmd = HWSIM_CMD_REGISTER,
  4603. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  4604. .doit = hwsim_register_received_nl,
  4605. .flags = GENL_UNS_ADMIN_PERM,
  4606. },
  4607. {
  4608. .cmd = HWSIM_CMD_FRAME,
  4609. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  4610. .doit = hwsim_cloned_frame_received_nl,
  4611. },
  4612. {
  4613. .cmd = HWSIM_CMD_TX_INFO_FRAME,
  4614. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  4615. .doit = hwsim_tx_info_frame_received_nl,
  4616. },
  4617. {
  4618. .cmd = HWSIM_CMD_NEW_RADIO,
  4619. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  4620. .doit = hwsim_new_radio_nl,
  4621. .flags = GENL_UNS_ADMIN_PERM,
  4622. },
  4623. {
  4624. .cmd = HWSIM_CMD_DEL_RADIO,
  4625. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  4626. .doit = hwsim_del_radio_nl,
  4627. .flags = GENL_UNS_ADMIN_PERM,
  4628. },
  4629. {
  4630. .cmd = HWSIM_CMD_GET_RADIO,
  4631. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  4632. .doit = hwsim_get_radio_nl,
  4633. .dumpit = hwsim_dump_radio_nl,
  4634. },
  4635. };
  4636. static struct genl_family hwsim_genl_family __ro_after_init = {
  4637. .name = "MAC80211_HWSIM",
  4638. .version = 1,
  4639. .maxattr = HWSIM_ATTR_MAX,
  4640. .policy = hwsim_genl_policy,
  4641. .netnsok = true,
  4642. .module = THIS_MODULE,
  4643. .small_ops = hwsim_ops,
  4644. .n_small_ops = ARRAY_SIZE(hwsim_ops),
  4645. .resv_start_op = HWSIM_CMD_DEL_MAC_ADDR + 1,
  4646. .mcgrps = hwsim_mcgrps,
  4647. .n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
  4648. };
  4649. static void remove_user_radios(u32 portid)
  4650. {
  4651. struct mac80211_hwsim_data *entry, *tmp;
  4652. LIST_HEAD(list);
  4653. spin_lock_bh(&hwsim_radio_lock);
  4654. list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
  4655. if (entry->destroy_on_close && entry->portid == portid) {
  4656. list_move(&entry->list, &list);
  4657. rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
  4658. hwsim_rht_params);
  4659. hwsim_radios_generation++;
  4660. }
  4661. }
  4662. spin_unlock_bh(&hwsim_radio_lock);
  4663. list_for_each_entry_safe(entry, tmp, &list, list) {
  4664. list_del(&entry->list);
  4665. mac80211_hwsim_del_radio(entry, wiphy_name(entry->hw->wiphy),
  4666. NULL);
  4667. }
  4668. }
  4669. static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
  4670. unsigned long state,
  4671. void *_notify)
  4672. {
  4673. struct netlink_notify *notify = _notify;
  4674. if (state != NETLINK_URELEASE)
  4675. return NOTIFY_DONE;
  4676. remove_user_radios(notify->portid);
  4677. if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
  4678. printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
  4679. " socket, switching to perfect channel medium\n");
  4680. hwsim_register_wmediumd(notify->net, 0);
  4681. }
  4682. return NOTIFY_DONE;
  4683. }
  4684. static struct notifier_block hwsim_netlink_notifier = {
  4685. .notifier_call = mac80211_hwsim_netlink_notify,
  4686. };
  4687. static int __init hwsim_init_netlink(void)
  4688. {
  4689. int rc;
  4690. printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
  4691. rc = genl_register_family(&hwsim_genl_family);
  4692. if (rc)
  4693. goto failure;
  4694. rc = netlink_register_notifier(&hwsim_netlink_notifier);
  4695. if (rc) {
  4696. genl_unregister_family(&hwsim_genl_family);
  4697. goto failure;
  4698. }
  4699. return 0;
  4700. failure:
  4701. pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
  4702. return -EINVAL;
  4703. }
  4704. static __net_init int hwsim_init_net(struct net *net)
  4705. {
  4706. return hwsim_net_set_netgroup(net);
  4707. }
  4708. static void __net_exit hwsim_exit_net(struct net *net)
  4709. {
  4710. struct mac80211_hwsim_data *data, *tmp;
  4711. LIST_HEAD(list);
  4712. spin_lock_bh(&hwsim_radio_lock);
  4713. list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
  4714. if (!net_eq(wiphy_net(data->hw->wiphy), net))
  4715. continue;
  4716. /* Radios created in init_net are returned to init_net. */
  4717. if (data->netgroup == hwsim_net_get_netgroup(&init_net))
  4718. continue;
  4719. list_move(&data->list, &list);
  4720. rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
  4721. hwsim_rht_params);
  4722. hwsim_radios_generation++;
  4723. }
  4724. spin_unlock_bh(&hwsim_radio_lock);
  4725. list_for_each_entry_safe(data, tmp, &list, list) {
  4726. list_del(&data->list);
  4727. mac80211_hwsim_del_radio(data,
  4728. wiphy_name(data->hw->wiphy),
  4729. NULL);
  4730. }
  4731. ida_free(&hwsim_netgroup_ida, hwsim_net_get_netgroup(net));
  4732. }
  4733. static struct pernet_operations hwsim_net_ops = {
  4734. .init = hwsim_init_net,
  4735. .exit = hwsim_exit_net,
  4736. .id = &hwsim_net_id,
  4737. .size = sizeof(struct hwsim_net),
  4738. };
  4739. static void hwsim_exit_netlink(void)
  4740. {
  4741. /* unregister the notifier */
  4742. netlink_unregister_notifier(&hwsim_netlink_notifier);
  4743. /* unregister the family */
  4744. genl_unregister_family(&hwsim_genl_family);
  4745. }
  4746. #if IS_REACHABLE(CONFIG_VIRTIO)
  4747. static void hwsim_virtio_tx_done(struct virtqueue *vq)
  4748. {
  4749. unsigned int len;
  4750. struct sk_buff *skb;
  4751. unsigned long flags;
  4752. spin_lock_irqsave(&hwsim_virtio_lock, flags);
  4753. while ((skb = virtqueue_get_buf(vq, &len)))
  4754. nlmsg_free(skb);
  4755. spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
  4756. }
  4757. static int hwsim_virtio_handle_cmd(struct sk_buff *skb)
  4758. {
  4759. struct nlmsghdr *nlh;
  4760. struct genlmsghdr *gnlh;
  4761. struct nlattr *tb[HWSIM_ATTR_MAX + 1];
  4762. struct genl_info info = {};
  4763. int err;
  4764. nlh = nlmsg_hdr(skb);
  4765. gnlh = nlmsg_data(nlh);
  4766. if (skb->len < nlh->nlmsg_len)
  4767. return -EINVAL;
  4768. err = genlmsg_parse(nlh, &hwsim_genl_family, tb, HWSIM_ATTR_MAX,
  4769. hwsim_genl_policy, NULL);
  4770. if (err) {
  4771. pr_err_ratelimited("hwsim: genlmsg_parse returned %d\n", err);
  4772. return err;
  4773. }
  4774. info.attrs = tb;
  4775. switch (gnlh->cmd) {
  4776. case HWSIM_CMD_FRAME:
  4777. hwsim_cloned_frame_received_nl(skb, &info);
  4778. break;
  4779. case HWSIM_CMD_TX_INFO_FRAME:
  4780. hwsim_tx_info_frame_received_nl(skb, &info);
  4781. break;
  4782. default:
  4783. pr_err_ratelimited("hwsim: invalid cmd: %d\n", gnlh->cmd);
  4784. return -EPROTO;
  4785. }
  4786. return 0;
  4787. }
  4788. static void hwsim_virtio_rx_work(struct work_struct *work)
  4789. {
  4790. struct virtqueue *vq;
  4791. unsigned int len;
  4792. struct sk_buff *skb;
  4793. struct scatterlist sg[1];
  4794. int err;
  4795. unsigned long flags;
  4796. spin_lock_irqsave(&hwsim_virtio_lock, flags);
  4797. if (!hwsim_virtio_enabled)
  4798. goto out_unlock;
  4799. skb = virtqueue_get_buf(hwsim_vqs[HWSIM_VQ_RX], &len);
  4800. if (!skb)
  4801. goto out_unlock;
  4802. spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
  4803. skb->data = skb->head;
  4804. skb_reset_tail_pointer(skb);
  4805. skb_put(skb, len);
  4806. hwsim_virtio_handle_cmd(skb);
  4807. spin_lock_irqsave(&hwsim_virtio_lock, flags);
  4808. if (!hwsim_virtio_enabled) {
  4809. nlmsg_free(skb);
  4810. goto out_unlock;
  4811. }
  4812. vq = hwsim_vqs[HWSIM_VQ_RX];
  4813. sg_init_one(sg, skb->head, skb_end_offset(skb));
  4814. err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_ATOMIC);
  4815. if (WARN(err, "virtqueue_add_inbuf returned %d\n", err))
  4816. nlmsg_free(skb);
  4817. else
  4818. virtqueue_kick(vq);
  4819. schedule_work(&hwsim_virtio_rx);
  4820. out_unlock:
  4821. spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
  4822. }
  4823. static void hwsim_virtio_rx_done(struct virtqueue *vq)
  4824. {
  4825. schedule_work(&hwsim_virtio_rx);
  4826. }
  4827. static int init_vqs(struct virtio_device *vdev)
  4828. {
  4829. vq_callback_t *callbacks[HWSIM_NUM_VQS] = {
  4830. [HWSIM_VQ_TX] = hwsim_virtio_tx_done,
  4831. [HWSIM_VQ_RX] = hwsim_virtio_rx_done,
  4832. };
  4833. const char *names[HWSIM_NUM_VQS] = {
  4834. [HWSIM_VQ_TX] = "tx",
  4835. [HWSIM_VQ_RX] = "rx",
  4836. };
  4837. return virtio_find_vqs(vdev, HWSIM_NUM_VQS,
  4838. hwsim_vqs, callbacks, names, NULL);
  4839. }
  4840. static int fill_vq(struct virtqueue *vq)
  4841. {
  4842. int i, err;
  4843. struct sk_buff *skb;
  4844. struct scatterlist sg[1];
  4845. for (i = 0; i < virtqueue_get_vring_size(vq); i++) {
  4846. skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  4847. if (!skb)
  4848. return -ENOMEM;
  4849. sg_init_one(sg, skb->head, skb_end_offset(skb));
  4850. err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_KERNEL);
  4851. if (err) {
  4852. nlmsg_free(skb);
  4853. return err;
  4854. }
  4855. }
  4856. virtqueue_kick(vq);
  4857. return 0;
  4858. }
  4859. static void remove_vqs(struct virtio_device *vdev)
  4860. {
  4861. int i;
  4862. virtio_reset_device(vdev);
  4863. for (i = 0; i < ARRAY_SIZE(hwsim_vqs); i++) {
  4864. struct virtqueue *vq = hwsim_vqs[i];
  4865. struct sk_buff *skb;
  4866. while ((skb = virtqueue_detach_unused_buf(vq)))
  4867. nlmsg_free(skb);
  4868. }
  4869. vdev->config->del_vqs(vdev);
  4870. }
  4871. static int hwsim_virtio_probe(struct virtio_device *vdev)
  4872. {
  4873. int err;
  4874. unsigned long flags;
  4875. spin_lock_irqsave(&hwsim_virtio_lock, flags);
  4876. if (hwsim_virtio_enabled) {
  4877. spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
  4878. return -EEXIST;
  4879. }
  4880. spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
  4881. err = init_vqs(vdev);
  4882. if (err)
  4883. return err;
  4884. virtio_device_ready(vdev);
  4885. err = fill_vq(hwsim_vqs[HWSIM_VQ_RX]);
  4886. if (err)
  4887. goto out_remove;
  4888. spin_lock_irqsave(&hwsim_virtio_lock, flags);
  4889. hwsim_virtio_enabled = true;
  4890. spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
  4891. schedule_work(&hwsim_virtio_rx);
  4892. return 0;
  4893. out_remove:
  4894. remove_vqs(vdev);
  4895. return err;
  4896. }
  4897. static void hwsim_virtio_remove(struct virtio_device *vdev)
  4898. {
  4899. hwsim_virtio_enabled = false;
  4900. cancel_work_sync(&hwsim_virtio_rx);
  4901. remove_vqs(vdev);
  4902. }
  4903. /* MAC80211_HWSIM virtio device id table */
  4904. static const struct virtio_device_id id_table[] = {
  4905. { VIRTIO_ID_MAC80211_HWSIM, VIRTIO_DEV_ANY_ID },
  4906. { 0 }
  4907. };
  4908. MODULE_DEVICE_TABLE(virtio, id_table);
  4909. static struct virtio_driver virtio_hwsim = {
  4910. .driver.name = KBUILD_MODNAME,
  4911. .driver.owner = THIS_MODULE,
  4912. .id_table = id_table,
  4913. .probe = hwsim_virtio_probe,
  4914. .remove = hwsim_virtio_remove,
  4915. };
  4916. static int hwsim_register_virtio_driver(void)
  4917. {
  4918. return register_virtio_driver(&virtio_hwsim);
  4919. }
  4920. static void hwsim_unregister_virtio_driver(void)
  4921. {
  4922. unregister_virtio_driver(&virtio_hwsim);
  4923. }
  4924. #else
  4925. static inline int hwsim_register_virtio_driver(void)
  4926. {
  4927. return 0;
  4928. }
  4929. static inline void hwsim_unregister_virtio_driver(void)
  4930. {
  4931. }
  4932. #endif
  4933. static int __init init_mac80211_hwsim(void)
  4934. {
  4935. int i, err;
  4936. if (radios < 0 || radios > 100)
  4937. return -EINVAL;
  4938. if (channels < 1)
  4939. return -EINVAL;
  4940. err = rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
  4941. if (err)
  4942. return err;
  4943. err = register_pernet_device(&hwsim_net_ops);
  4944. if (err)
  4945. goto out_free_rht;
  4946. err = platform_driver_register(&mac80211_hwsim_driver);
  4947. if (err)
  4948. goto out_unregister_pernet;
  4949. err = hwsim_init_netlink();
  4950. if (err)
  4951. goto out_unregister_driver;
  4952. err = hwsim_register_virtio_driver();
  4953. if (err)
  4954. goto out_exit_netlink;
  4955. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  4956. if (IS_ERR(hwsim_class)) {
  4957. err = PTR_ERR(hwsim_class);
  4958. goto out_exit_virtio;
  4959. }
  4960. hwsim_init_s1g_channels(hwsim_channels_s1g);
  4961. for (i = 0; i < radios; i++) {
  4962. struct hwsim_new_radio_params param = { 0 };
  4963. param.channels = channels;
  4964. switch (regtest) {
  4965. case HWSIM_REGTEST_DIFF_COUNTRY:
  4966. if (i < ARRAY_SIZE(hwsim_alpha2s))
  4967. param.reg_alpha2 = hwsim_alpha2s[i];
  4968. break;
  4969. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  4970. if (!i)
  4971. param.reg_alpha2 = hwsim_alpha2s[0];
  4972. break;
  4973. case HWSIM_REGTEST_STRICT_ALL:
  4974. param.reg_strict = true;
  4975. fallthrough;
  4976. case HWSIM_REGTEST_DRIVER_REG_ALL:
  4977. param.reg_alpha2 = hwsim_alpha2s[0];
  4978. break;
  4979. case HWSIM_REGTEST_WORLD_ROAM:
  4980. if (i == 0)
  4981. param.regd = &hwsim_world_regdom_custom_01;
  4982. break;
  4983. case HWSIM_REGTEST_CUSTOM_WORLD:
  4984. param.regd = &hwsim_world_regdom_custom_01;
  4985. break;
  4986. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  4987. if (i == 0)
  4988. param.regd = &hwsim_world_regdom_custom_01;
  4989. else if (i == 1)
  4990. param.regd = &hwsim_world_regdom_custom_02;
  4991. break;
  4992. case HWSIM_REGTEST_STRICT_FOLLOW:
  4993. if (i == 0) {
  4994. param.reg_strict = true;
  4995. param.reg_alpha2 = hwsim_alpha2s[0];
  4996. }
  4997. break;
  4998. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  4999. if (i == 0) {
  5000. param.reg_strict = true;
  5001. param.reg_alpha2 = hwsim_alpha2s[0];
  5002. } else if (i == 1) {
  5003. param.reg_alpha2 = hwsim_alpha2s[1];
  5004. }
  5005. break;
  5006. case HWSIM_REGTEST_ALL:
  5007. switch (i) {
  5008. case 0:
  5009. param.regd = &hwsim_world_regdom_custom_01;
  5010. break;
  5011. case 1:
  5012. param.regd = &hwsim_world_regdom_custom_02;
  5013. break;
  5014. case 2:
  5015. param.reg_alpha2 = hwsim_alpha2s[0];
  5016. break;
  5017. case 3:
  5018. param.reg_alpha2 = hwsim_alpha2s[1];
  5019. break;
  5020. case 4:
  5021. param.reg_strict = true;
  5022. param.reg_alpha2 = hwsim_alpha2s[2];
  5023. break;
  5024. }
  5025. break;
  5026. default:
  5027. break;
  5028. }
  5029. param.p2p_device = support_p2p_device;
  5030. param.mlo = mlo;
  5031. param.use_chanctx = channels > 1 || mlo;
  5032. param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
  5033. if (param.p2p_device)
  5034. param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
  5035. err = mac80211_hwsim_new_radio(NULL, &param);
  5036. if (err < 0)
  5037. goto out_free_radios;
  5038. }
  5039. hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
  5040. hwsim_mon_setup);
  5041. if (hwsim_mon == NULL) {
  5042. err = -ENOMEM;
  5043. goto out_free_radios;
  5044. }
  5045. rtnl_lock();
  5046. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  5047. if (err < 0) {
  5048. rtnl_unlock();
  5049. goto out_free_mon;
  5050. }
  5051. err = register_netdevice(hwsim_mon);
  5052. if (err < 0) {
  5053. rtnl_unlock();
  5054. goto out_free_mon;
  5055. }
  5056. rtnl_unlock();
  5057. return 0;
  5058. out_free_mon:
  5059. free_netdev(hwsim_mon);
  5060. out_free_radios:
  5061. mac80211_hwsim_free();
  5062. out_exit_virtio:
  5063. hwsim_unregister_virtio_driver();
  5064. out_exit_netlink:
  5065. hwsim_exit_netlink();
  5066. out_unregister_driver:
  5067. platform_driver_unregister(&mac80211_hwsim_driver);
  5068. out_unregister_pernet:
  5069. unregister_pernet_device(&hwsim_net_ops);
  5070. out_free_rht:
  5071. rhashtable_destroy(&hwsim_radios_rht);
  5072. return err;
  5073. }
  5074. module_init(init_mac80211_hwsim);
  5075. static void __exit exit_mac80211_hwsim(void)
  5076. {
  5077. pr_debug("mac80211_hwsim: unregister radios\n");
  5078. hwsim_unregister_virtio_driver();
  5079. hwsim_exit_netlink();
  5080. mac80211_hwsim_free();
  5081. rhashtable_destroy(&hwsim_radios_rht);
  5082. unregister_netdev(hwsim_mon);
  5083. platform_driver_unregister(&mac80211_hwsim_driver);
  5084. unregister_pernet_device(&hwsim_net_ops);
  5085. }
  5086. module_exit(exit_mac80211_hwsim);