driver_nl80211.c 275 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288
  1. /*
  2. * Driver interaction with Linux nl80211/cfg80211
  3. * Copyright (c) 2002-2015, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2003-2004, Instant802 Networks, Inc.
  5. * Copyright (c) 2005-2006, Devicescape Software, Inc.
  6. * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
  7. * Copyright (c) 2009-2010, Atheros Communications
  8. *
  9. * This software may be distributed under the terms of the BSD license.
  10. * See README for more details.
  11. */
  12. #include "includes.h"
  13. #include <sys/types.h>
  14. #include <fcntl.h>
  15. #include <net/if.h>
  16. #include <netlink/genl/genl.h>
  17. #include <netlink/genl/ctrl.h>
  18. #ifdef CONFIG_LIBNL3_ROUTE
  19. #include <netlink/route/neighbour.h>
  20. #endif /* CONFIG_LIBNL3_ROUTE */
  21. #include <linux/rtnetlink.h>
  22. #include <netpacket/packet.h>
  23. #include <linux/errqueue.h>
  24. #include "common.h"
  25. #include "eloop.h"
  26. #include "common/qca-vendor.h"
  27. #include "common/qca-vendor-attr.h"
  28. #include "common/ieee802_11_defs.h"
  29. #include "common/ieee802_11_common.h"
  30. #include "common/wpa_common.h"
  31. #include "l2_packet/l2_packet.h"
  32. #include "netlink.h"
  33. #include "linux_defines.h"
  34. #include "linux_ioctl.h"
  35. #include "radiotap.h"
  36. #include "radiotap_iter.h"
  37. #include "rfkill.h"
  38. #include "driver_nl80211.h"
  39. #ifndef CONFIG_LIBNL20
  40. /*
  41. * libnl 1.1 has a bug, it tries to allocate socket numbers densely
  42. * but when you free a socket again it will mess up its bitmap and
  43. * and use the wrong number the next time it needs a socket ID.
  44. * Therefore, we wrap the handle alloc/destroy and add our own pid
  45. * accounting.
  46. */
  47. static uint32_t port_bitmap[32] = { 0 };
  48. static struct nl_handle *nl80211_handle_alloc(void *cb)
  49. {
  50. struct nl_handle *handle;
  51. uint32_t pid = getpid() & 0x3FFFFF;
  52. int i;
  53. handle = nl_handle_alloc_cb(cb);
  54. for (i = 0; i < 1024; i++) {
  55. if (port_bitmap[i / 32] & (1 << (i % 32)))
  56. continue;
  57. port_bitmap[i / 32] |= 1 << (i % 32);
  58. pid += i << 22;
  59. break;
  60. }
  61. nl_socket_set_local_port(handle, pid);
  62. return handle;
  63. }
  64. static void nl80211_handle_destroy(struct nl_handle *handle)
  65. {
  66. uint32_t port = nl_socket_get_local_port(handle);
  67. port >>= 22;
  68. port_bitmap[port / 32] &= ~(1 << (port % 32));
  69. nl_handle_destroy(handle);
  70. }
  71. #endif /* CONFIG_LIBNL20 */
  72. #ifdef ANDROID
  73. /* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
  74. #undef nl_socket_set_nonblocking
  75. #define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
  76. #endif /* ANDROID */
  77. static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
  78. {
  79. struct nl_handle *handle;
  80. handle = nl80211_handle_alloc(cb);
  81. if (handle == NULL) {
  82. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  83. "callbacks (%s)", dbg);
  84. return NULL;
  85. }
  86. if (genl_connect(handle)) {
  87. wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
  88. "netlink (%s)", dbg);
  89. nl80211_handle_destroy(handle);
  90. return NULL;
  91. }
  92. return handle;
  93. }
  94. static void nl_destroy_handles(struct nl_handle **handle)
  95. {
  96. if (*handle == NULL)
  97. return;
  98. nl80211_handle_destroy(*handle);
  99. *handle = NULL;
  100. }
  101. #if __WORDSIZE == 64
  102. #define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
  103. #else
  104. #define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
  105. #endif
  106. static void nl80211_register_eloop_read(struct nl_handle **handle,
  107. eloop_sock_handler handler,
  108. void *eloop_data)
  109. {
  110. #ifdef CONFIG_LIBNL20
  111. /*
  112. * libnl uses a pretty small buffer (32 kB that gets converted to 64 kB)
  113. * by default. It is possible to hit that limit in some cases where
  114. * operations are blocked, e.g., with a burst of Deauthentication frames
  115. * to hostapd and STA entry deletion. Try to increase the buffer to make
  116. * this less likely to occur.
  117. */
  118. if (nl_socket_set_buffer_size(*handle, 262144, 0) < 0) {
  119. wpa_printf(MSG_DEBUG,
  120. "nl80211: Could not set nl_socket RX buffer size: %s",
  121. strerror(errno));
  122. /* continue anyway with the default (smaller) buffer */
  123. }
  124. #endif /* CONFIG_LIBNL20 */
  125. nl_socket_set_nonblocking(*handle);
  126. eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
  127. eloop_data, *handle);
  128. *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
  129. }
  130. static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
  131. {
  132. *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
  133. eloop_unregister_read_sock(nl_socket_get_fd(*handle));
  134. nl_destroy_handles(handle);
  135. }
  136. static void nl80211_global_deinit(void *priv);
  137. static void nl80211_check_global(struct nl80211_global *global);
  138. static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
  139. static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
  140. struct hostapd_freq_params *freq);
  141. static int
  142. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
  143. const u8 *set_addr, int first,
  144. const char *driver_params);
  145. static int nl80211_send_frame_cmd(struct i802_bss *bss,
  146. unsigned int freq, unsigned int wait,
  147. const u8 *buf, size_t buf_len, u64 *cookie,
  148. int no_cck, int no_ack, int offchanok,
  149. const u16 *csa_offs, size_t csa_offs_len);
  150. static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
  151. int report);
  152. #define IFIDX_ANY -1
  153. static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
  154. int ifidx_reason);
  155. static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
  156. int ifidx_reason);
  157. static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
  158. int ifidx_reason);
  159. static int nl80211_set_channel(struct i802_bss *bss,
  160. struct hostapd_freq_params *freq, int set_chan);
  161. static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
  162. int ifindex, int disabled);
  163. static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
  164. int reset_mode);
  165. static int i802_set_iface_flags(struct i802_bss *bss, int up);
  166. static int nl80211_set_param(void *priv, const char *param);
  167. #ifdef CONFIG_MESH
  168. static int nl80211_put_mesh_config(struct nl_msg *msg,
  169. struct wpa_driver_mesh_bss_params *params);
  170. #endif /* CONFIG_MESH */
  171. static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
  172. int reason);
  173. /* Converts nl80211_chan_width to a common format */
  174. enum chan_width convert2width(int width)
  175. {
  176. switch (width) {
  177. case NL80211_CHAN_WIDTH_20_NOHT:
  178. return CHAN_WIDTH_20_NOHT;
  179. case NL80211_CHAN_WIDTH_20:
  180. return CHAN_WIDTH_20;
  181. case NL80211_CHAN_WIDTH_40:
  182. return CHAN_WIDTH_40;
  183. case NL80211_CHAN_WIDTH_80:
  184. return CHAN_WIDTH_80;
  185. case NL80211_CHAN_WIDTH_80P80:
  186. return CHAN_WIDTH_80P80;
  187. case NL80211_CHAN_WIDTH_160:
  188. return CHAN_WIDTH_160;
  189. }
  190. return CHAN_WIDTH_UNKNOWN;
  191. }
  192. int is_ap_interface(enum nl80211_iftype nlmode)
  193. {
  194. return nlmode == NL80211_IFTYPE_AP ||
  195. nlmode == NL80211_IFTYPE_P2P_GO;
  196. }
  197. int is_sta_interface(enum nl80211_iftype nlmode)
  198. {
  199. return nlmode == NL80211_IFTYPE_STATION ||
  200. nlmode == NL80211_IFTYPE_P2P_CLIENT;
  201. }
  202. static int is_p2p_net_interface(enum nl80211_iftype nlmode)
  203. {
  204. return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
  205. nlmode == NL80211_IFTYPE_P2P_GO;
  206. }
  207. struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
  208. int ifindex)
  209. {
  210. struct i802_bss *bss;
  211. for (bss = drv->first_bss; bss; bss = bss->next) {
  212. if (bss->ifindex == ifindex)
  213. return bss;
  214. }
  215. return NULL;
  216. }
  217. static int is_mesh_interface(enum nl80211_iftype nlmode)
  218. {
  219. return nlmode == NL80211_IFTYPE_MESH_POINT;
  220. }
  221. void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
  222. {
  223. if (drv->associated)
  224. os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
  225. drv->associated = 0;
  226. os_memset(drv->bssid, 0, ETH_ALEN);
  227. }
  228. /* nl80211 code */
  229. static int ack_handler(struct nl_msg *msg, void *arg)
  230. {
  231. int *err = arg;
  232. *err = 0;
  233. return NL_STOP;
  234. }
  235. static int finish_handler(struct nl_msg *msg, void *arg)
  236. {
  237. int *ret = arg;
  238. *ret = 0;
  239. return NL_SKIP;
  240. }
  241. static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
  242. void *arg)
  243. {
  244. int *ret = arg;
  245. *ret = err->error;
  246. return NL_SKIP;
  247. }
  248. static int no_seq_check(struct nl_msg *msg, void *arg)
  249. {
  250. return NL_OK;
  251. }
  252. static void nl80211_nlmsg_clear(struct nl_msg *msg)
  253. {
  254. /*
  255. * Clear nlmsg data, e.g., to make sure key material is not left in
  256. * heap memory for unnecessarily long time.
  257. */
  258. if (msg) {
  259. struct nlmsghdr *hdr = nlmsg_hdr(msg);
  260. void *data = nlmsg_data(hdr);
  261. /*
  262. * This would use nlmsg_datalen() or the older nlmsg_len() if
  263. * only libnl were to maintain a stable API.. Neither will work
  264. * with all released versions, so just calculate the length
  265. * here.
  266. */
  267. int len = hdr->nlmsg_len - NLMSG_HDRLEN;
  268. os_memset(data, 0, len);
  269. }
  270. }
  271. static int send_and_recv(struct nl80211_global *global,
  272. struct nl_handle *nl_handle, struct nl_msg *msg,
  273. int (*valid_handler)(struct nl_msg *, void *),
  274. void *valid_data)
  275. {
  276. struct nl_cb *cb;
  277. int err = -ENOMEM;
  278. if (!msg)
  279. return -ENOMEM;
  280. cb = nl_cb_clone(global->nl_cb);
  281. if (!cb)
  282. goto out;
  283. err = nl_send_auto_complete(nl_handle, msg);
  284. if (err < 0)
  285. goto out;
  286. err = 1;
  287. nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
  288. nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
  289. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
  290. if (valid_handler)
  291. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
  292. valid_handler, valid_data);
  293. while (err > 0) {
  294. int res = nl_recvmsgs(nl_handle, cb);
  295. if (res < 0) {
  296. wpa_printf(MSG_INFO,
  297. "nl80211: %s->nl_recvmsgs failed: %d",
  298. __func__, res);
  299. }
  300. }
  301. out:
  302. nl_cb_put(cb);
  303. if (!valid_handler && valid_data == (void *) -1)
  304. nl80211_nlmsg_clear(msg);
  305. nlmsg_free(msg);
  306. return err;
  307. }
  308. int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
  309. struct nl_msg *msg,
  310. int (*valid_handler)(struct nl_msg *, void *),
  311. void *valid_data)
  312. {
  313. return send_and_recv(drv->global, drv->global->nl, msg,
  314. valid_handler, valid_data);
  315. }
  316. struct family_data {
  317. const char *group;
  318. int id;
  319. };
  320. static int family_handler(struct nl_msg *msg, void *arg)
  321. {
  322. struct family_data *res = arg;
  323. struct nlattr *tb[CTRL_ATTR_MAX + 1];
  324. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  325. struct nlattr *mcgrp;
  326. int i;
  327. nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  328. genlmsg_attrlen(gnlh, 0), NULL);
  329. if (!tb[CTRL_ATTR_MCAST_GROUPS])
  330. return NL_SKIP;
  331. nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
  332. struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
  333. nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
  334. nla_len(mcgrp), NULL);
  335. if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
  336. !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
  337. os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
  338. res->group,
  339. nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
  340. continue;
  341. res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
  342. break;
  343. };
  344. return NL_SKIP;
  345. }
  346. static int nl_get_multicast_id(struct nl80211_global *global,
  347. const char *family, const char *group)
  348. {
  349. struct nl_msg *msg;
  350. int ret;
  351. struct family_data res = { group, -ENOENT };
  352. msg = nlmsg_alloc();
  353. if (!msg)
  354. return -ENOMEM;
  355. if (!genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
  356. 0, 0, CTRL_CMD_GETFAMILY, 0) ||
  357. nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family)) {
  358. nlmsg_free(msg);
  359. return -1;
  360. }
  361. ret = send_and_recv(global, global->nl, msg, family_handler, &res);
  362. if (ret == 0)
  363. ret = res.id;
  364. return ret;
  365. }
  366. void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
  367. struct nl_msg *msg, int flags, uint8_t cmd)
  368. {
  369. if (TEST_FAIL())
  370. return NULL;
  371. return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
  372. 0, flags, cmd, 0);
  373. }
  374. static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
  375. {
  376. if (bss->wdev_id_set)
  377. return nla_put_u64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
  378. return nla_put_u32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  379. }
  380. struct nl_msg * nl80211_cmd_msg(struct i802_bss *bss, int flags, uint8_t cmd)
  381. {
  382. struct nl_msg *msg;
  383. msg = nlmsg_alloc();
  384. if (!msg)
  385. return NULL;
  386. if (!nl80211_cmd(bss->drv, msg, flags, cmd) ||
  387. nl80211_set_iface_id(msg, bss) < 0) {
  388. nlmsg_free(msg);
  389. return NULL;
  390. }
  391. return msg;
  392. }
  393. static struct nl_msg *
  394. nl80211_ifindex_msg(struct wpa_driver_nl80211_data *drv, int ifindex,
  395. int flags, uint8_t cmd)
  396. {
  397. struct nl_msg *msg;
  398. msg = nlmsg_alloc();
  399. if (!msg)
  400. return NULL;
  401. if (!nl80211_cmd(drv, msg, flags, cmd) ||
  402. nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex)) {
  403. nlmsg_free(msg);
  404. return NULL;
  405. }
  406. return msg;
  407. }
  408. struct nl_msg * nl80211_drv_msg(struct wpa_driver_nl80211_data *drv, int flags,
  409. uint8_t cmd)
  410. {
  411. return nl80211_ifindex_msg(drv, drv->ifindex, flags, cmd);
  412. }
  413. struct nl_msg * nl80211_bss_msg(struct i802_bss *bss, int flags, uint8_t cmd)
  414. {
  415. return nl80211_ifindex_msg(bss->drv, bss->ifindex, flags, cmd);
  416. }
  417. struct wiphy_idx_data {
  418. int wiphy_idx;
  419. enum nl80211_iftype nlmode;
  420. u8 *macaddr;
  421. };
  422. static int netdev_info_handler(struct nl_msg *msg, void *arg)
  423. {
  424. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  425. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  426. struct wiphy_idx_data *info = arg;
  427. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  428. genlmsg_attrlen(gnlh, 0), NULL);
  429. if (tb[NL80211_ATTR_WIPHY])
  430. info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
  431. if (tb[NL80211_ATTR_IFTYPE])
  432. info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
  433. if (tb[NL80211_ATTR_MAC] && info->macaddr)
  434. os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
  435. ETH_ALEN);
  436. return NL_SKIP;
  437. }
  438. int nl80211_get_wiphy_index(struct i802_bss *bss)
  439. {
  440. struct nl_msg *msg;
  441. struct wiphy_idx_data data = {
  442. .wiphy_idx = -1,
  443. .macaddr = NULL,
  444. };
  445. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
  446. return -1;
  447. if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
  448. return data.wiphy_idx;
  449. return -1;
  450. }
  451. static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
  452. {
  453. struct nl_msg *msg;
  454. struct wiphy_idx_data data = {
  455. .nlmode = NL80211_IFTYPE_UNSPECIFIED,
  456. .macaddr = NULL,
  457. };
  458. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
  459. return NL80211_IFTYPE_UNSPECIFIED;
  460. if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
  461. return data.nlmode;
  462. return NL80211_IFTYPE_UNSPECIFIED;
  463. }
  464. static int nl80211_get_macaddr(struct i802_bss *bss)
  465. {
  466. struct nl_msg *msg;
  467. struct wiphy_idx_data data = {
  468. .macaddr = bss->addr,
  469. };
  470. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
  471. return -1;
  472. return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
  473. }
  474. static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
  475. struct nl80211_wiphy_data *w)
  476. {
  477. struct nl_msg *msg;
  478. int ret;
  479. msg = nlmsg_alloc();
  480. if (!msg)
  481. return -1;
  482. if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS) ||
  483. nla_put_u32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx)) {
  484. nlmsg_free(msg);
  485. return -1;
  486. }
  487. ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
  488. if (ret) {
  489. wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
  490. "failed: ret=%d (%s)",
  491. ret, strerror(-ret));
  492. }
  493. return ret;
  494. }
  495. static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
  496. {
  497. struct nl80211_wiphy_data *w = eloop_ctx;
  498. int res;
  499. wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
  500. res = nl_recvmsgs(handle, w->nl_cb);
  501. if (res < 0) {
  502. wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
  503. __func__, res);
  504. }
  505. }
  506. static int process_beacon_event(struct nl_msg *msg, void *arg)
  507. {
  508. struct nl80211_wiphy_data *w = arg;
  509. struct wpa_driver_nl80211_data *drv;
  510. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  511. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  512. union wpa_event_data event;
  513. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  514. genlmsg_attrlen(gnlh, 0), NULL);
  515. if (gnlh->cmd != NL80211_CMD_FRAME) {
  516. wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
  517. gnlh->cmd);
  518. return NL_SKIP;
  519. }
  520. if (!tb[NL80211_ATTR_FRAME])
  521. return NL_SKIP;
  522. dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
  523. wiphy_list) {
  524. os_memset(&event, 0, sizeof(event));
  525. event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
  526. event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
  527. wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
  528. }
  529. return NL_SKIP;
  530. }
  531. static struct nl80211_wiphy_data *
  532. nl80211_get_wiphy_data_ap(struct i802_bss *bss)
  533. {
  534. static DEFINE_DL_LIST(nl80211_wiphys);
  535. struct nl80211_wiphy_data *w;
  536. int wiphy_idx, found = 0;
  537. struct i802_bss *tmp_bss;
  538. u8 channel;
  539. if (bss->wiphy_data != NULL)
  540. return bss->wiphy_data;
  541. wiphy_idx = nl80211_get_wiphy_index(bss);
  542. dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
  543. if (w->wiphy_idx == wiphy_idx)
  544. goto add;
  545. }
  546. /* alloc new one */
  547. w = os_zalloc(sizeof(*w));
  548. if (w == NULL)
  549. return NULL;
  550. w->wiphy_idx = wiphy_idx;
  551. dl_list_init(&w->bsss);
  552. dl_list_init(&w->drvs);
  553. /* Beacon frames not supported in IEEE 802.11ad */
  554. if (ieee80211_freq_to_chan(bss->freq, &channel) !=
  555. HOSTAPD_MODE_IEEE80211AD) {
  556. w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  557. if (!w->nl_cb) {
  558. os_free(w);
  559. return NULL;
  560. }
  561. nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
  562. no_seq_check, NULL);
  563. nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
  564. process_beacon_event, w);
  565. w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
  566. "wiphy beacons");
  567. if (w->nl_beacons == NULL) {
  568. os_free(w);
  569. return NULL;
  570. }
  571. if (nl80211_register_beacons(bss->drv, w)) {
  572. nl_destroy_handles(&w->nl_beacons);
  573. os_free(w);
  574. return NULL;
  575. }
  576. nl80211_register_eloop_read(&w->nl_beacons,
  577. nl80211_recv_beacons, w);
  578. }
  579. dl_list_add(&nl80211_wiphys, &w->list);
  580. add:
  581. /* drv entry for this bss already there? */
  582. dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
  583. if (tmp_bss->drv == bss->drv) {
  584. found = 1;
  585. break;
  586. }
  587. }
  588. /* if not add it */
  589. if (!found)
  590. dl_list_add(&w->drvs, &bss->drv->wiphy_list);
  591. dl_list_add(&w->bsss, &bss->wiphy_list);
  592. bss->wiphy_data = w;
  593. return w;
  594. }
  595. static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
  596. {
  597. struct nl80211_wiphy_data *w = bss->wiphy_data;
  598. struct i802_bss *tmp_bss;
  599. int found = 0;
  600. if (w == NULL)
  601. return;
  602. bss->wiphy_data = NULL;
  603. dl_list_del(&bss->wiphy_list);
  604. /* still any for this drv present? */
  605. dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
  606. if (tmp_bss->drv == bss->drv) {
  607. found = 1;
  608. break;
  609. }
  610. }
  611. /* if not remove it */
  612. if (!found)
  613. dl_list_del(&bss->drv->wiphy_list);
  614. if (!dl_list_empty(&w->bsss))
  615. return;
  616. if (w->nl_beacons)
  617. nl80211_destroy_eloop_handle(&w->nl_beacons);
  618. nl_cb_put(w->nl_cb);
  619. dl_list_del(&w->list);
  620. os_free(w);
  621. }
  622. static unsigned int nl80211_get_ifindex(void *priv)
  623. {
  624. struct i802_bss *bss = priv;
  625. struct wpa_driver_nl80211_data *drv = bss->drv;
  626. return drv->ifindex;
  627. }
  628. static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
  629. {
  630. struct i802_bss *bss = priv;
  631. struct wpa_driver_nl80211_data *drv = bss->drv;
  632. if (!drv->associated)
  633. return -1;
  634. os_memcpy(bssid, drv->bssid, ETH_ALEN);
  635. return 0;
  636. }
  637. static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
  638. {
  639. struct i802_bss *bss = priv;
  640. struct wpa_driver_nl80211_data *drv = bss->drv;
  641. if (!drv->associated)
  642. return -1;
  643. os_memcpy(ssid, drv->ssid, drv->ssid_len);
  644. return drv->ssid_len;
  645. }
  646. static void wpa_driver_nl80211_event_newlink(
  647. struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
  648. int ifindex, const char *ifname)
  649. {
  650. union wpa_event_data event;
  651. if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
  652. if (if_nametoindex(drv->first_bss->ifname) == 0) {
  653. wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
  654. drv->first_bss->ifname);
  655. return;
  656. }
  657. if (!drv->if_removed)
  658. return;
  659. wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
  660. drv->first_bss->ifname);
  661. drv->if_removed = 0;
  662. }
  663. os_memset(&event, 0, sizeof(event));
  664. event.interface_status.ifindex = ifindex;
  665. os_strlcpy(event.interface_status.ifname, ifname,
  666. sizeof(event.interface_status.ifname));
  667. event.interface_status.ievent = EVENT_INTERFACE_ADDED;
  668. if (drv)
  669. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
  670. else
  671. wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
  672. &event);
  673. }
  674. static void wpa_driver_nl80211_event_dellink(
  675. struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
  676. int ifindex, const char *ifname)
  677. {
  678. union wpa_event_data event;
  679. if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
  680. if (drv->if_removed) {
  681. wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
  682. ifname);
  683. return;
  684. }
  685. wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
  686. ifname);
  687. drv->if_removed = 1;
  688. } else {
  689. wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
  690. ifname);
  691. }
  692. os_memset(&event, 0, sizeof(event));
  693. event.interface_status.ifindex = ifindex;
  694. os_strlcpy(event.interface_status.ifname, ifname,
  695. sizeof(event.interface_status.ifname));
  696. event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
  697. if (drv)
  698. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
  699. else
  700. wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
  701. &event);
  702. }
  703. static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
  704. u8 *buf, size_t len)
  705. {
  706. int attrlen, rta_len;
  707. struct rtattr *attr;
  708. attrlen = len;
  709. attr = (struct rtattr *) buf;
  710. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  711. while (RTA_OK(attr, attrlen)) {
  712. if (attr->rta_type == IFLA_IFNAME) {
  713. if (os_strcmp(((char *) attr) + rta_len,
  714. drv->first_bss->ifname) == 0)
  715. return 1;
  716. else
  717. break;
  718. }
  719. attr = RTA_NEXT(attr, attrlen);
  720. }
  721. return 0;
  722. }
  723. static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
  724. int ifindex, u8 *buf, size_t len)
  725. {
  726. if (drv->ifindex == ifindex)
  727. return 1;
  728. if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
  729. nl80211_check_global(drv->global);
  730. wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
  731. "interface");
  732. if (wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL) < 0)
  733. return -1;
  734. return 1;
  735. }
  736. return 0;
  737. }
  738. static struct wpa_driver_nl80211_data *
  739. nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len,
  740. int *init_failed)
  741. {
  742. struct wpa_driver_nl80211_data *drv;
  743. int res;
  744. if (init_failed)
  745. *init_failed = 0;
  746. dl_list_for_each(drv, &global->interfaces,
  747. struct wpa_driver_nl80211_data, list) {
  748. res = wpa_driver_nl80211_own_ifindex(drv, idx, buf, len);
  749. if (res < 0) {
  750. wpa_printf(MSG_DEBUG,
  751. "nl80211: Found matching own interface, but failed to complete reinitialization");
  752. if (init_failed)
  753. *init_failed = 1;
  754. return drv;
  755. }
  756. if (res > 0 || have_ifidx(drv, idx, IFIDX_ANY))
  757. return drv;
  758. }
  759. return NULL;
  760. }
  761. static void nl80211_refresh_mac(struct wpa_driver_nl80211_data *drv,
  762. int ifindex)
  763. {
  764. struct i802_bss *bss;
  765. u8 addr[ETH_ALEN];
  766. bss = get_bss_ifindex(drv, ifindex);
  767. if (bss &&
  768. linux_get_ifhwaddr(drv->global->ioctl_sock,
  769. bss->ifname, addr) < 0) {
  770. wpa_printf(MSG_DEBUG,
  771. "nl80211: %s: failed to re-read MAC address",
  772. bss->ifname);
  773. } else if (bss && os_memcmp(addr, bss->addr, ETH_ALEN) != 0) {
  774. wpa_printf(MSG_DEBUG,
  775. "nl80211: Own MAC address on ifindex %d (%s) changed from "
  776. MACSTR " to " MACSTR,
  777. ifindex, bss->ifname,
  778. MAC2STR(bss->addr), MAC2STR(addr));
  779. os_memcpy(bss->addr, addr, ETH_ALEN);
  780. }
  781. }
  782. static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
  783. struct ifinfomsg *ifi,
  784. u8 *buf, size_t len)
  785. {
  786. struct nl80211_global *global = ctx;
  787. struct wpa_driver_nl80211_data *drv;
  788. int attrlen;
  789. struct rtattr *attr;
  790. u32 brid = 0;
  791. char namebuf[IFNAMSIZ];
  792. char ifname[IFNAMSIZ + 1];
  793. char extra[100], *pos, *end;
  794. int init_failed;
  795. extra[0] = '\0';
  796. pos = extra;
  797. end = pos + sizeof(extra);
  798. ifname[0] = '\0';
  799. attrlen = len;
  800. attr = (struct rtattr *) buf;
  801. while (RTA_OK(attr, attrlen)) {
  802. switch (attr->rta_type) {
  803. case IFLA_IFNAME:
  804. if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
  805. break;
  806. os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
  807. ifname[RTA_PAYLOAD(attr)] = '\0';
  808. break;
  809. case IFLA_MASTER:
  810. brid = nla_get_u32((struct nlattr *) attr);
  811. pos += os_snprintf(pos, end - pos, " master=%u", brid);
  812. break;
  813. case IFLA_WIRELESS:
  814. pos += os_snprintf(pos, end - pos, " wext");
  815. break;
  816. case IFLA_OPERSTATE:
  817. pos += os_snprintf(pos, end - pos, " operstate=%u",
  818. nla_get_u32((struct nlattr *) attr));
  819. break;
  820. case IFLA_LINKMODE:
  821. pos += os_snprintf(pos, end - pos, " linkmode=%u",
  822. nla_get_u32((struct nlattr *) attr));
  823. break;
  824. }
  825. attr = RTA_NEXT(attr, attrlen);
  826. }
  827. extra[sizeof(extra) - 1] = '\0';
  828. wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
  829. ifi->ifi_index, ifname, extra, ifi->ifi_family,
  830. ifi->ifi_flags,
  831. (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
  832. (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
  833. (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
  834. (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
  835. drv = nl80211_find_drv(global, ifi->ifi_index, buf, len, &init_failed);
  836. if (!drv)
  837. goto event_newlink;
  838. if (init_failed)
  839. return; /* do not update interface state */
  840. if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
  841. namebuf[0] = '\0';
  842. if (if_indextoname(ifi->ifi_index, namebuf) &&
  843. linux_iface_up(drv->global->ioctl_sock, namebuf) > 0) {
  844. /* Re-read MAC address as it may have changed */
  845. nl80211_refresh_mac(drv, ifi->ifi_index);
  846. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
  847. "event since interface %s is up", namebuf);
  848. drv->ignore_if_down_event = 0;
  849. return;
  850. }
  851. wpa_printf(MSG_DEBUG, "nl80211: Interface down (%s/%s)",
  852. namebuf, ifname);
  853. if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
  854. wpa_printf(MSG_DEBUG,
  855. "nl80211: Not the main interface (%s) - do not indicate interface down",
  856. drv->first_bss->ifname);
  857. } else if (drv->ignore_if_down_event) {
  858. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
  859. "event generated by mode change");
  860. drv->ignore_if_down_event = 0;
  861. } else {
  862. drv->if_disabled = 1;
  863. wpa_supplicant_event(drv->ctx,
  864. EVENT_INTERFACE_DISABLED, NULL);
  865. /*
  866. * Try to get drv again, since it may be removed as
  867. * part of the EVENT_INTERFACE_DISABLED handling for
  868. * dynamic interfaces
  869. */
  870. drv = nl80211_find_drv(global, ifi->ifi_index,
  871. buf, len, NULL);
  872. if (!drv)
  873. return;
  874. }
  875. }
  876. if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
  877. if (if_indextoname(ifi->ifi_index, namebuf) &&
  878. linux_iface_up(drv->global->ioctl_sock, namebuf) == 0) {
  879. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
  880. "event since interface %s is down",
  881. namebuf);
  882. } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
  883. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
  884. "event since interface %s does not exist",
  885. drv->first_bss->ifname);
  886. } else if (drv->if_removed) {
  887. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
  888. "event since interface %s is marked "
  889. "removed", drv->first_bss->ifname);
  890. } else {
  891. /* Re-read MAC address as it may have changed */
  892. nl80211_refresh_mac(drv, ifi->ifi_index);
  893. wpa_printf(MSG_DEBUG, "nl80211: Interface up");
  894. drv->if_disabled = 0;
  895. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
  896. NULL);
  897. }
  898. }
  899. /*
  900. * Some drivers send the association event before the operup event--in
  901. * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
  902. * fails. This will hit us when wpa_supplicant does not need to do
  903. * IEEE 802.1X authentication
  904. */
  905. if (drv->operstate == 1 &&
  906. (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
  907. !(ifi->ifi_flags & IFF_RUNNING)) {
  908. wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
  909. netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
  910. -1, IF_OPER_UP);
  911. }
  912. event_newlink:
  913. if (ifname[0])
  914. wpa_driver_nl80211_event_newlink(global, drv, ifi->ifi_index,
  915. ifname);
  916. if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
  917. struct i802_bss *bss;
  918. /* device has been added to bridge */
  919. if (!if_indextoname(brid, namebuf)) {
  920. wpa_printf(MSG_DEBUG,
  921. "nl80211: Could not find bridge ifname for ifindex %u",
  922. brid);
  923. return;
  924. }
  925. wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
  926. brid, namebuf);
  927. add_ifidx(drv, brid, ifi->ifi_index);
  928. for (bss = drv->first_bss; bss; bss = bss->next) {
  929. if (os_strcmp(ifname, bss->ifname) == 0) {
  930. os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
  931. break;
  932. }
  933. }
  934. }
  935. }
  936. static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
  937. struct ifinfomsg *ifi,
  938. u8 *buf, size_t len)
  939. {
  940. struct nl80211_global *global = ctx;
  941. struct wpa_driver_nl80211_data *drv;
  942. int attrlen;
  943. struct rtattr *attr;
  944. u32 brid = 0;
  945. char ifname[IFNAMSIZ + 1];
  946. char extra[100], *pos, *end;
  947. extra[0] = '\0';
  948. pos = extra;
  949. end = pos + sizeof(extra);
  950. ifname[0] = '\0';
  951. attrlen = len;
  952. attr = (struct rtattr *) buf;
  953. while (RTA_OK(attr, attrlen)) {
  954. switch (attr->rta_type) {
  955. case IFLA_IFNAME:
  956. if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
  957. break;
  958. os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
  959. ifname[RTA_PAYLOAD(attr)] = '\0';
  960. break;
  961. case IFLA_MASTER:
  962. brid = nla_get_u32((struct nlattr *) attr);
  963. pos += os_snprintf(pos, end - pos, " master=%u", brid);
  964. break;
  965. case IFLA_OPERSTATE:
  966. pos += os_snprintf(pos, end - pos, " operstate=%u",
  967. nla_get_u32((struct nlattr *) attr));
  968. break;
  969. case IFLA_LINKMODE:
  970. pos += os_snprintf(pos, end - pos, " linkmode=%u",
  971. nla_get_u32((struct nlattr *) attr));
  972. break;
  973. }
  974. attr = RTA_NEXT(attr, attrlen);
  975. }
  976. extra[sizeof(extra) - 1] = '\0';
  977. wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
  978. ifi->ifi_index, ifname, extra, ifi->ifi_family,
  979. ifi->ifi_flags,
  980. (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
  981. (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
  982. (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
  983. (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
  984. drv = nl80211_find_drv(global, ifi->ifi_index, buf, len, NULL);
  985. if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
  986. /* device has been removed from bridge */
  987. char namebuf[IFNAMSIZ];
  988. if (!if_indextoname(brid, namebuf)) {
  989. wpa_printf(MSG_DEBUG,
  990. "nl80211: Could not find bridge ifname for ifindex %u",
  991. brid);
  992. } else {
  993. wpa_printf(MSG_DEBUG,
  994. "nl80211: Remove ifindex %u for bridge %s",
  995. brid, namebuf);
  996. }
  997. del_ifidx(drv, brid, ifi->ifi_index);
  998. }
  999. if (ifi->ifi_family != AF_BRIDGE || !brid)
  1000. wpa_driver_nl80211_event_dellink(global, drv, ifi->ifi_index,
  1001. ifname);
  1002. }
  1003. struct nl80211_get_assoc_freq_arg {
  1004. struct wpa_driver_nl80211_data *drv;
  1005. unsigned int assoc_freq;
  1006. unsigned int ibss_freq;
  1007. u8 assoc_bssid[ETH_ALEN];
  1008. u8 assoc_ssid[SSID_MAX_LEN];
  1009. u8 assoc_ssid_len;
  1010. };
  1011. static int nl80211_get_assoc_freq_handler(struct nl_msg *msg, void *arg)
  1012. {
  1013. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1014. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1015. struct nlattr *bss[NL80211_BSS_MAX + 1];
  1016. static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
  1017. [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
  1018. [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
  1019. [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
  1020. [NL80211_BSS_STATUS] = { .type = NLA_U32 },
  1021. };
  1022. struct nl80211_get_assoc_freq_arg *ctx = arg;
  1023. enum nl80211_bss_status status;
  1024. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1025. genlmsg_attrlen(gnlh, 0), NULL);
  1026. if (!tb[NL80211_ATTR_BSS] ||
  1027. nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
  1028. bss_policy) ||
  1029. !bss[NL80211_BSS_STATUS])
  1030. return NL_SKIP;
  1031. status = nla_get_u32(bss[NL80211_BSS_STATUS]);
  1032. if (status == NL80211_BSS_STATUS_ASSOCIATED &&
  1033. bss[NL80211_BSS_FREQUENCY]) {
  1034. ctx->assoc_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
  1035. wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
  1036. ctx->assoc_freq);
  1037. }
  1038. if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
  1039. bss[NL80211_BSS_FREQUENCY]) {
  1040. ctx->ibss_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
  1041. wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
  1042. ctx->ibss_freq);
  1043. }
  1044. if (status == NL80211_BSS_STATUS_ASSOCIATED &&
  1045. bss[NL80211_BSS_BSSID]) {
  1046. os_memcpy(ctx->assoc_bssid,
  1047. nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
  1048. wpa_printf(MSG_DEBUG, "nl80211: Associated with "
  1049. MACSTR, MAC2STR(ctx->assoc_bssid));
  1050. }
  1051. if (status == NL80211_BSS_STATUS_ASSOCIATED &&
  1052. bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
  1053. const u8 *ie, *ssid;
  1054. size_t ie_len;
  1055. ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  1056. ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  1057. ssid = get_ie(ie, ie_len, WLAN_EID_SSID);
  1058. if (ssid && ssid[1] > 0 && ssid[1] <= SSID_MAX_LEN) {
  1059. ctx->assoc_ssid_len = ssid[1];
  1060. os_memcpy(ctx->assoc_ssid, ssid + 2, ssid[1]);
  1061. }
  1062. }
  1063. return NL_SKIP;
  1064. }
  1065. int nl80211_get_assoc_ssid(struct wpa_driver_nl80211_data *drv, u8 *ssid)
  1066. {
  1067. struct nl_msg *msg;
  1068. int ret;
  1069. struct nl80211_get_assoc_freq_arg arg;
  1070. msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
  1071. os_memset(&arg, 0, sizeof(arg));
  1072. arg.drv = drv;
  1073. ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
  1074. &arg);
  1075. if (ret == 0) {
  1076. os_memcpy(ssid, arg.assoc_ssid, arg.assoc_ssid_len);
  1077. return arg.assoc_ssid_len;
  1078. }
  1079. wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d (%s)",
  1080. ret, strerror(-ret));
  1081. return ret;
  1082. }
  1083. unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
  1084. {
  1085. struct nl_msg *msg;
  1086. int ret;
  1087. struct nl80211_get_assoc_freq_arg arg;
  1088. msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
  1089. os_memset(&arg, 0, sizeof(arg));
  1090. arg.drv = drv;
  1091. ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
  1092. &arg);
  1093. if (ret == 0) {
  1094. unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
  1095. arg.ibss_freq : arg.assoc_freq;
  1096. wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
  1097. "associated BSS from scan results: %u MHz", freq);
  1098. if (freq)
  1099. drv->assoc_freq = freq;
  1100. return drv->assoc_freq;
  1101. }
  1102. wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
  1103. "(%s)", ret, strerror(-ret));
  1104. return drv->assoc_freq;
  1105. }
  1106. static int get_link_signal(struct nl_msg *msg, void *arg)
  1107. {
  1108. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1109. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1110. struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
  1111. static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
  1112. [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
  1113. [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
  1114. [NL80211_STA_INFO_BEACON_SIGNAL_AVG] = { .type = NLA_U8 },
  1115. };
  1116. struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
  1117. static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
  1118. [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
  1119. [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
  1120. [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
  1121. [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
  1122. };
  1123. struct wpa_signal_info *sig_change = arg;
  1124. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1125. genlmsg_attrlen(gnlh, 0), NULL);
  1126. if (!tb[NL80211_ATTR_STA_INFO] ||
  1127. nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
  1128. tb[NL80211_ATTR_STA_INFO], policy))
  1129. return NL_SKIP;
  1130. if (!sinfo[NL80211_STA_INFO_SIGNAL])
  1131. return NL_SKIP;
  1132. sig_change->current_signal =
  1133. (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
  1134. if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
  1135. sig_change->avg_signal =
  1136. (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
  1137. else
  1138. sig_change->avg_signal = 0;
  1139. if (sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
  1140. sig_change->avg_beacon_signal =
  1141. (s8)
  1142. nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]);
  1143. else
  1144. sig_change->avg_beacon_signal = 0;
  1145. if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
  1146. if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
  1147. sinfo[NL80211_STA_INFO_TX_BITRATE],
  1148. rate_policy)) {
  1149. sig_change->current_txrate = 0;
  1150. } else {
  1151. if (rinfo[NL80211_RATE_INFO_BITRATE]) {
  1152. sig_change->current_txrate =
  1153. nla_get_u16(rinfo[
  1154. NL80211_RATE_INFO_BITRATE]) * 100;
  1155. }
  1156. }
  1157. }
  1158. return NL_SKIP;
  1159. }
  1160. int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
  1161. struct wpa_signal_info *sig)
  1162. {
  1163. struct nl_msg *msg;
  1164. sig->current_signal = -9999;
  1165. sig->current_txrate = 0;
  1166. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
  1167. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) {
  1168. nlmsg_free(msg);
  1169. return -ENOBUFS;
  1170. }
  1171. return send_and_recv_msgs(drv, msg, get_link_signal, sig);
  1172. }
  1173. static int get_link_noise(struct nl_msg *msg, void *arg)
  1174. {
  1175. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1176. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1177. struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
  1178. static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
  1179. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  1180. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  1181. };
  1182. struct wpa_signal_info *sig_change = arg;
  1183. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1184. genlmsg_attrlen(gnlh, 0), NULL);
  1185. if (!tb[NL80211_ATTR_SURVEY_INFO]) {
  1186. wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
  1187. return NL_SKIP;
  1188. }
  1189. if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
  1190. tb[NL80211_ATTR_SURVEY_INFO],
  1191. survey_policy)) {
  1192. wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
  1193. "attributes!");
  1194. return NL_SKIP;
  1195. }
  1196. if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
  1197. return NL_SKIP;
  1198. if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
  1199. sig_change->frequency)
  1200. return NL_SKIP;
  1201. if (!sinfo[NL80211_SURVEY_INFO_NOISE])
  1202. return NL_SKIP;
  1203. sig_change->current_noise =
  1204. (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
  1205. return NL_SKIP;
  1206. }
  1207. int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
  1208. struct wpa_signal_info *sig_change)
  1209. {
  1210. struct nl_msg *msg;
  1211. sig_change->current_noise = 9999;
  1212. sig_change->frequency = drv->assoc_freq;
  1213. msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
  1214. return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
  1215. }
  1216. static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
  1217. void *handle)
  1218. {
  1219. struct nl_cb *cb = eloop_ctx;
  1220. int res;
  1221. wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
  1222. res = nl_recvmsgs(handle, cb);
  1223. if (res < 0) {
  1224. wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
  1225. __func__, res);
  1226. }
  1227. }
  1228. /**
  1229. * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
  1230. * @priv: driver_nl80211 private data
  1231. * @alpha2_arg: country to which to switch to
  1232. * Returns: 0 on success, -1 on failure
  1233. *
  1234. * This asks nl80211 to set the regulatory domain for given
  1235. * country ISO / IEC alpha2.
  1236. */
  1237. static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
  1238. {
  1239. struct i802_bss *bss = priv;
  1240. struct wpa_driver_nl80211_data *drv = bss->drv;
  1241. char alpha2[3];
  1242. struct nl_msg *msg;
  1243. msg = nlmsg_alloc();
  1244. if (!msg)
  1245. return -ENOMEM;
  1246. alpha2[0] = alpha2_arg[0];
  1247. alpha2[1] = alpha2_arg[1];
  1248. alpha2[2] = '\0';
  1249. if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
  1250. nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
  1251. nlmsg_free(msg);
  1252. return -EINVAL;
  1253. }
  1254. if (send_and_recv_msgs(drv, msg, NULL, NULL))
  1255. return -EINVAL;
  1256. return 0;
  1257. }
  1258. static int nl80211_get_country(struct nl_msg *msg, void *arg)
  1259. {
  1260. char *alpha2 = arg;
  1261. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  1262. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1263. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1264. genlmsg_attrlen(gnlh, 0), NULL);
  1265. if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
  1266. wpa_printf(MSG_DEBUG, "nl80211: No country information available");
  1267. return NL_SKIP;
  1268. }
  1269. os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
  1270. return NL_SKIP;
  1271. }
  1272. static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
  1273. {
  1274. struct i802_bss *bss = priv;
  1275. struct wpa_driver_nl80211_data *drv = bss->drv;
  1276. struct nl_msg *msg;
  1277. int ret;
  1278. msg = nlmsg_alloc();
  1279. if (!msg)
  1280. return -ENOMEM;
  1281. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
  1282. alpha2[0] = '\0';
  1283. ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
  1284. if (!alpha2[0])
  1285. ret = -1;
  1286. return ret;
  1287. }
  1288. static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
  1289. {
  1290. int ret;
  1291. global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  1292. if (global->nl_cb == NULL) {
  1293. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  1294. "callbacks");
  1295. return -1;
  1296. }
  1297. global->nl = nl_create_handle(global->nl_cb, "nl");
  1298. if (global->nl == NULL)
  1299. goto err;
  1300. global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
  1301. if (global->nl80211_id < 0) {
  1302. wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
  1303. "found");
  1304. goto err;
  1305. }
  1306. global->nl_event = nl_create_handle(global->nl_cb, "event");
  1307. if (global->nl_event == NULL)
  1308. goto err;
  1309. ret = nl_get_multicast_id(global, "nl80211", "scan");
  1310. if (ret >= 0)
  1311. ret = nl_socket_add_membership(global->nl_event, ret);
  1312. if (ret < 0) {
  1313. wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
  1314. "membership for scan events: %d (%s)",
  1315. ret, strerror(-ret));
  1316. goto err;
  1317. }
  1318. ret = nl_get_multicast_id(global, "nl80211", "mlme");
  1319. if (ret >= 0)
  1320. ret = nl_socket_add_membership(global->nl_event, ret);
  1321. if (ret < 0) {
  1322. wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
  1323. "membership for mlme events: %d (%s)",
  1324. ret, strerror(-ret));
  1325. goto err;
  1326. }
  1327. ret = nl_get_multicast_id(global, "nl80211", "regulatory");
  1328. if (ret >= 0)
  1329. ret = nl_socket_add_membership(global->nl_event, ret);
  1330. if (ret < 0) {
  1331. wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
  1332. "membership for regulatory events: %d (%s)",
  1333. ret, strerror(-ret));
  1334. /* Continue without regulatory events */
  1335. }
  1336. ret = nl_get_multicast_id(global, "nl80211", "vendor");
  1337. if (ret >= 0)
  1338. ret = nl_socket_add_membership(global->nl_event, ret);
  1339. if (ret < 0) {
  1340. wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
  1341. "membership for vendor events: %d (%s)",
  1342. ret, strerror(-ret));
  1343. /* Continue without vendor events */
  1344. }
  1345. nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
  1346. no_seq_check, NULL);
  1347. nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
  1348. process_global_event, global);
  1349. nl80211_register_eloop_read(&global->nl_event,
  1350. wpa_driver_nl80211_event_receive,
  1351. global->nl_cb);
  1352. return 0;
  1353. err:
  1354. nl_destroy_handles(&global->nl_event);
  1355. nl_destroy_handles(&global->nl);
  1356. nl_cb_put(global->nl_cb);
  1357. global->nl_cb = NULL;
  1358. return -1;
  1359. }
  1360. static void nl80211_check_global(struct nl80211_global *global)
  1361. {
  1362. struct nl_handle *handle;
  1363. const char *groups[] = { "scan", "mlme", "regulatory", "vendor", NULL };
  1364. int ret;
  1365. unsigned int i;
  1366. /*
  1367. * Try to re-add memberships to handle case of cfg80211 getting reloaded
  1368. * and all registration having been cleared.
  1369. */
  1370. handle = (void *) (((intptr_t) global->nl_event) ^
  1371. ELOOP_SOCKET_INVALID);
  1372. for (i = 0; groups[i]; i++) {
  1373. ret = nl_get_multicast_id(global, "nl80211", groups[i]);
  1374. if (ret >= 0)
  1375. ret = nl_socket_add_membership(handle, ret);
  1376. if (ret < 0) {
  1377. wpa_printf(MSG_INFO,
  1378. "nl80211: Could not re-add multicast membership for %s events: %d (%s)",
  1379. groups[i], ret, strerror(-ret));
  1380. }
  1381. }
  1382. }
  1383. static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
  1384. {
  1385. struct wpa_driver_nl80211_data *drv = ctx;
  1386. wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
  1387. /*
  1388. * rtnetlink ifdown handler will report interfaces other than the P2P
  1389. * Device interface as disabled.
  1390. */
  1391. if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
  1392. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_DISABLED, NULL);
  1393. }
  1394. static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
  1395. {
  1396. struct wpa_driver_nl80211_data *drv = ctx;
  1397. wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
  1398. if (i802_set_iface_flags(drv->first_bss, 1)) {
  1399. wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
  1400. "after rfkill unblock");
  1401. return;
  1402. }
  1403. if (is_p2p_net_interface(drv->nlmode))
  1404. nl80211_disable_11b_rates(drv, drv->ifindex, 1);
  1405. /*
  1406. * rtnetlink ifup handler will report interfaces other than the P2P
  1407. * Device interface as enabled.
  1408. */
  1409. if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
  1410. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
  1411. }
  1412. static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
  1413. void *eloop_ctx,
  1414. void *handle)
  1415. {
  1416. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  1417. u8 data[2048];
  1418. struct msghdr msg;
  1419. struct iovec entry;
  1420. u8 control[512];
  1421. struct cmsghdr *cmsg;
  1422. int res, found_ee = 0, found_wifi = 0, acked = 0;
  1423. union wpa_event_data event;
  1424. memset(&msg, 0, sizeof(msg));
  1425. msg.msg_iov = &entry;
  1426. msg.msg_iovlen = 1;
  1427. entry.iov_base = data;
  1428. entry.iov_len = sizeof(data);
  1429. msg.msg_control = &control;
  1430. msg.msg_controllen = sizeof(control);
  1431. res = recvmsg(sock, &msg, MSG_ERRQUEUE);
  1432. /* if error or not fitting 802.3 header, return */
  1433. if (res < 14)
  1434. return;
  1435. for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
  1436. {
  1437. if (cmsg->cmsg_level == SOL_SOCKET &&
  1438. cmsg->cmsg_type == SCM_WIFI_STATUS) {
  1439. int *ack;
  1440. found_wifi = 1;
  1441. ack = (void *)CMSG_DATA(cmsg);
  1442. acked = *ack;
  1443. }
  1444. if (cmsg->cmsg_level == SOL_PACKET &&
  1445. cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
  1446. struct sock_extended_err *err =
  1447. (struct sock_extended_err *)CMSG_DATA(cmsg);
  1448. if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
  1449. found_ee = 1;
  1450. }
  1451. }
  1452. if (!found_ee || !found_wifi)
  1453. return;
  1454. memset(&event, 0, sizeof(event));
  1455. event.eapol_tx_status.dst = data;
  1456. event.eapol_tx_status.data = data + 14;
  1457. event.eapol_tx_status.data_len = res - 14;
  1458. event.eapol_tx_status.ack = acked;
  1459. wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
  1460. }
  1461. static int nl80211_init_bss(struct i802_bss *bss)
  1462. {
  1463. bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  1464. if (!bss->nl_cb)
  1465. return -1;
  1466. nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
  1467. no_seq_check, NULL);
  1468. nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
  1469. process_bss_event, bss);
  1470. return 0;
  1471. }
  1472. static void nl80211_destroy_bss(struct i802_bss *bss)
  1473. {
  1474. nl_cb_put(bss->nl_cb);
  1475. bss->nl_cb = NULL;
  1476. }
  1477. static void
  1478. wpa_driver_nl80211_drv_init_rfkill(struct wpa_driver_nl80211_data *drv)
  1479. {
  1480. struct rfkill_config *rcfg;
  1481. if (drv->rfkill)
  1482. return;
  1483. rcfg = os_zalloc(sizeof(*rcfg));
  1484. if (!rcfg)
  1485. return;
  1486. rcfg->ctx = drv;
  1487. /* rfkill uses netdev sysfs for initialization. However, P2P Device is
  1488. * not associated with a netdev, so use the name of some other interface
  1489. * sharing the same wiphy as the P2P Device interface.
  1490. *
  1491. * Note: This is valid, as a P2P Device interface is always dynamically
  1492. * created and is created only once another wpa_s interface was added.
  1493. */
  1494. if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) {
  1495. struct nl80211_global *global = drv->global;
  1496. struct wpa_driver_nl80211_data *tmp1;
  1497. dl_list_for_each(tmp1, &global->interfaces,
  1498. struct wpa_driver_nl80211_data, list) {
  1499. if (drv == tmp1 || drv->wiphy_idx != tmp1->wiphy_idx ||
  1500. !tmp1->rfkill)
  1501. continue;
  1502. wpa_printf(MSG_DEBUG,
  1503. "nl80211: Use (%s) to initialize P2P Device rfkill",
  1504. tmp1->first_bss->ifname);
  1505. os_strlcpy(rcfg->ifname, tmp1->first_bss->ifname,
  1506. sizeof(rcfg->ifname));
  1507. break;
  1508. }
  1509. } else {
  1510. os_strlcpy(rcfg->ifname, drv->first_bss->ifname,
  1511. sizeof(rcfg->ifname));
  1512. }
  1513. rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
  1514. rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
  1515. drv->rfkill = rfkill_init(rcfg);
  1516. if (!drv->rfkill) {
  1517. wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
  1518. os_free(rcfg);
  1519. }
  1520. }
  1521. static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
  1522. void *global_priv, int hostapd,
  1523. const u8 *set_addr,
  1524. const char *driver_params)
  1525. {
  1526. struct wpa_driver_nl80211_data *drv;
  1527. struct i802_bss *bss;
  1528. if (global_priv == NULL)
  1529. return NULL;
  1530. drv = os_zalloc(sizeof(*drv));
  1531. if (drv == NULL)
  1532. return NULL;
  1533. drv->global = global_priv;
  1534. drv->ctx = ctx;
  1535. drv->hostapd = !!hostapd;
  1536. drv->eapol_sock = -1;
  1537. /*
  1538. * There is no driver capability flag for this, so assume it is
  1539. * supported and disable this on first attempt to use if the driver
  1540. * rejects the command due to missing support.
  1541. */
  1542. drv->set_rekey_offload = 1;
  1543. drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
  1544. drv->if_indices = drv->default_if_indices;
  1545. drv->if_indices_reason = drv->default_if_indices_reason;
  1546. drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
  1547. if (!drv->first_bss) {
  1548. os_free(drv);
  1549. return NULL;
  1550. }
  1551. bss = drv->first_bss;
  1552. bss->drv = drv;
  1553. bss->ctx = ctx;
  1554. os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
  1555. drv->monitor_ifidx = -1;
  1556. drv->monitor_sock = -1;
  1557. drv->eapol_tx_sock = -1;
  1558. drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
  1559. if (nl80211_init_bss(bss))
  1560. goto failed;
  1561. if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
  1562. goto failed;
  1563. drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
  1564. if (drv->eapol_tx_sock < 0)
  1565. goto failed;
  1566. if (drv->data_tx_status) {
  1567. int enabled = 1;
  1568. if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
  1569. &enabled, sizeof(enabled)) < 0) {
  1570. wpa_printf(MSG_DEBUG,
  1571. "nl80211: wifi status sockopt failed\n");
  1572. drv->data_tx_status = 0;
  1573. if (!drv->use_monitor)
  1574. drv->capa.flags &=
  1575. ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
  1576. } else {
  1577. eloop_register_read_sock(drv->eapol_tx_sock,
  1578. wpa_driver_nl80211_handle_eapol_tx_status,
  1579. drv, NULL);
  1580. }
  1581. }
  1582. if (drv->global) {
  1583. nl80211_check_global(drv->global);
  1584. dl_list_add(&drv->global->interfaces, &drv->list);
  1585. drv->in_interface_list = 1;
  1586. }
  1587. return bss;
  1588. failed:
  1589. wpa_driver_nl80211_deinit(bss);
  1590. return NULL;
  1591. }
  1592. /**
  1593. * wpa_driver_nl80211_init - Initialize nl80211 driver interface
  1594. * @ctx: context to be used when calling wpa_supplicant functions,
  1595. * e.g., wpa_supplicant_event()
  1596. * @ifname: interface name, e.g., wlan0
  1597. * @global_priv: private driver global data from global_init()
  1598. * Returns: Pointer to private data, %NULL on failure
  1599. */
  1600. static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
  1601. void *global_priv)
  1602. {
  1603. return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
  1604. NULL);
  1605. }
  1606. static int nl80211_register_frame(struct i802_bss *bss,
  1607. struct nl_handle *nl_handle,
  1608. u16 type, const u8 *match, size_t match_len)
  1609. {
  1610. struct wpa_driver_nl80211_data *drv = bss->drv;
  1611. struct nl_msg *msg;
  1612. int ret;
  1613. char buf[30];
  1614. buf[0] = '\0';
  1615. wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
  1616. wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
  1617. type, fc2str(type), nl_handle, buf);
  1618. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_ACTION)) ||
  1619. nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
  1620. nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
  1621. nlmsg_free(msg);
  1622. return -1;
  1623. }
  1624. ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
  1625. if (ret) {
  1626. wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
  1627. "failed (type=%u): ret=%d (%s)",
  1628. type, ret, strerror(-ret));
  1629. wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
  1630. match, match_len);
  1631. }
  1632. return ret;
  1633. }
  1634. static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
  1635. {
  1636. if (bss->nl_mgmt) {
  1637. wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
  1638. "already on! (nl_mgmt=%p)", bss->nl_mgmt);
  1639. return -1;
  1640. }
  1641. bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
  1642. if (bss->nl_mgmt == NULL)
  1643. return -1;
  1644. return 0;
  1645. }
  1646. static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
  1647. {
  1648. nl80211_register_eloop_read(&bss->nl_mgmt,
  1649. wpa_driver_nl80211_event_receive,
  1650. bss->nl_cb);
  1651. }
  1652. static int nl80211_register_action_frame(struct i802_bss *bss,
  1653. const u8 *match, size_t match_len)
  1654. {
  1655. u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
  1656. return nl80211_register_frame(bss, bss->nl_mgmt,
  1657. type, match, match_len);
  1658. }
  1659. static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
  1660. {
  1661. struct wpa_driver_nl80211_data *drv = bss->drv;
  1662. int ret = 0;
  1663. if (nl80211_alloc_mgmt_handle(bss))
  1664. return -1;
  1665. wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
  1666. "handle %p", bss->nl_mgmt);
  1667. if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
  1668. u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
  1669. /* register for any AUTH message */
  1670. nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
  1671. }
  1672. #ifdef CONFIG_INTERWORKING
  1673. /* QoS Map Configure */
  1674. if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
  1675. ret = -1;
  1676. #endif /* CONFIG_INTERWORKING */
  1677. #if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING) || defined(CONFIG_DPP)
  1678. /* GAS Initial Request */
  1679. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
  1680. ret = -1;
  1681. /* GAS Initial Response */
  1682. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
  1683. ret = -1;
  1684. /* GAS Comeback Request */
  1685. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
  1686. ret = -1;
  1687. /* GAS Comeback Response */
  1688. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
  1689. ret = -1;
  1690. /* Protected GAS Initial Request */
  1691. if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
  1692. ret = -1;
  1693. /* Protected GAS Initial Response */
  1694. if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
  1695. ret = -1;
  1696. /* Protected GAS Comeback Request */
  1697. if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
  1698. ret = -1;
  1699. /* Protected GAS Comeback Response */
  1700. if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
  1701. ret = -1;
  1702. #endif /* CONFIG_P2P || CONFIG_INTERWORKING || CONFIG_DPP */
  1703. #ifdef CONFIG_P2P
  1704. /* P2P Public Action */
  1705. if (nl80211_register_action_frame(bss,
  1706. (u8 *) "\x04\x09\x50\x6f\x9a\x09",
  1707. 6) < 0)
  1708. ret = -1;
  1709. /* P2P Action */
  1710. if (nl80211_register_action_frame(bss,
  1711. (u8 *) "\x7f\x50\x6f\x9a\x09",
  1712. 5) < 0)
  1713. ret = -1;
  1714. #endif /* CONFIG_P2P */
  1715. #ifdef CONFIG_DPP
  1716. /* DPP Public Action */
  1717. if (nl80211_register_action_frame(bss,
  1718. (u8 *) "\x04\x09\x50\x6f\x9a\x1a",
  1719. 6) < 0)
  1720. ret = -1;
  1721. #endif /* CONFIG_DPP */
  1722. #ifdef CONFIG_IEEE80211W
  1723. /* SA Query Response */
  1724. if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
  1725. ret = -1;
  1726. #endif /* CONFIG_IEEE80211W */
  1727. #ifdef CONFIG_TDLS
  1728. if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
  1729. /* TDLS Discovery Response */
  1730. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
  1731. 0)
  1732. ret = -1;
  1733. }
  1734. #endif /* CONFIG_TDLS */
  1735. #ifdef CONFIG_FST
  1736. /* FST Action frames */
  1737. if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
  1738. ret = -1;
  1739. #endif /* CONFIG_FST */
  1740. /* FT Action frames */
  1741. if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
  1742. ret = -1;
  1743. else
  1744. drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
  1745. WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
  1746. /* WNM - BSS Transition Management Request */
  1747. if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
  1748. ret = -1;
  1749. /* WNM-Sleep Mode Response */
  1750. if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
  1751. ret = -1;
  1752. #ifdef CONFIG_HS20
  1753. /* WNM-Notification */
  1754. if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
  1755. ret = -1;
  1756. #endif /* CONFIG_HS20 */
  1757. /* WMM-AC ADDTS Response */
  1758. if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
  1759. ret = -1;
  1760. /* WMM-AC DELTS */
  1761. if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
  1762. ret = -1;
  1763. /* Radio Measurement - Neighbor Report Response */
  1764. if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
  1765. ret = -1;
  1766. /* Radio Measurement - Radio Measurement Request */
  1767. if (nl80211_register_action_frame(bss, (u8 *) "\x05\x00", 2) < 0)
  1768. ret = -1;
  1769. /* Radio Measurement - Link Measurement Request */
  1770. if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
  1771. (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
  1772. ret = -1;
  1773. nl80211_mgmt_handle_register_eloop(bss);
  1774. return ret;
  1775. }
  1776. static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
  1777. {
  1778. int ret = 0;
  1779. if (nl80211_alloc_mgmt_handle(bss))
  1780. return -1;
  1781. wpa_printf(MSG_DEBUG,
  1782. "nl80211: Subscribe to mgmt frames with mesh handle %p",
  1783. bss->nl_mgmt);
  1784. /* Auth frames for mesh SAE */
  1785. if (nl80211_register_frame(bss, bss->nl_mgmt,
  1786. (WLAN_FC_TYPE_MGMT << 2) |
  1787. (WLAN_FC_STYPE_AUTH << 4),
  1788. NULL, 0) < 0)
  1789. ret = -1;
  1790. /* Mesh peering open */
  1791. if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
  1792. ret = -1;
  1793. /* Mesh peering confirm */
  1794. if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
  1795. ret = -1;
  1796. /* Mesh peering close */
  1797. if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
  1798. ret = -1;
  1799. nl80211_mgmt_handle_register_eloop(bss);
  1800. return ret;
  1801. }
  1802. static int nl80211_register_spurious_class3(struct i802_bss *bss)
  1803. {
  1804. struct nl_msg *msg;
  1805. int ret;
  1806. msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
  1807. ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
  1808. if (ret) {
  1809. wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
  1810. "failed: ret=%d (%s)",
  1811. ret, strerror(-ret));
  1812. }
  1813. return ret;
  1814. }
  1815. static int nl80211_action_subscribe_ap(struct i802_bss *bss)
  1816. {
  1817. int ret = 0;
  1818. /* Public Action frames */
  1819. if (nl80211_register_action_frame(bss, (u8 *) "\x04", 1) < 0)
  1820. ret = -1;
  1821. /* RRM Measurement Report */
  1822. if (nl80211_register_action_frame(bss, (u8 *) "\x05\x01", 2) < 0)
  1823. ret = -1;
  1824. /* RRM Link Measurement Report */
  1825. if (nl80211_register_action_frame(bss, (u8 *) "\x05\x03", 2) < 0)
  1826. ret = -1;
  1827. /* RRM Neighbor Report Request */
  1828. if (nl80211_register_action_frame(bss, (u8 *) "\x05\x04", 2) < 0)
  1829. ret = -1;
  1830. /* FT Action frames */
  1831. if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
  1832. ret = -1;
  1833. #ifdef CONFIG_IEEE80211W
  1834. /* SA Query */
  1835. if (nl80211_register_action_frame(bss, (u8 *) "\x08", 1) < 0)
  1836. ret = -1;
  1837. #endif /* CONFIG_IEEE80211W */
  1838. /* Protected Dual of Public Action */
  1839. if (nl80211_register_action_frame(bss, (u8 *) "\x09", 1) < 0)
  1840. ret = -1;
  1841. /* WNM */
  1842. if (nl80211_register_action_frame(bss, (u8 *) "\x0a", 1) < 0)
  1843. ret = -1;
  1844. /* WMM */
  1845. if (nl80211_register_action_frame(bss, (u8 *) "\x11", 1) < 0)
  1846. ret = -1;
  1847. #ifdef CONFIG_FST
  1848. /* FST Action frames */
  1849. if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
  1850. ret = -1;
  1851. #endif /* CONFIG_FST */
  1852. /* Vendor-specific */
  1853. if (nl80211_register_action_frame(bss, (u8 *) "\x7f", 1) < 0)
  1854. ret = -1;
  1855. return ret;
  1856. }
  1857. static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
  1858. {
  1859. static const int stypes[] = {
  1860. WLAN_FC_STYPE_AUTH,
  1861. WLAN_FC_STYPE_ASSOC_REQ,
  1862. WLAN_FC_STYPE_REASSOC_REQ,
  1863. WLAN_FC_STYPE_DISASSOC,
  1864. WLAN_FC_STYPE_DEAUTH,
  1865. WLAN_FC_STYPE_PROBE_REQ,
  1866. /* Beacon doesn't work as mac80211 doesn't currently allow
  1867. * it, but it wouldn't really be the right thing anyway as
  1868. * it isn't per interface ... maybe just dump the scan
  1869. * results periodically for OLBC?
  1870. */
  1871. /* WLAN_FC_STYPE_BEACON, */
  1872. };
  1873. unsigned int i;
  1874. if (nl80211_alloc_mgmt_handle(bss))
  1875. return -1;
  1876. wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
  1877. "handle %p", bss->nl_mgmt);
  1878. for (i = 0; i < ARRAY_SIZE(stypes); i++) {
  1879. if (nl80211_register_frame(bss, bss->nl_mgmt,
  1880. (WLAN_FC_TYPE_MGMT << 2) |
  1881. (stypes[i] << 4),
  1882. NULL, 0) < 0) {
  1883. goto out_err;
  1884. }
  1885. }
  1886. if (nl80211_action_subscribe_ap(bss))
  1887. goto out_err;
  1888. if (nl80211_register_spurious_class3(bss))
  1889. goto out_err;
  1890. nl80211_mgmt_handle_register_eloop(bss);
  1891. return 0;
  1892. out_err:
  1893. nl_destroy_handles(&bss->nl_mgmt);
  1894. return -1;
  1895. }
  1896. static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
  1897. {
  1898. if (nl80211_alloc_mgmt_handle(bss))
  1899. return -1;
  1900. wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
  1901. "handle %p (device SME)", bss->nl_mgmt);
  1902. if (nl80211_action_subscribe_ap(bss))
  1903. goto out_err;
  1904. nl80211_mgmt_handle_register_eloop(bss);
  1905. return 0;
  1906. out_err:
  1907. nl_destroy_handles(&bss->nl_mgmt);
  1908. return -1;
  1909. }
  1910. static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
  1911. {
  1912. if (bss->nl_mgmt == NULL)
  1913. return;
  1914. wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
  1915. "(%s)", bss->nl_mgmt, reason);
  1916. nl80211_destroy_eloop_handle(&bss->nl_mgmt);
  1917. nl80211_put_wiphy_data_ap(bss);
  1918. }
  1919. static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
  1920. {
  1921. wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
  1922. }
  1923. static void nl80211_del_p2pdev(struct i802_bss *bss)
  1924. {
  1925. struct nl_msg *msg;
  1926. int ret;
  1927. msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
  1928. ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  1929. wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
  1930. bss->ifname, (long long unsigned int) bss->wdev_id,
  1931. strerror(-ret));
  1932. }
  1933. static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
  1934. {
  1935. struct nl_msg *msg;
  1936. int ret;
  1937. msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
  1938. NL80211_CMD_STOP_P2P_DEVICE);
  1939. ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  1940. wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
  1941. start ? "Start" : "Stop",
  1942. bss->ifname, (long long unsigned int) bss->wdev_id,
  1943. strerror(-ret));
  1944. return ret;
  1945. }
  1946. static int i802_set_iface_flags(struct i802_bss *bss, int up)
  1947. {
  1948. enum nl80211_iftype nlmode;
  1949. nlmode = nl80211_get_ifmode(bss);
  1950. if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
  1951. return linux_set_iface_flags(bss->drv->global->ioctl_sock,
  1952. bss->ifname, up);
  1953. }
  1954. /* P2P Device has start/stop which is equivalent */
  1955. return nl80211_set_p2pdev(bss, up);
  1956. }
  1957. #ifdef CONFIG_TESTING_OPTIONS
  1958. static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
  1959. {
  1960. /* struct wpa_driver_nl80211_data *drv = arg; */
  1961. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1962. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1963. wpa_printf(MSG_DEBUG,
  1964. "nl80211: QCA vendor test command response received");
  1965. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1966. genlmsg_attrlen(gnlh, 0), NULL);
  1967. if (!tb[NL80211_ATTR_VENDOR_DATA]) {
  1968. wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
  1969. return NL_SKIP;
  1970. }
  1971. wpa_hexdump(MSG_DEBUG,
  1972. "nl80211: Received QCA vendor test command response",
  1973. nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
  1974. nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
  1975. return NL_SKIP;
  1976. }
  1977. #endif /* CONFIG_TESTING_OPTIONS */
  1978. static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
  1979. {
  1980. #ifdef CONFIG_TESTING_OPTIONS
  1981. struct nl_msg *msg;
  1982. struct nlattr *params;
  1983. int ret;
  1984. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
  1985. nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
  1986. nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
  1987. QCA_NL80211_VENDOR_SUBCMD_TEST) ||
  1988. !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
  1989. nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
  1990. nlmsg_free(msg);
  1991. return;
  1992. }
  1993. nla_nest_end(msg, params);
  1994. ret = send_and_recv_msgs(drv, msg, qca_vendor_test_cmd_handler, drv);
  1995. wpa_printf(MSG_DEBUG,
  1996. "nl80211: QCA vendor test command returned %d (%s)",
  1997. ret, strerror(-ret));
  1998. #endif /* CONFIG_TESTING_OPTIONS */
  1999. }
  2000. static int
  2001. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
  2002. const u8 *set_addr, int first,
  2003. const char *driver_params)
  2004. {
  2005. struct i802_bss *bss = drv->first_bss;
  2006. int send_rfkill_event = 0;
  2007. enum nl80211_iftype nlmode;
  2008. drv->ifindex = if_nametoindex(bss->ifname);
  2009. bss->ifindex = drv->ifindex;
  2010. bss->wdev_id = drv->global->if_add_wdevid;
  2011. bss->wdev_id_set = drv->global->if_add_wdevid_set;
  2012. bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
  2013. bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
  2014. drv->global->if_add_wdevid_set = 0;
  2015. if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
  2016. bss->static_ap = 1;
  2017. if (first &&
  2018. nl80211_get_ifmode(bss) != NL80211_IFTYPE_P2P_DEVICE &&
  2019. linux_iface_up(drv->global->ioctl_sock, bss->ifname) > 0)
  2020. drv->start_iface_up = 1;
  2021. if (wpa_driver_nl80211_capa(drv))
  2022. return -1;
  2023. if (driver_params && nl80211_set_param(bss, driver_params) < 0)
  2024. return -1;
  2025. wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
  2026. bss->ifname, drv->phyname);
  2027. if (set_addr &&
  2028. (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
  2029. linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  2030. set_addr)))
  2031. return -1;
  2032. if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
  2033. drv->start_mode_ap = 1;
  2034. if (drv->hostapd || bss->static_ap)
  2035. nlmode = NL80211_IFTYPE_AP;
  2036. else if (bss->if_dynamic ||
  2037. nl80211_get_ifmode(bss) == NL80211_IFTYPE_MESH_POINT)
  2038. nlmode = nl80211_get_ifmode(bss);
  2039. else
  2040. nlmode = NL80211_IFTYPE_STATION;
  2041. if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
  2042. wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
  2043. return -1;
  2044. }
  2045. if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
  2046. nl80211_get_macaddr(bss);
  2047. wpa_driver_nl80211_drv_init_rfkill(drv);
  2048. if (!rfkill_is_blocked(drv->rfkill)) {
  2049. int ret = i802_set_iface_flags(bss, 1);
  2050. if (ret) {
  2051. wpa_printf(MSG_ERROR, "nl80211: Could not set "
  2052. "interface '%s' UP", bss->ifname);
  2053. return ret;
  2054. }
  2055. if (is_p2p_net_interface(nlmode))
  2056. nl80211_disable_11b_rates(bss->drv,
  2057. bss->drv->ifindex, 1);
  2058. if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
  2059. return ret;
  2060. } else {
  2061. wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
  2062. "interface '%s' due to rfkill", bss->ifname);
  2063. if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
  2064. drv->if_disabled = 1;
  2065. send_rfkill_event = 1;
  2066. }
  2067. if (!drv->hostapd && nlmode != NL80211_IFTYPE_P2P_DEVICE)
  2068. netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
  2069. 1, IF_OPER_DORMANT);
  2070. if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
  2071. if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  2072. bss->addr))
  2073. return -1;
  2074. os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
  2075. }
  2076. if (send_rfkill_event) {
  2077. eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
  2078. drv, drv->ctx);
  2079. }
  2080. if (drv->vendor_cmd_test_avail)
  2081. qca_vendor_test(drv);
  2082. return 0;
  2083. }
  2084. static int wpa_driver_nl80211_del_beacon(struct i802_bss *bss)
  2085. {
  2086. struct nl_msg *msg;
  2087. struct wpa_driver_nl80211_data *drv = bss->drv;
  2088. wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
  2089. drv->ifindex);
  2090. nl80211_put_wiphy_data_ap(bss);
  2091. msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
  2092. return send_and_recv_msgs(drv, msg, NULL, NULL);
  2093. }
  2094. /**
  2095. * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
  2096. * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
  2097. *
  2098. * Shut down driver interface and processing of driver events. Free
  2099. * private data buffer if one was allocated in wpa_driver_nl80211_init().
  2100. */
  2101. static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
  2102. {
  2103. struct wpa_driver_nl80211_data *drv = bss->drv;
  2104. unsigned int i;
  2105. wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
  2106. bss->ifname, drv->disabled_11b_rates);
  2107. bss->in_deinit = 1;
  2108. if (drv->data_tx_status)
  2109. eloop_unregister_read_sock(drv->eapol_tx_sock);
  2110. if (drv->eapol_tx_sock >= 0)
  2111. close(drv->eapol_tx_sock);
  2112. if (bss->nl_preq)
  2113. wpa_driver_nl80211_probe_req_report(bss, 0);
  2114. if (bss->added_if_into_bridge) {
  2115. if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
  2116. bss->ifname) < 0)
  2117. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  2118. "interface %s from bridge %s: %s",
  2119. bss->ifname, bss->brname, strerror(errno));
  2120. if (drv->rtnl_sk)
  2121. nl80211_handle_destroy(drv->rtnl_sk);
  2122. }
  2123. if (bss->added_bridge) {
  2124. if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
  2125. 0) < 0)
  2126. wpa_printf(MSG_INFO,
  2127. "nl80211: Could not set bridge %s down",
  2128. bss->brname);
  2129. if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
  2130. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  2131. "bridge %s: %s",
  2132. bss->brname, strerror(errno));
  2133. }
  2134. nl80211_remove_monitor_interface(drv);
  2135. if (is_ap_interface(drv->nlmode))
  2136. wpa_driver_nl80211_del_beacon(bss);
  2137. if (drv->eapol_sock >= 0) {
  2138. eloop_unregister_read_sock(drv->eapol_sock);
  2139. close(drv->eapol_sock);
  2140. }
  2141. if (drv->if_indices != drv->default_if_indices)
  2142. os_free(drv->if_indices);
  2143. if (drv->if_indices_reason != drv->default_if_indices_reason)
  2144. os_free(drv->if_indices_reason);
  2145. if (drv->disabled_11b_rates)
  2146. nl80211_disable_11b_rates(drv, drv->ifindex, 0);
  2147. netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
  2148. IF_OPER_UP);
  2149. eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
  2150. rfkill_deinit(drv->rfkill);
  2151. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
  2152. if (!drv->start_iface_up)
  2153. (void) i802_set_iface_flags(bss, 0);
  2154. if (drv->addr_changed) {
  2155. if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
  2156. 0) < 0) {
  2157. wpa_printf(MSG_DEBUG,
  2158. "nl80211: Could not set interface down to restore permanent MAC address");
  2159. }
  2160. if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  2161. drv->perm_addr) < 0) {
  2162. wpa_printf(MSG_DEBUG,
  2163. "nl80211: Could not restore permanent MAC address");
  2164. }
  2165. }
  2166. if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
  2167. if (!drv->hostapd || !drv->start_mode_ap)
  2168. wpa_driver_nl80211_set_mode(bss,
  2169. NL80211_IFTYPE_STATION);
  2170. nl80211_mgmt_unsubscribe(bss, "deinit");
  2171. } else {
  2172. nl80211_mgmt_unsubscribe(bss, "deinit");
  2173. nl80211_del_p2pdev(bss);
  2174. }
  2175. nl80211_destroy_bss(drv->first_bss);
  2176. os_free(drv->filter_ssids);
  2177. os_free(drv->auth_ie);
  2178. if (drv->in_interface_list)
  2179. dl_list_del(&drv->list);
  2180. os_free(drv->extended_capa);
  2181. os_free(drv->extended_capa_mask);
  2182. for (i = 0; i < drv->num_iface_ext_capa; i++) {
  2183. os_free(drv->iface_ext_capa[i].ext_capa);
  2184. os_free(drv->iface_ext_capa[i].ext_capa_mask);
  2185. }
  2186. os_free(drv->first_bss);
  2187. os_free(drv);
  2188. }
  2189. static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
  2190. {
  2191. switch (alg) {
  2192. case WPA_ALG_WEP:
  2193. if (key_len == 5)
  2194. return RSN_CIPHER_SUITE_WEP40;
  2195. return RSN_CIPHER_SUITE_WEP104;
  2196. case WPA_ALG_TKIP:
  2197. return RSN_CIPHER_SUITE_TKIP;
  2198. case WPA_ALG_CCMP:
  2199. return RSN_CIPHER_SUITE_CCMP;
  2200. case WPA_ALG_GCMP:
  2201. return RSN_CIPHER_SUITE_GCMP;
  2202. case WPA_ALG_CCMP_256:
  2203. return RSN_CIPHER_SUITE_CCMP_256;
  2204. case WPA_ALG_GCMP_256:
  2205. return RSN_CIPHER_SUITE_GCMP_256;
  2206. case WPA_ALG_IGTK:
  2207. return RSN_CIPHER_SUITE_AES_128_CMAC;
  2208. case WPA_ALG_BIP_GMAC_128:
  2209. return RSN_CIPHER_SUITE_BIP_GMAC_128;
  2210. case WPA_ALG_BIP_GMAC_256:
  2211. return RSN_CIPHER_SUITE_BIP_GMAC_256;
  2212. case WPA_ALG_BIP_CMAC_256:
  2213. return RSN_CIPHER_SUITE_BIP_CMAC_256;
  2214. case WPA_ALG_SMS4:
  2215. return RSN_CIPHER_SUITE_SMS4;
  2216. case WPA_ALG_KRK:
  2217. return RSN_CIPHER_SUITE_KRK;
  2218. case WPA_ALG_NONE:
  2219. case WPA_ALG_PMK:
  2220. wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
  2221. alg);
  2222. return 0;
  2223. }
  2224. wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
  2225. alg);
  2226. return 0;
  2227. }
  2228. static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
  2229. {
  2230. switch (cipher) {
  2231. case WPA_CIPHER_CCMP_256:
  2232. return RSN_CIPHER_SUITE_CCMP_256;
  2233. case WPA_CIPHER_GCMP_256:
  2234. return RSN_CIPHER_SUITE_GCMP_256;
  2235. case WPA_CIPHER_CCMP:
  2236. return RSN_CIPHER_SUITE_CCMP;
  2237. case WPA_CIPHER_GCMP:
  2238. return RSN_CIPHER_SUITE_GCMP;
  2239. case WPA_CIPHER_TKIP:
  2240. return RSN_CIPHER_SUITE_TKIP;
  2241. case WPA_CIPHER_WEP104:
  2242. return RSN_CIPHER_SUITE_WEP104;
  2243. case WPA_CIPHER_WEP40:
  2244. return RSN_CIPHER_SUITE_WEP40;
  2245. case WPA_CIPHER_GTK_NOT_USED:
  2246. return RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED;
  2247. }
  2248. return 0;
  2249. }
  2250. static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
  2251. int max_suites)
  2252. {
  2253. int num_suites = 0;
  2254. if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
  2255. suites[num_suites++] = RSN_CIPHER_SUITE_CCMP_256;
  2256. if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
  2257. suites[num_suites++] = RSN_CIPHER_SUITE_GCMP_256;
  2258. if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
  2259. suites[num_suites++] = RSN_CIPHER_SUITE_CCMP;
  2260. if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
  2261. suites[num_suites++] = RSN_CIPHER_SUITE_GCMP;
  2262. if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
  2263. suites[num_suites++] = RSN_CIPHER_SUITE_TKIP;
  2264. if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
  2265. suites[num_suites++] = RSN_CIPHER_SUITE_WEP104;
  2266. if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
  2267. suites[num_suites++] = RSN_CIPHER_SUITE_WEP40;
  2268. return num_suites;
  2269. }
  2270. #ifdef CONFIG_DRIVER_NL80211_QCA
  2271. static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
  2272. const u8 *key, size_t key_len)
  2273. {
  2274. struct nl_msg *msg;
  2275. int ret;
  2276. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
  2277. return 0;
  2278. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
  2279. nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
  2280. nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
  2281. QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
  2282. nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
  2283. nl80211_nlmsg_clear(msg);
  2284. nlmsg_free(msg);
  2285. return -1;
  2286. }
  2287. ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
  2288. if (ret) {
  2289. wpa_printf(MSG_DEBUG,
  2290. "nl80211: Key management set key failed: ret=%d (%s)",
  2291. ret, strerror(-ret));
  2292. }
  2293. return ret;
  2294. }
  2295. #endif /* CONFIG_DRIVER_NL80211_QCA */
  2296. static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
  2297. enum wpa_alg alg, const u8 *addr,
  2298. int key_idx, int set_tx,
  2299. const u8 *seq, size_t seq_len,
  2300. const u8 *key, size_t key_len)
  2301. {
  2302. struct wpa_driver_nl80211_data *drv = bss->drv;
  2303. int ifindex;
  2304. struct nl_msg *msg = NULL;
  2305. int ret;
  2306. int tdls = 0;
  2307. /* Ignore for P2P Device */
  2308. if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
  2309. return 0;
  2310. ifindex = if_nametoindex(ifname);
  2311. wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
  2312. "set_tx=%d seq_len=%lu key_len=%lu",
  2313. __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
  2314. (unsigned long) seq_len, (unsigned long) key_len);
  2315. #ifdef CONFIG_TDLS
  2316. if (key_idx == -1) {
  2317. key_idx = 0;
  2318. tdls = 1;
  2319. }
  2320. #endif /* CONFIG_TDLS */
  2321. #ifdef CONFIG_DRIVER_NL80211_QCA
  2322. if (alg == WPA_ALG_PMK &&
  2323. (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
  2324. wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
  2325. __func__);
  2326. ret = issue_key_mgmt_set_key(drv, key, key_len);
  2327. return ret;
  2328. }
  2329. #endif /* CONFIG_DRIVER_NL80211_QCA */
  2330. if (alg == WPA_ALG_NONE) {
  2331. msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
  2332. if (!msg)
  2333. return -ENOBUFS;
  2334. } else {
  2335. u32 suite;
  2336. suite = wpa_alg_to_cipher_suite(alg, key_len);
  2337. if (!suite)
  2338. goto fail;
  2339. msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
  2340. if (!msg ||
  2341. nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
  2342. nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER, suite))
  2343. goto fail;
  2344. wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
  2345. }
  2346. if (seq && seq_len) {
  2347. if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
  2348. goto fail;
  2349. wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
  2350. }
  2351. if (addr && !is_broadcast_ether_addr(addr)) {
  2352. wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
  2353. if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
  2354. goto fail;
  2355. if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
  2356. wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
  2357. if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
  2358. NL80211_KEYTYPE_GROUP))
  2359. goto fail;
  2360. }
  2361. } else if (addr && is_broadcast_ether_addr(addr)) {
  2362. struct nlattr *types;
  2363. wpa_printf(MSG_DEBUG, " broadcast key");
  2364. types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
  2365. if (!types ||
  2366. nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
  2367. goto fail;
  2368. nla_nest_end(msg, types);
  2369. }
  2370. if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
  2371. goto fail;
  2372. ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
  2373. if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
  2374. ret = 0;
  2375. if (ret)
  2376. wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
  2377. ret, strerror(-ret));
  2378. /*
  2379. * If we failed or don't need to set the default TX key (below),
  2380. * we're done here.
  2381. */
  2382. if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
  2383. return ret;
  2384. if (is_ap_interface(drv->nlmode) && addr &&
  2385. !is_broadcast_ether_addr(addr))
  2386. return ret;
  2387. msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
  2388. if (!msg ||
  2389. nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
  2390. nla_put_flag(msg, (alg == WPA_ALG_IGTK ||
  2391. alg == WPA_ALG_BIP_GMAC_128 ||
  2392. alg == WPA_ALG_BIP_GMAC_256 ||
  2393. alg == WPA_ALG_BIP_CMAC_256) ?
  2394. NL80211_ATTR_KEY_DEFAULT_MGMT :
  2395. NL80211_ATTR_KEY_DEFAULT))
  2396. goto fail;
  2397. if (addr && is_broadcast_ether_addr(addr)) {
  2398. struct nlattr *types;
  2399. types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
  2400. if (!types ||
  2401. nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
  2402. goto fail;
  2403. nla_nest_end(msg, types);
  2404. } else if (addr) {
  2405. struct nlattr *types;
  2406. types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
  2407. if (!types ||
  2408. nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
  2409. goto fail;
  2410. nla_nest_end(msg, types);
  2411. }
  2412. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2413. if (ret == -ENOENT)
  2414. ret = 0;
  2415. if (ret)
  2416. wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
  2417. "err=%d %s)", ret, strerror(-ret));
  2418. return ret;
  2419. fail:
  2420. nl80211_nlmsg_clear(msg);
  2421. nlmsg_free(msg);
  2422. return -ENOBUFS;
  2423. }
  2424. static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
  2425. int key_idx, int defkey,
  2426. const u8 *seq, size_t seq_len,
  2427. const u8 *key, size_t key_len)
  2428. {
  2429. struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
  2430. u32 suite;
  2431. if (!key_attr)
  2432. return -1;
  2433. suite = wpa_alg_to_cipher_suite(alg, key_len);
  2434. if (!suite)
  2435. return -1;
  2436. if (defkey && alg == WPA_ALG_IGTK) {
  2437. if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
  2438. return -1;
  2439. } else if (defkey) {
  2440. if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
  2441. return -1;
  2442. }
  2443. if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
  2444. nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
  2445. (seq && seq_len &&
  2446. nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
  2447. nla_put(msg, NL80211_KEY_DATA, key_len, key))
  2448. return -1;
  2449. nla_nest_end(msg, key_attr);
  2450. return 0;
  2451. }
  2452. static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
  2453. struct nl_msg *msg)
  2454. {
  2455. int i, privacy = 0;
  2456. struct nlattr *nl_keys, *nl_key;
  2457. for (i = 0; i < 4; i++) {
  2458. if (!params->wep_key[i])
  2459. continue;
  2460. privacy = 1;
  2461. break;
  2462. }
  2463. if (params->wps == WPS_MODE_PRIVACY)
  2464. privacy = 1;
  2465. if (params->pairwise_suite &&
  2466. params->pairwise_suite != WPA_CIPHER_NONE)
  2467. privacy = 1;
  2468. if (!privacy)
  2469. return 0;
  2470. if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
  2471. return -ENOBUFS;
  2472. nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
  2473. if (!nl_keys)
  2474. return -ENOBUFS;
  2475. for (i = 0; i < 4; i++) {
  2476. if (!params->wep_key[i])
  2477. continue;
  2478. nl_key = nla_nest_start(msg, i);
  2479. if (!nl_key ||
  2480. nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
  2481. params->wep_key[i]) ||
  2482. nla_put_u32(msg, NL80211_KEY_CIPHER,
  2483. params->wep_key_len[i] == 5 ?
  2484. RSN_CIPHER_SUITE_WEP40 :
  2485. RSN_CIPHER_SUITE_WEP104) ||
  2486. nla_put_u8(msg, NL80211_KEY_IDX, i) ||
  2487. (i == params->wep_tx_keyidx &&
  2488. nla_put_flag(msg, NL80211_KEY_DEFAULT)))
  2489. return -ENOBUFS;
  2490. nla_nest_end(msg, nl_key);
  2491. }
  2492. nla_nest_end(msg, nl_keys);
  2493. return 0;
  2494. }
  2495. int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
  2496. const u8 *addr, int cmd, u16 reason_code,
  2497. int local_state_change)
  2498. {
  2499. int ret;
  2500. struct nl_msg *msg;
  2501. if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
  2502. nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
  2503. (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
  2504. (local_state_change &&
  2505. nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
  2506. nlmsg_free(msg);
  2507. return -1;
  2508. }
  2509. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2510. if (ret) {
  2511. wpa_dbg(drv->ctx, MSG_DEBUG,
  2512. "nl80211: MLME command failed: reason=%u ret=%d (%s)",
  2513. reason_code, ret, strerror(-ret));
  2514. }
  2515. return ret;
  2516. }
  2517. static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
  2518. int reason_code)
  2519. {
  2520. int ret;
  2521. wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
  2522. nl80211_mark_disconnected(drv);
  2523. /* Disconnect command doesn't need BSSID - it uses cached value */
  2524. ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
  2525. reason_code, 0);
  2526. /*
  2527. * For locally generated disconnect, supplicant already generates a
  2528. * DEAUTH event, so ignore the event from NL80211.
  2529. */
  2530. drv->ignore_next_local_disconnect = ret == 0;
  2531. return ret;
  2532. }
  2533. static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
  2534. const u8 *addr, int reason_code)
  2535. {
  2536. struct wpa_driver_nl80211_data *drv = bss->drv;
  2537. int ret;
  2538. if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
  2539. nl80211_mark_disconnected(drv);
  2540. return nl80211_leave_ibss(drv, 1);
  2541. }
  2542. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
  2543. return wpa_driver_nl80211_disconnect(drv, reason_code);
  2544. wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
  2545. __func__, MAC2STR(addr), reason_code);
  2546. nl80211_mark_disconnected(drv);
  2547. ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
  2548. reason_code, 0);
  2549. /*
  2550. * For locally generated deauthenticate, supplicant already generates a
  2551. * DEAUTH event, so ignore the event from NL80211.
  2552. */
  2553. drv->ignore_next_local_deauth = ret == 0;
  2554. return ret;
  2555. }
  2556. static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
  2557. struct wpa_driver_auth_params *params)
  2558. {
  2559. int i;
  2560. drv->auth_freq = params->freq;
  2561. drv->auth_alg = params->auth_alg;
  2562. drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
  2563. drv->auth_local_state_change = params->local_state_change;
  2564. drv->auth_p2p = params->p2p;
  2565. if (params->bssid)
  2566. os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
  2567. else
  2568. os_memset(drv->auth_bssid_, 0, ETH_ALEN);
  2569. if (params->ssid) {
  2570. os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
  2571. drv->auth_ssid_len = params->ssid_len;
  2572. } else
  2573. drv->auth_ssid_len = 0;
  2574. os_free(drv->auth_ie);
  2575. drv->auth_ie = NULL;
  2576. drv->auth_ie_len = 0;
  2577. if (params->ie) {
  2578. drv->auth_ie = os_malloc(params->ie_len);
  2579. if (drv->auth_ie) {
  2580. os_memcpy(drv->auth_ie, params->ie, params->ie_len);
  2581. drv->auth_ie_len = params->ie_len;
  2582. }
  2583. }
  2584. for (i = 0; i < 4; i++) {
  2585. if (params->wep_key[i] && params->wep_key_len[i] &&
  2586. params->wep_key_len[i] <= 16) {
  2587. os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
  2588. params->wep_key_len[i]);
  2589. drv->auth_wep_key_len[i] = params->wep_key_len[i];
  2590. } else
  2591. drv->auth_wep_key_len[i] = 0;
  2592. }
  2593. }
  2594. static void nl80211_unmask_11b_rates(struct i802_bss *bss)
  2595. {
  2596. struct wpa_driver_nl80211_data *drv = bss->drv;
  2597. if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
  2598. return;
  2599. /*
  2600. * Looks like we failed to unmask 11b rates previously. This could
  2601. * happen, e.g., if the interface was down at the point in time when a
  2602. * P2P group was terminated.
  2603. */
  2604. wpa_printf(MSG_DEBUG,
  2605. "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
  2606. bss->ifname);
  2607. nl80211_disable_11b_rates(drv, drv->ifindex, 0);
  2608. }
  2609. static int wpa_driver_nl80211_authenticate(
  2610. struct i802_bss *bss, struct wpa_driver_auth_params *params)
  2611. {
  2612. struct wpa_driver_nl80211_data *drv = bss->drv;
  2613. int ret = -1, i;
  2614. struct nl_msg *msg;
  2615. enum nl80211_auth_type type;
  2616. enum nl80211_iftype nlmode;
  2617. int count = 0;
  2618. int is_retry;
  2619. nl80211_unmask_11b_rates(bss);
  2620. is_retry = drv->retry_auth;
  2621. drv->retry_auth = 0;
  2622. drv->ignore_deauth_event = 0;
  2623. nl80211_mark_disconnected(drv);
  2624. os_memset(drv->auth_bssid, 0, ETH_ALEN);
  2625. if (params->bssid)
  2626. os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
  2627. else
  2628. os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
  2629. /* FIX: IBSS mode */
  2630. nlmode = params->p2p ?
  2631. NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
  2632. if (drv->nlmode != nlmode &&
  2633. wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
  2634. return -1;
  2635. retry:
  2636. wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
  2637. drv->ifindex);
  2638. msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
  2639. if (!msg)
  2640. goto fail;
  2641. for (i = 0; i < 4; i++) {
  2642. if (!params->wep_key[i])
  2643. continue;
  2644. wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
  2645. NULL, i,
  2646. i == params->wep_tx_keyidx, NULL, 0,
  2647. params->wep_key[i],
  2648. params->wep_key_len[i]);
  2649. if (params->wep_tx_keyidx != i)
  2650. continue;
  2651. if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
  2652. params->wep_key[i], params->wep_key_len[i]))
  2653. goto fail;
  2654. }
  2655. if (params->bssid) {
  2656. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  2657. MAC2STR(params->bssid));
  2658. if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
  2659. goto fail;
  2660. }
  2661. if (params->freq) {
  2662. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  2663. if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
  2664. goto fail;
  2665. }
  2666. if (params->ssid) {
  2667. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  2668. params->ssid, params->ssid_len);
  2669. if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
  2670. params->ssid))
  2671. goto fail;
  2672. }
  2673. wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
  2674. if (params->ie &&
  2675. nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
  2676. goto fail;
  2677. if (params->auth_data) {
  2678. wpa_hexdump(MSG_DEBUG, " * auth_data", params->auth_data,
  2679. params->auth_data_len);
  2680. if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->auth_data_len,
  2681. params->auth_data))
  2682. goto fail;
  2683. }
  2684. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  2685. type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  2686. else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  2687. type = NL80211_AUTHTYPE_SHARED_KEY;
  2688. else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  2689. type = NL80211_AUTHTYPE_NETWORK_EAP;
  2690. else if (params->auth_alg & WPA_AUTH_ALG_FT)
  2691. type = NL80211_AUTHTYPE_FT;
  2692. else if (params->auth_alg & WPA_AUTH_ALG_SAE)
  2693. type = NL80211_AUTHTYPE_SAE;
  2694. else if (params->auth_alg & WPA_AUTH_ALG_FILS)
  2695. type = NL80211_AUTHTYPE_FILS_SK;
  2696. else if (params->auth_alg & WPA_AUTH_ALG_FILS_SK_PFS)
  2697. type = NL80211_AUTHTYPE_FILS_SK_PFS;
  2698. else
  2699. goto fail;
  2700. wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
  2701. if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
  2702. goto fail;
  2703. if (params->local_state_change) {
  2704. wpa_printf(MSG_DEBUG, " * Local state change only");
  2705. if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
  2706. goto fail;
  2707. }
  2708. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2709. msg = NULL;
  2710. if (ret) {
  2711. wpa_dbg(drv->ctx, MSG_DEBUG,
  2712. "nl80211: MLME command failed (auth): ret=%d (%s)",
  2713. ret, strerror(-ret));
  2714. count++;
  2715. if (ret == -EALREADY && count == 1 && params->bssid &&
  2716. !params->local_state_change) {
  2717. /*
  2718. * mac80211 does not currently accept new
  2719. * authentication if we are already authenticated. As a
  2720. * workaround, force deauthentication and try again.
  2721. */
  2722. wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
  2723. "after forced deauthentication");
  2724. drv->ignore_deauth_event = 1;
  2725. wpa_driver_nl80211_deauthenticate(
  2726. bss, params->bssid,
  2727. WLAN_REASON_PREV_AUTH_NOT_VALID);
  2728. nlmsg_free(msg);
  2729. goto retry;
  2730. }
  2731. if (ret == -ENOENT && params->freq && !is_retry) {
  2732. /*
  2733. * cfg80211 has likely expired the BSS entry even
  2734. * though it was previously available in our internal
  2735. * BSS table. To recover quickly, start a single
  2736. * channel scan on the specified channel.
  2737. */
  2738. struct wpa_driver_scan_params scan;
  2739. int freqs[2];
  2740. os_memset(&scan, 0, sizeof(scan));
  2741. scan.num_ssids = 1;
  2742. if (params->ssid) {
  2743. scan.ssids[0].ssid = params->ssid;
  2744. scan.ssids[0].ssid_len = params->ssid_len;
  2745. }
  2746. freqs[0] = params->freq;
  2747. freqs[1] = 0;
  2748. scan.freqs = freqs;
  2749. wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
  2750. "channel scan to refresh cfg80211 BSS "
  2751. "entry");
  2752. ret = wpa_driver_nl80211_scan(bss, &scan);
  2753. if (ret == 0) {
  2754. nl80211_copy_auth_params(drv, params);
  2755. drv->scan_for_auth = 1;
  2756. }
  2757. } else if (is_retry) {
  2758. /*
  2759. * Need to indicate this with an event since the return
  2760. * value from the retry is not delivered to core code.
  2761. */
  2762. union wpa_event_data event;
  2763. wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
  2764. "failed");
  2765. os_memset(&event, 0, sizeof(event));
  2766. os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
  2767. ETH_ALEN);
  2768. wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
  2769. &event);
  2770. }
  2771. } else {
  2772. wpa_printf(MSG_DEBUG,
  2773. "nl80211: Authentication request send successfully");
  2774. }
  2775. fail:
  2776. nlmsg_free(msg);
  2777. return ret;
  2778. }
  2779. int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
  2780. {
  2781. struct wpa_driver_auth_params params;
  2782. struct i802_bss *bss = drv->first_bss;
  2783. int i;
  2784. wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
  2785. os_memset(&params, 0, sizeof(params));
  2786. params.freq = drv->auth_freq;
  2787. params.auth_alg = drv->auth_alg;
  2788. params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
  2789. params.local_state_change = drv->auth_local_state_change;
  2790. params.p2p = drv->auth_p2p;
  2791. if (!is_zero_ether_addr(drv->auth_bssid_))
  2792. params.bssid = drv->auth_bssid_;
  2793. if (drv->auth_ssid_len) {
  2794. params.ssid = drv->auth_ssid;
  2795. params.ssid_len = drv->auth_ssid_len;
  2796. }
  2797. params.ie = drv->auth_ie;
  2798. params.ie_len = drv->auth_ie_len;
  2799. for (i = 0; i < 4; i++) {
  2800. if (drv->auth_wep_key_len[i]) {
  2801. params.wep_key[i] = drv->auth_wep_key[i];
  2802. params.wep_key_len[i] = drv->auth_wep_key_len[i];
  2803. }
  2804. }
  2805. drv->retry_auth = 1;
  2806. return wpa_driver_nl80211_authenticate(bss, &params);
  2807. }
  2808. static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
  2809. const void *data, size_t len,
  2810. int encrypt, int noack,
  2811. unsigned int freq, int no_cck,
  2812. int offchanok, unsigned int wait_time,
  2813. const u16 *csa_offs,
  2814. size_t csa_offs_len)
  2815. {
  2816. struct wpa_driver_nl80211_data *drv = bss->drv;
  2817. u64 cookie;
  2818. int res;
  2819. if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
  2820. freq = nl80211_get_assoc_freq(drv);
  2821. wpa_printf(MSG_DEBUG,
  2822. "nl80211: send_frame - Use assoc_freq=%u for IBSS",
  2823. freq);
  2824. }
  2825. if (freq == 0) {
  2826. wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
  2827. bss->freq);
  2828. freq = bss->freq;
  2829. }
  2830. if (drv->use_monitor) {
  2831. wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
  2832. freq, bss->freq);
  2833. return nl80211_send_monitor(drv, data, len, encrypt, noack);
  2834. }
  2835. wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
  2836. res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
  2837. &cookie, no_cck, noack, offchanok,
  2838. csa_offs, csa_offs_len);
  2839. if (res == 0 && !noack) {
  2840. const struct ieee80211_mgmt *mgmt;
  2841. u16 fc;
  2842. mgmt = (const struct ieee80211_mgmt *) data;
  2843. fc = le_to_host16(mgmt->frame_control);
  2844. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  2845. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
  2846. wpa_printf(MSG_MSGDUMP,
  2847. "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
  2848. (long long unsigned int)
  2849. drv->send_action_cookie,
  2850. (long long unsigned int) cookie);
  2851. drv->send_action_cookie = cookie;
  2852. }
  2853. }
  2854. return res;
  2855. }
  2856. static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
  2857. size_t data_len, int noack,
  2858. unsigned int freq, int no_cck,
  2859. int offchanok,
  2860. unsigned int wait_time,
  2861. const u16 *csa_offs,
  2862. size_t csa_offs_len)
  2863. {
  2864. struct wpa_driver_nl80211_data *drv = bss->drv;
  2865. struct ieee80211_mgmt *mgmt;
  2866. int encrypt = 1;
  2867. u16 fc;
  2868. mgmt = (struct ieee80211_mgmt *) data;
  2869. fc = le_to_host16(mgmt->frame_control);
  2870. wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
  2871. " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
  2872. MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
  2873. fc, fc2str(fc), drv->nlmode);
  2874. if ((is_sta_interface(drv->nlmode) ||
  2875. drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
  2876. WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  2877. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
  2878. /*
  2879. * The use of last_mgmt_freq is a bit of a hack,
  2880. * but it works due to the single-threaded nature
  2881. * of wpa_supplicant.
  2882. */
  2883. if (freq == 0) {
  2884. wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
  2885. drv->last_mgmt_freq);
  2886. freq = drv->last_mgmt_freq;
  2887. }
  2888. return nl80211_send_frame_cmd(bss, freq, 0,
  2889. data, data_len, NULL, 1, noack,
  2890. 1, csa_offs, csa_offs_len);
  2891. }
  2892. if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
  2893. if (freq == 0) {
  2894. wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
  2895. bss->freq);
  2896. freq = bss->freq;
  2897. }
  2898. return nl80211_send_frame_cmd(bss, freq,
  2899. (int) freq == bss->freq ? 0 :
  2900. wait_time,
  2901. data, data_len,
  2902. &drv->send_action_cookie,
  2903. no_cck, noack, offchanok,
  2904. csa_offs, csa_offs_len);
  2905. }
  2906. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  2907. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
  2908. /*
  2909. * Only one of the authentication frame types is encrypted.
  2910. * In order for static WEP encryption to work properly (i.e.,
  2911. * to not encrypt the frame), we need to tell mac80211 about
  2912. * the frames that must not be encrypted.
  2913. */
  2914. u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  2915. u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
  2916. if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
  2917. encrypt = 0;
  2918. }
  2919. wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
  2920. return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
  2921. noack, freq, no_cck, offchanok,
  2922. wait_time, csa_offs,
  2923. csa_offs_len);
  2924. }
  2925. static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
  2926. {
  2927. u8 rates[NL80211_MAX_SUPP_RATES];
  2928. u8 rates_len = 0;
  2929. int i;
  2930. if (!basic_rates)
  2931. return 0;
  2932. for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
  2933. rates[rates_len++] = basic_rates[i] / 5;
  2934. return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
  2935. }
  2936. static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
  2937. int slot, int ht_opmode, int ap_isolate,
  2938. const int *basic_rates)
  2939. {
  2940. struct wpa_driver_nl80211_data *drv = bss->drv;
  2941. struct nl_msg *msg;
  2942. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
  2943. (cts >= 0 &&
  2944. nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
  2945. (preamble >= 0 &&
  2946. nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
  2947. (slot >= 0 &&
  2948. nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
  2949. (ht_opmode >= 0 &&
  2950. nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
  2951. (ap_isolate >= 0 &&
  2952. nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
  2953. nl80211_put_basic_rates(msg, basic_rates)) {
  2954. nlmsg_free(msg);
  2955. return -ENOBUFS;
  2956. }
  2957. return send_and_recv_msgs(drv, msg, NULL, NULL);
  2958. }
  2959. static int wpa_driver_nl80211_set_acl(void *priv,
  2960. struct hostapd_acl_params *params)
  2961. {
  2962. struct i802_bss *bss = priv;
  2963. struct wpa_driver_nl80211_data *drv = bss->drv;
  2964. struct nl_msg *msg;
  2965. struct nl_msg *acl;
  2966. unsigned int i;
  2967. int ret;
  2968. if (!(drv->capa.max_acl_mac_addrs))
  2969. return -ENOTSUP;
  2970. if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
  2971. return -ENOTSUP;
  2972. wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
  2973. params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
  2974. acl = nlmsg_alloc();
  2975. if (!acl)
  2976. return -ENOMEM;
  2977. for (i = 0; i < params->num_mac_acl; i++) {
  2978. if (nla_put(acl, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
  2979. nlmsg_free(acl);
  2980. return -ENOMEM;
  2981. }
  2982. }
  2983. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
  2984. nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
  2985. NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
  2986. NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
  2987. nla_put_nested(msg, NL80211_ATTR_MAC_ADDRS, acl)) {
  2988. nlmsg_free(msg);
  2989. nlmsg_free(acl);
  2990. return -ENOMEM;
  2991. }
  2992. nlmsg_free(acl);
  2993. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2994. if (ret) {
  2995. wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
  2996. ret, strerror(-ret));
  2997. }
  2998. return ret;
  2999. }
  3000. static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
  3001. {
  3002. if (beacon_int > 0) {
  3003. wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
  3004. return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
  3005. beacon_int);
  3006. }
  3007. return 0;
  3008. }
  3009. static int nl80211_put_dtim_period(struct nl_msg *msg, int dtim_period)
  3010. {
  3011. if (dtim_period > 0) {
  3012. wpa_printf(MSG_DEBUG, " * dtim_period=%d", dtim_period);
  3013. return nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
  3014. }
  3015. return 0;
  3016. }
  3017. #ifdef CONFIG_MESH
  3018. static int nl80211_set_mesh_config(void *priv,
  3019. struct wpa_driver_mesh_bss_params *params)
  3020. {
  3021. struct i802_bss *bss = priv;
  3022. struct wpa_driver_nl80211_data *drv = bss->drv;
  3023. struct nl_msg *msg;
  3024. int ret;
  3025. msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MESH_CONFIG);
  3026. if (!msg)
  3027. return -1;
  3028. ret = nl80211_put_mesh_config(msg, params);
  3029. if (ret < 0) {
  3030. nlmsg_free(msg);
  3031. return ret;
  3032. }
  3033. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3034. if (ret) {
  3035. wpa_printf(MSG_ERROR,
  3036. "nl80211: Mesh config set failed: %d (%s)",
  3037. ret, strerror(-ret));
  3038. return ret;
  3039. }
  3040. return 0;
  3041. }
  3042. #endif /* CONFIG_MESH */
  3043. static int nl80211_put_beacon_rate(struct nl_msg *msg, const u64 flags,
  3044. struct wpa_driver_ap_params *params)
  3045. {
  3046. struct nlattr *bands, *band;
  3047. struct nl80211_txrate_vht vht_rate;
  3048. if (!params->freq ||
  3049. (params->beacon_rate == 0 &&
  3050. params->rate_type == BEACON_RATE_LEGACY))
  3051. return 0;
  3052. bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
  3053. if (!bands)
  3054. return -1;
  3055. switch (params->freq->mode) {
  3056. case HOSTAPD_MODE_IEEE80211B:
  3057. case HOSTAPD_MODE_IEEE80211G:
  3058. band = nla_nest_start(msg, NL80211_BAND_2GHZ);
  3059. break;
  3060. case HOSTAPD_MODE_IEEE80211A:
  3061. band = nla_nest_start(msg, NL80211_BAND_5GHZ);
  3062. break;
  3063. case HOSTAPD_MODE_IEEE80211AD:
  3064. band = nla_nest_start(msg, NL80211_BAND_60GHZ);
  3065. break;
  3066. default:
  3067. return 0;
  3068. }
  3069. if (!band)
  3070. return -1;
  3071. os_memset(&vht_rate, 0, sizeof(vht_rate));
  3072. switch (params->rate_type) {
  3073. case BEACON_RATE_LEGACY:
  3074. if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_LEGACY)) {
  3075. wpa_printf(MSG_INFO,
  3076. "nl80211: Driver does not support setting Beacon frame rate (legacy)");
  3077. return -1;
  3078. }
  3079. if (nla_put_u8(msg, NL80211_TXRATE_LEGACY,
  3080. (u8) params->beacon_rate / 5) ||
  3081. nla_put(msg, NL80211_TXRATE_HT, 0, NULL) ||
  3082. (params->freq->vht_enabled &&
  3083. nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
  3084. &vht_rate)))
  3085. return -1;
  3086. wpa_printf(MSG_DEBUG, " * beacon_rate = legacy:%u (* 100 kbps)",
  3087. params->beacon_rate);
  3088. break;
  3089. case BEACON_RATE_HT:
  3090. if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_HT)) {
  3091. wpa_printf(MSG_INFO,
  3092. "nl80211: Driver does not support setting Beacon frame rate (HT)");
  3093. return -1;
  3094. }
  3095. if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL) ||
  3096. nla_put_u8(msg, NL80211_TXRATE_HT, params->beacon_rate) ||
  3097. (params->freq->vht_enabled &&
  3098. nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
  3099. &vht_rate)))
  3100. return -1;
  3101. wpa_printf(MSG_DEBUG, " * beacon_rate = HT-MCS %u",
  3102. params->beacon_rate);
  3103. break;
  3104. case BEACON_RATE_VHT:
  3105. if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_VHT)) {
  3106. wpa_printf(MSG_INFO,
  3107. "nl80211: Driver does not support setting Beacon frame rate (VHT)");
  3108. return -1;
  3109. }
  3110. vht_rate.mcs[0] = BIT(params->beacon_rate);
  3111. if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL))
  3112. return -1;
  3113. if (nla_put(msg, NL80211_TXRATE_HT, 0, NULL))
  3114. return -1;
  3115. if (nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
  3116. &vht_rate))
  3117. return -1;
  3118. wpa_printf(MSG_DEBUG, " * beacon_rate = VHT-MCS %u",
  3119. params->beacon_rate);
  3120. break;
  3121. }
  3122. nla_nest_end(msg, band);
  3123. nla_nest_end(msg, bands);
  3124. return 0;
  3125. }
  3126. static int nl80211_set_multicast_to_unicast(struct i802_bss *bss,
  3127. int multicast_to_unicast)
  3128. {
  3129. struct wpa_driver_nl80211_data *drv = bss->drv;
  3130. struct nl_msg *msg;
  3131. int ret;
  3132. msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_MULTICAST_TO_UNICAST);
  3133. if (!msg ||
  3134. (multicast_to_unicast &&
  3135. nla_put_flag(msg, NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED))) {
  3136. wpa_printf(MSG_ERROR,
  3137. "nl80211: Failed to build NL80211_CMD_SET_MULTICAST_TO_UNICAST msg for %s",
  3138. bss->ifname);
  3139. nlmsg_free(msg);
  3140. return -ENOBUFS;
  3141. }
  3142. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3143. switch (ret) {
  3144. case 0:
  3145. wpa_printf(MSG_DEBUG,
  3146. "nl80211: multicast to unicast %s on interface %s",
  3147. multicast_to_unicast ? "enabled" : "disabled",
  3148. bss->ifname);
  3149. break;
  3150. case -EOPNOTSUPP:
  3151. if (!multicast_to_unicast)
  3152. break;
  3153. wpa_printf(MSG_INFO,
  3154. "nl80211: multicast to unicast not supported on interface %s",
  3155. bss->ifname);
  3156. break;
  3157. default:
  3158. wpa_printf(MSG_ERROR,
  3159. "nl80211: %s multicast to unicast failed with %d (%s) on interface %s",
  3160. multicast_to_unicast ? "enabling" : "disabling",
  3161. ret, strerror(-ret), bss->ifname);
  3162. break;
  3163. }
  3164. return ret;
  3165. }
  3166. static int wpa_driver_nl80211_set_ap(void *priv,
  3167. struct wpa_driver_ap_params *params)
  3168. {
  3169. struct i802_bss *bss = priv;
  3170. struct wpa_driver_nl80211_data *drv = bss->drv;
  3171. struct nl_msg *msg;
  3172. u8 cmd = NL80211_CMD_NEW_BEACON;
  3173. int ret;
  3174. int beacon_set;
  3175. int num_suites;
  3176. int smps_mode;
  3177. u32 suites[10], suite;
  3178. u32 ver;
  3179. #ifdef CONFIG_MESH
  3180. struct wpa_driver_mesh_bss_params mesh_params;
  3181. #endif /* CONFIG_MESH */
  3182. beacon_set = params->reenable ? 0 : bss->beacon_set;
  3183. wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
  3184. beacon_set);
  3185. if (beacon_set)
  3186. cmd = NL80211_CMD_SET_BEACON;
  3187. else if (!drv->device_ap_sme && !drv->use_monitor &&
  3188. !nl80211_get_wiphy_data_ap(bss))
  3189. return -ENOBUFS;
  3190. wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
  3191. params->head, params->head_len);
  3192. wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
  3193. params->tail, params->tail_len);
  3194. wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
  3195. wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
  3196. wpa_printf(MSG_DEBUG, "nl80211: beacon_rate=%u", params->beacon_rate);
  3197. wpa_printf(MSG_DEBUG, "nl80211: rate_type=%d", params->rate_type);
  3198. wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
  3199. wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
  3200. params->ssid, params->ssid_len);
  3201. if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
  3202. nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
  3203. params->head) ||
  3204. nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
  3205. params->tail) ||
  3206. nl80211_put_beacon_int(msg, params->beacon_int) ||
  3207. nl80211_put_beacon_rate(msg, drv->capa.flags, params) ||
  3208. nl80211_put_dtim_period(msg, params->dtim_period) ||
  3209. nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
  3210. goto fail;
  3211. if (params->proberesp && params->proberesp_len) {
  3212. wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
  3213. params->proberesp, params->proberesp_len);
  3214. if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
  3215. params->proberesp))
  3216. goto fail;
  3217. }
  3218. switch (params->hide_ssid) {
  3219. case NO_SSID_HIDING:
  3220. wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
  3221. if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
  3222. NL80211_HIDDEN_SSID_NOT_IN_USE))
  3223. goto fail;
  3224. break;
  3225. case HIDDEN_SSID_ZERO_LEN:
  3226. wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
  3227. if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
  3228. NL80211_HIDDEN_SSID_ZERO_LEN))
  3229. goto fail;
  3230. break;
  3231. case HIDDEN_SSID_ZERO_CONTENTS:
  3232. wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
  3233. if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
  3234. NL80211_HIDDEN_SSID_ZERO_CONTENTS))
  3235. goto fail;
  3236. break;
  3237. }
  3238. wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
  3239. if (params->privacy &&
  3240. nla_put_flag(msg, NL80211_ATTR_PRIVACY))
  3241. goto fail;
  3242. wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
  3243. if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
  3244. (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
  3245. /* Leave out the attribute */
  3246. } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
  3247. if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
  3248. NL80211_AUTHTYPE_SHARED_KEY))
  3249. goto fail;
  3250. } else {
  3251. if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
  3252. NL80211_AUTHTYPE_OPEN_SYSTEM))
  3253. goto fail;
  3254. }
  3255. wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
  3256. ver = 0;
  3257. if (params->wpa_version & WPA_PROTO_WPA)
  3258. ver |= NL80211_WPA_VERSION_1;
  3259. if (params->wpa_version & WPA_PROTO_RSN)
  3260. ver |= NL80211_WPA_VERSION_2;
  3261. if (ver &&
  3262. nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
  3263. goto fail;
  3264. wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
  3265. params->key_mgmt_suites);
  3266. num_suites = 0;
  3267. if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
  3268. suites[num_suites++] = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
  3269. if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
  3270. suites[num_suites++] = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
  3271. if (num_suites &&
  3272. nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
  3273. suites))
  3274. goto fail;
  3275. if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  3276. (!params->pairwise_ciphers ||
  3277. params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40)) &&
  3278. (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
  3279. nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
  3280. goto fail;
  3281. wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
  3282. params->pairwise_ciphers);
  3283. num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
  3284. suites, ARRAY_SIZE(suites));
  3285. if (num_suites &&
  3286. nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
  3287. num_suites * sizeof(u32), suites))
  3288. goto fail;
  3289. wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
  3290. params->group_cipher);
  3291. suite = wpa_cipher_to_cipher_suite(params->group_cipher);
  3292. if (suite &&
  3293. nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
  3294. goto fail;
  3295. if (params->ht_opmode != -1) {
  3296. switch (params->smps_mode) {
  3297. case HT_CAP_INFO_SMPS_DYNAMIC:
  3298. wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
  3299. smps_mode = NL80211_SMPS_DYNAMIC;
  3300. break;
  3301. case HT_CAP_INFO_SMPS_STATIC:
  3302. wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
  3303. smps_mode = NL80211_SMPS_STATIC;
  3304. break;
  3305. default:
  3306. /* invalid - fallback to smps off */
  3307. case HT_CAP_INFO_SMPS_DISABLED:
  3308. wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
  3309. smps_mode = NL80211_SMPS_OFF;
  3310. break;
  3311. }
  3312. if (nla_put_u32(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
  3313. goto fail;
  3314. }
  3315. if (params->beacon_ies) {
  3316. wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
  3317. params->beacon_ies);
  3318. if (nla_put(msg, NL80211_ATTR_IE,
  3319. wpabuf_len(params->beacon_ies),
  3320. wpabuf_head(params->beacon_ies)))
  3321. goto fail;
  3322. }
  3323. if (params->proberesp_ies) {
  3324. wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
  3325. params->proberesp_ies);
  3326. if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
  3327. wpabuf_len(params->proberesp_ies),
  3328. wpabuf_head(params->proberesp_ies)))
  3329. goto fail;
  3330. }
  3331. if (params->assocresp_ies) {
  3332. wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
  3333. params->assocresp_ies);
  3334. if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
  3335. wpabuf_len(params->assocresp_ies),
  3336. wpabuf_head(params->assocresp_ies)))
  3337. goto fail;
  3338. }
  3339. if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
  3340. wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
  3341. params->ap_max_inactivity);
  3342. if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
  3343. params->ap_max_inactivity))
  3344. goto fail;
  3345. }
  3346. #ifdef CONFIG_P2P
  3347. if (params->p2p_go_ctwindow > 0) {
  3348. if (drv->p2p_go_ctwindow_supported) {
  3349. wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
  3350. params->p2p_go_ctwindow);
  3351. if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
  3352. params->p2p_go_ctwindow))
  3353. goto fail;
  3354. } else {
  3355. wpa_printf(MSG_INFO,
  3356. "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
  3357. }
  3358. }
  3359. #endif /* CONFIG_P2P */
  3360. if (params->pbss) {
  3361. wpa_printf(MSG_DEBUG, "nl80211: PBSS");
  3362. if (nla_put_flag(msg, NL80211_ATTR_PBSS))
  3363. goto fail;
  3364. }
  3365. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3366. if (ret) {
  3367. wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
  3368. ret, strerror(-ret));
  3369. } else {
  3370. bss->beacon_set = 1;
  3371. nl80211_set_bss(bss, params->cts_protect, params->preamble,
  3372. params->short_slot_time, params->ht_opmode,
  3373. params->isolate, params->basic_rates);
  3374. nl80211_set_multicast_to_unicast(bss,
  3375. params->multicast_to_unicast);
  3376. if (beacon_set && params->freq &&
  3377. params->freq->bandwidth != bss->bandwidth) {
  3378. wpa_printf(MSG_DEBUG,
  3379. "nl80211: Update BSS %s bandwidth: %d -> %d",
  3380. bss->ifname, bss->bandwidth,
  3381. params->freq->bandwidth);
  3382. ret = nl80211_set_channel(bss, params->freq, 1);
  3383. if (ret) {
  3384. wpa_printf(MSG_DEBUG,
  3385. "nl80211: Frequency set failed: %d (%s)",
  3386. ret, strerror(-ret));
  3387. } else {
  3388. wpa_printf(MSG_DEBUG,
  3389. "nl80211: Frequency set succeeded for ht2040 coex");
  3390. bss->bandwidth = params->freq->bandwidth;
  3391. }
  3392. } else if (!beacon_set && params->freq) {
  3393. /*
  3394. * cfg80211 updates the driver on frequence change in AP
  3395. * mode only at the point when beaconing is started, so
  3396. * set the initial value here.
  3397. */
  3398. bss->bandwidth = params->freq->bandwidth;
  3399. }
  3400. }
  3401. #ifdef CONFIG_MESH
  3402. if (is_mesh_interface(drv->nlmode) && params->ht_opmode != -1) {
  3403. os_memset(&mesh_params, 0, sizeof(mesh_params));
  3404. mesh_params.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
  3405. mesh_params.ht_opmode = params->ht_opmode;
  3406. ret = nl80211_set_mesh_config(priv, &mesh_params);
  3407. if (ret < 0)
  3408. return ret;
  3409. }
  3410. #endif /* CONFIG_MESH */
  3411. return ret;
  3412. fail:
  3413. nlmsg_free(msg);
  3414. return -ENOBUFS;
  3415. }
  3416. static int nl80211_put_freq_params(struct nl_msg *msg,
  3417. const struct hostapd_freq_params *freq)
  3418. {
  3419. wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
  3420. if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
  3421. return -ENOBUFS;
  3422. wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
  3423. wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
  3424. if (freq->vht_enabled) {
  3425. enum nl80211_chan_width cw;
  3426. wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
  3427. switch (freq->bandwidth) {
  3428. case 20:
  3429. cw = NL80211_CHAN_WIDTH_20;
  3430. break;
  3431. case 40:
  3432. cw = NL80211_CHAN_WIDTH_40;
  3433. break;
  3434. case 80:
  3435. if (freq->center_freq2)
  3436. cw = NL80211_CHAN_WIDTH_80P80;
  3437. else
  3438. cw = NL80211_CHAN_WIDTH_80;
  3439. break;
  3440. case 160:
  3441. cw = NL80211_CHAN_WIDTH_160;
  3442. break;
  3443. default:
  3444. return -EINVAL;
  3445. }
  3446. wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
  3447. wpa_printf(MSG_DEBUG, " * center_freq1=%d",
  3448. freq->center_freq1);
  3449. wpa_printf(MSG_DEBUG, " * center_freq2=%d",
  3450. freq->center_freq2);
  3451. if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
  3452. nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
  3453. freq->center_freq1) ||
  3454. (freq->center_freq2 &&
  3455. nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
  3456. freq->center_freq2)))
  3457. return -ENOBUFS;
  3458. } else if (freq->ht_enabled) {
  3459. enum nl80211_channel_type ct;
  3460. wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
  3461. freq->sec_channel_offset);
  3462. switch (freq->sec_channel_offset) {
  3463. case -1:
  3464. ct = NL80211_CHAN_HT40MINUS;
  3465. break;
  3466. case 1:
  3467. ct = NL80211_CHAN_HT40PLUS;
  3468. break;
  3469. default:
  3470. ct = NL80211_CHAN_HT20;
  3471. break;
  3472. }
  3473. wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
  3474. if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
  3475. return -ENOBUFS;
  3476. } else {
  3477. wpa_printf(MSG_DEBUG, " * channel_type=%d",
  3478. NL80211_CHAN_NO_HT);
  3479. if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  3480. NL80211_CHAN_NO_HT))
  3481. return -ENOBUFS;
  3482. }
  3483. return 0;
  3484. }
  3485. static int nl80211_set_channel(struct i802_bss *bss,
  3486. struct hostapd_freq_params *freq, int set_chan)
  3487. {
  3488. struct wpa_driver_nl80211_data *drv = bss->drv;
  3489. struct nl_msg *msg;
  3490. int ret;
  3491. wpa_printf(MSG_DEBUG,
  3492. "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
  3493. freq->freq, freq->ht_enabled, freq->vht_enabled,
  3494. freq->bandwidth, freq->center_freq1, freq->center_freq2);
  3495. msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
  3496. NL80211_CMD_SET_WIPHY);
  3497. if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
  3498. nlmsg_free(msg);
  3499. return -1;
  3500. }
  3501. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3502. if (ret == 0) {
  3503. bss->freq = freq->freq;
  3504. return 0;
  3505. }
  3506. wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
  3507. "%d (%s)", freq->freq, ret, strerror(-ret));
  3508. return -1;
  3509. }
  3510. static u32 sta_flags_nl80211(int flags)
  3511. {
  3512. u32 f = 0;
  3513. if (flags & WPA_STA_AUTHORIZED)
  3514. f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
  3515. if (flags & WPA_STA_WMM)
  3516. f |= BIT(NL80211_STA_FLAG_WME);
  3517. if (flags & WPA_STA_SHORT_PREAMBLE)
  3518. f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
  3519. if (flags & WPA_STA_MFP)
  3520. f |= BIT(NL80211_STA_FLAG_MFP);
  3521. if (flags & WPA_STA_TDLS_PEER)
  3522. f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
  3523. if (flags & WPA_STA_AUTHENTICATED)
  3524. f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
  3525. if (flags & WPA_STA_ASSOCIATED)
  3526. f |= BIT(NL80211_STA_FLAG_ASSOCIATED);
  3527. return f;
  3528. }
  3529. #ifdef CONFIG_MESH
  3530. static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
  3531. {
  3532. switch (state) {
  3533. case PLINK_IDLE:
  3534. return NL80211_PLINK_LISTEN;
  3535. case PLINK_OPN_SNT:
  3536. return NL80211_PLINK_OPN_SNT;
  3537. case PLINK_OPN_RCVD:
  3538. return NL80211_PLINK_OPN_RCVD;
  3539. case PLINK_CNF_RCVD:
  3540. return NL80211_PLINK_CNF_RCVD;
  3541. case PLINK_ESTAB:
  3542. return NL80211_PLINK_ESTAB;
  3543. case PLINK_HOLDING:
  3544. return NL80211_PLINK_HOLDING;
  3545. case PLINK_BLOCKED:
  3546. return NL80211_PLINK_BLOCKED;
  3547. default:
  3548. wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
  3549. state);
  3550. }
  3551. return -1;
  3552. }
  3553. #endif /* CONFIG_MESH */
  3554. static int wpa_driver_nl80211_sta_add(void *priv,
  3555. struct hostapd_sta_add_params *params)
  3556. {
  3557. struct i802_bss *bss = priv;
  3558. struct wpa_driver_nl80211_data *drv = bss->drv;
  3559. struct nl_msg *msg;
  3560. struct nl80211_sta_flag_update upd;
  3561. int ret = -ENOBUFS;
  3562. if ((params->flags & WPA_STA_TDLS_PEER) &&
  3563. !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
  3564. return -EOPNOTSUPP;
  3565. wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
  3566. params->set ? "Set" : "Add", MAC2STR(params->addr));
  3567. msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
  3568. NL80211_CMD_NEW_STATION);
  3569. if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
  3570. goto fail;
  3571. /*
  3572. * Set the below properties only in one of the following cases:
  3573. * 1. New station is added, already associated.
  3574. * 2. Set WPA_STA_TDLS_PEER station.
  3575. * 3. Set an already added unassociated station, if driver supports
  3576. * full AP client state. (Set these properties after station became
  3577. * associated will be rejected by the driver).
  3578. */
  3579. if (!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
  3580. (params->set && FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
  3581. (params->flags & WPA_STA_ASSOCIATED))) {
  3582. wpa_hexdump(MSG_DEBUG, " * supported rates",
  3583. params->supp_rates, params->supp_rates_len);
  3584. wpa_printf(MSG_DEBUG, " * capability=0x%x",
  3585. params->capability);
  3586. if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
  3587. params->supp_rates_len, params->supp_rates) ||
  3588. nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
  3589. params->capability))
  3590. goto fail;
  3591. if (params->ht_capabilities) {
  3592. wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
  3593. (u8 *) params->ht_capabilities,
  3594. sizeof(*params->ht_capabilities));
  3595. if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
  3596. sizeof(*params->ht_capabilities),
  3597. params->ht_capabilities))
  3598. goto fail;
  3599. }
  3600. if (params->vht_capabilities) {
  3601. wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
  3602. (u8 *) params->vht_capabilities,
  3603. sizeof(*params->vht_capabilities));
  3604. if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
  3605. sizeof(*params->vht_capabilities),
  3606. params->vht_capabilities))
  3607. goto fail;
  3608. }
  3609. if (params->ext_capab) {
  3610. wpa_hexdump(MSG_DEBUG, " * ext_capab",
  3611. params->ext_capab, params->ext_capab_len);
  3612. if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
  3613. params->ext_capab_len, params->ext_capab))
  3614. goto fail;
  3615. }
  3616. if (is_ap_interface(drv->nlmode) &&
  3617. nla_put_u8(msg, NL80211_ATTR_STA_SUPPORT_P2P_PS,
  3618. params->support_p2p_ps ?
  3619. NL80211_P2P_PS_SUPPORTED :
  3620. NL80211_P2P_PS_UNSUPPORTED))
  3621. goto fail;
  3622. }
  3623. if (!params->set) {
  3624. if (params->aid) {
  3625. wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
  3626. if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
  3627. goto fail;
  3628. } else {
  3629. /*
  3630. * cfg80211 validates that AID is non-zero, so we have
  3631. * to make this a non-zero value for the TDLS case where
  3632. * a dummy STA entry is used for now and for a station
  3633. * that is still not associated.
  3634. */
  3635. wpa_printf(MSG_DEBUG, " * aid=1 (%s workaround)",
  3636. (params->flags & WPA_STA_TDLS_PEER) ?
  3637. "TDLS" : "UNASSOC_STA");
  3638. if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
  3639. goto fail;
  3640. }
  3641. wpa_printf(MSG_DEBUG, " * listen_interval=%u",
  3642. params->listen_interval);
  3643. if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
  3644. params->listen_interval))
  3645. goto fail;
  3646. } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
  3647. wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
  3648. if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
  3649. goto fail;
  3650. } else if (FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
  3651. (params->flags & WPA_STA_ASSOCIATED)) {
  3652. wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
  3653. wpa_printf(MSG_DEBUG, " * listen_interval=%u",
  3654. params->listen_interval);
  3655. if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid) ||
  3656. nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
  3657. params->listen_interval))
  3658. goto fail;
  3659. }
  3660. if (params->vht_opmode_enabled) {
  3661. wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
  3662. if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
  3663. params->vht_opmode))
  3664. goto fail;
  3665. }
  3666. if (params->supp_channels) {
  3667. wpa_hexdump(MSG_DEBUG, " * supported channels",
  3668. params->supp_channels, params->supp_channels_len);
  3669. if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
  3670. params->supp_channels_len, params->supp_channels))
  3671. goto fail;
  3672. }
  3673. if (params->supp_oper_classes) {
  3674. wpa_hexdump(MSG_DEBUG, " * supported operating classes",
  3675. params->supp_oper_classes,
  3676. params->supp_oper_classes_len);
  3677. if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
  3678. params->supp_oper_classes_len,
  3679. params->supp_oper_classes))
  3680. goto fail;
  3681. }
  3682. os_memset(&upd, 0, sizeof(upd));
  3683. upd.set = sta_flags_nl80211(params->flags);
  3684. upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
  3685. /*
  3686. * If the driver doesn't support full AP client state, ignore ASSOC/AUTH
  3687. * flags, as nl80211 driver moves a new station, by default, into
  3688. * associated state.
  3689. *
  3690. * On the other hand, if the driver supports that feature and the
  3691. * station is added in unauthenticated state, set the
  3692. * authenticated/associated bits in the mask to prevent moving this
  3693. * station to associated state before it is actually associated.
  3694. *
  3695. * This is irrelevant for mesh mode where the station is added to the
  3696. * driver as authenticated already, and ASSOCIATED isn't part of the
  3697. * nl80211 API.
  3698. */
  3699. if (!is_mesh_interface(drv->nlmode)) {
  3700. if (!FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) {
  3701. wpa_printf(MSG_DEBUG,
  3702. "nl80211: Ignore ASSOC/AUTH flags since driver doesn't support full AP client state");
  3703. upd.mask &= ~(BIT(NL80211_STA_FLAG_ASSOCIATED) |
  3704. BIT(NL80211_STA_FLAG_AUTHENTICATED));
  3705. } else if (!params->set &&
  3706. !(params->flags & WPA_STA_TDLS_PEER)) {
  3707. if (!(params->flags & WPA_STA_AUTHENTICATED))
  3708. upd.mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
  3709. if (!(params->flags & WPA_STA_ASSOCIATED))
  3710. upd.mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
  3711. }
  3712. #ifdef CONFIG_MESH
  3713. } else {
  3714. if (params->plink_state == PLINK_ESTAB && params->peer_aid) {
  3715. ret = nla_put_u16(msg, NL80211_ATTR_MESH_PEER_AID,
  3716. params->peer_aid);
  3717. if (ret)
  3718. goto fail;
  3719. }
  3720. #endif /* CONFIG_MESH */
  3721. }
  3722. wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
  3723. upd.set, upd.mask);
  3724. if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
  3725. goto fail;
  3726. #ifdef CONFIG_MESH
  3727. if (params->plink_state &&
  3728. nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
  3729. sta_plink_state_nl80211(params->plink_state)))
  3730. goto fail;
  3731. #endif /* CONFIG_MESH */
  3732. if (params->flags & WPA_STA_WMM) {
  3733. struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
  3734. wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
  3735. if (!wme ||
  3736. nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
  3737. params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
  3738. nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
  3739. (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
  3740. WMM_QOSINFO_STA_SP_MASK))
  3741. goto fail;
  3742. nla_nest_end(msg, wme);
  3743. }
  3744. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3745. msg = NULL;
  3746. if (ret)
  3747. wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
  3748. "result: %d (%s)", params->set ? "SET" : "NEW", ret,
  3749. strerror(-ret));
  3750. if (ret == -EEXIST)
  3751. ret = 0;
  3752. fail:
  3753. nlmsg_free(msg);
  3754. return ret;
  3755. }
  3756. static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
  3757. {
  3758. #ifdef CONFIG_LIBNL3_ROUTE
  3759. struct wpa_driver_nl80211_data *drv = bss->drv;
  3760. struct rtnl_neigh *rn;
  3761. struct nl_addr *nl_addr;
  3762. int err;
  3763. rn = rtnl_neigh_alloc();
  3764. if (!rn)
  3765. return;
  3766. rtnl_neigh_set_family(rn, AF_BRIDGE);
  3767. rtnl_neigh_set_ifindex(rn, bss->ifindex);
  3768. nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
  3769. if (!nl_addr) {
  3770. rtnl_neigh_put(rn);
  3771. return;
  3772. }
  3773. rtnl_neigh_set_lladdr(rn, nl_addr);
  3774. err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
  3775. if (err < 0) {
  3776. wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
  3777. MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
  3778. bss->ifindex, nl_geterror(err));
  3779. } else {
  3780. wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
  3781. MACSTR, MAC2STR(addr));
  3782. }
  3783. nl_addr_put(nl_addr);
  3784. rtnl_neigh_put(rn);
  3785. #endif /* CONFIG_LIBNL3_ROUTE */
  3786. }
  3787. static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
  3788. int deauth, u16 reason_code)
  3789. {
  3790. struct wpa_driver_nl80211_data *drv = bss->drv;
  3791. struct nl_msg *msg;
  3792. int ret;
  3793. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
  3794. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
  3795. (deauth == 0 &&
  3796. nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
  3797. WLAN_FC_STYPE_DISASSOC)) ||
  3798. (deauth == 1 &&
  3799. nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
  3800. WLAN_FC_STYPE_DEAUTH)) ||
  3801. (reason_code &&
  3802. nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
  3803. nlmsg_free(msg);
  3804. return -ENOBUFS;
  3805. }
  3806. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3807. wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
  3808. " --> %d (%s)",
  3809. bss->ifname, MAC2STR(addr), ret, strerror(-ret));
  3810. if (drv->rtnl_sk)
  3811. rtnl_neigh_delete_fdb_entry(bss, addr);
  3812. if (ret == -ENOENT)
  3813. return 0;
  3814. return ret;
  3815. }
  3816. void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
  3817. {
  3818. struct nl_msg *msg;
  3819. struct wpa_driver_nl80211_data *drv2;
  3820. wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
  3821. /* stop listening for EAPOL on this interface */
  3822. dl_list_for_each(drv2, &drv->global->interfaces,
  3823. struct wpa_driver_nl80211_data, list)
  3824. {
  3825. del_ifidx(drv2, ifidx, IFIDX_ANY);
  3826. /* Remove all bridges learned for this iface */
  3827. del_ifidx(drv2, IFIDX_ANY, ifidx);
  3828. }
  3829. msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
  3830. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  3831. return;
  3832. wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
  3833. }
  3834. const char * nl80211_iftype_str(enum nl80211_iftype mode)
  3835. {
  3836. switch (mode) {
  3837. case NL80211_IFTYPE_ADHOC:
  3838. return "ADHOC";
  3839. case NL80211_IFTYPE_STATION:
  3840. return "STATION";
  3841. case NL80211_IFTYPE_AP:
  3842. return "AP";
  3843. case NL80211_IFTYPE_AP_VLAN:
  3844. return "AP_VLAN";
  3845. case NL80211_IFTYPE_WDS:
  3846. return "WDS";
  3847. case NL80211_IFTYPE_MONITOR:
  3848. return "MONITOR";
  3849. case NL80211_IFTYPE_MESH_POINT:
  3850. return "MESH_POINT";
  3851. case NL80211_IFTYPE_P2P_CLIENT:
  3852. return "P2P_CLIENT";
  3853. case NL80211_IFTYPE_P2P_GO:
  3854. return "P2P_GO";
  3855. case NL80211_IFTYPE_P2P_DEVICE:
  3856. return "P2P_DEVICE";
  3857. default:
  3858. return "unknown";
  3859. }
  3860. }
  3861. static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
  3862. const char *ifname,
  3863. enum nl80211_iftype iftype,
  3864. const u8 *addr, int wds,
  3865. int (*handler)(struct nl_msg *, void *),
  3866. void *arg)
  3867. {
  3868. struct nl_msg *msg;
  3869. int ifidx;
  3870. int ret = -ENOBUFS;
  3871. wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
  3872. iftype, nl80211_iftype_str(iftype));
  3873. msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
  3874. if (!msg ||
  3875. nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
  3876. nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
  3877. goto fail;
  3878. if (iftype == NL80211_IFTYPE_MONITOR) {
  3879. struct nlattr *flags;
  3880. flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
  3881. if (!flags ||
  3882. nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
  3883. goto fail;
  3884. nla_nest_end(msg, flags);
  3885. } else if (wds) {
  3886. if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
  3887. goto fail;
  3888. }
  3889. /*
  3890. * Tell cfg80211 that the interface belongs to the socket that created
  3891. * it, and the interface should be deleted when the socket is closed.
  3892. */
  3893. if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
  3894. goto fail;
  3895. ret = send_and_recv_msgs(drv, msg, handler, arg);
  3896. msg = NULL;
  3897. if (ret) {
  3898. fail:
  3899. nlmsg_free(msg);
  3900. wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
  3901. ifname, ret, strerror(-ret));
  3902. return ret;
  3903. }
  3904. if (iftype == NL80211_IFTYPE_P2P_DEVICE)
  3905. return 0;
  3906. ifidx = if_nametoindex(ifname);
  3907. wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
  3908. ifname, ifidx);
  3909. if (ifidx <= 0)
  3910. return -1;
  3911. /*
  3912. * Some virtual interfaces need to process EAPOL packets and events on
  3913. * the parent interface. This is used mainly with hostapd.
  3914. */
  3915. if (drv->hostapd ||
  3916. iftype == NL80211_IFTYPE_AP_VLAN ||
  3917. iftype == NL80211_IFTYPE_WDS ||
  3918. iftype == NL80211_IFTYPE_MONITOR) {
  3919. /* start listening for EAPOL on this interface */
  3920. add_ifidx(drv, ifidx, IFIDX_ANY);
  3921. }
  3922. if (addr && iftype != NL80211_IFTYPE_MONITOR &&
  3923. linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
  3924. nl80211_remove_iface(drv, ifidx);
  3925. return -1;
  3926. }
  3927. return ifidx;
  3928. }
  3929. int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
  3930. const char *ifname, enum nl80211_iftype iftype,
  3931. const u8 *addr, int wds,
  3932. int (*handler)(struct nl_msg *, void *),
  3933. void *arg, int use_existing)
  3934. {
  3935. int ret;
  3936. ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
  3937. arg);
  3938. /* if error occurred and interface exists already */
  3939. if (ret == -ENFILE && if_nametoindex(ifname)) {
  3940. if (use_existing) {
  3941. wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
  3942. ifname);
  3943. if (addr && iftype != NL80211_IFTYPE_MONITOR &&
  3944. linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
  3945. addr) < 0 &&
  3946. (linux_set_iface_flags(drv->global->ioctl_sock,
  3947. ifname, 0) < 0 ||
  3948. linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
  3949. addr) < 0 ||
  3950. linux_set_iface_flags(drv->global->ioctl_sock,
  3951. ifname, 1) < 0))
  3952. return -1;
  3953. return -ENFILE;
  3954. }
  3955. wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
  3956. /* Try to remove the interface that was already there. */
  3957. nl80211_remove_iface(drv, if_nametoindex(ifname));
  3958. /* Try to create the interface again */
  3959. ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
  3960. wds, handler, arg);
  3961. }
  3962. if (ret >= 0 && is_p2p_net_interface(iftype)) {
  3963. wpa_printf(MSG_DEBUG,
  3964. "nl80211: Interface %s created for P2P - disable 11b rates",
  3965. ifname);
  3966. nl80211_disable_11b_rates(drv, ret, 1);
  3967. }
  3968. return ret;
  3969. }
  3970. static int nl80211_setup_ap(struct i802_bss *bss)
  3971. {
  3972. struct wpa_driver_nl80211_data *drv = bss->drv;
  3973. wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
  3974. bss->ifname, drv->device_ap_sme, drv->use_monitor);
  3975. /*
  3976. * Disable Probe Request reporting unless we need it in this way for
  3977. * devices that include the AP SME, in the other case (unless using
  3978. * monitor iface) we'll get it through the nl_mgmt socket instead.
  3979. */
  3980. if (!drv->device_ap_sme)
  3981. wpa_driver_nl80211_probe_req_report(bss, 0);
  3982. if (!drv->device_ap_sme && !drv->use_monitor)
  3983. if (nl80211_mgmt_subscribe_ap(bss))
  3984. return -1;
  3985. if (drv->device_ap_sme && !drv->use_monitor)
  3986. if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
  3987. wpa_printf(MSG_DEBUG,
  3988. "nl80211: Failed to subscribe for mgmt frames from SME driver - trying to run without it");
  3989. if (!drv->device_ap_sme && drv->use_monitor &&
  3990. nl80211_create_monitor_interface(drv) &&
  3991. !drv->device_ap_sme)
  3992. return -1;
  3993. if (drv->device_ap_sme &&
  3994. wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
  3995. wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
  3996. "Probe Request frame reporting in AP mode");
  3997. /* Try to survive without this */
  3998. }
  3999. return 0;
  4000. }
  4001. static void nl80211_teardown_ap(struct i802_bss *bss)
  4002. {
  4003. struct wpa_driver_nl80211_data *drv = bss->drv;
  4004. wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
  4005. bss->ifname, drv->device_ap_sme, drv->use_monitor);
  4006. if (drv->device_ap_sme) {
  4007. wpa_driver_nl80211_probe_req_report(bss, 0);
  4008. if (!drv->use_monitor)
  4009. nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
  4010. } else if (drv->use_monitor)
  4011. nl80211_remove_monitor_interface(drv);
  4012. else
  4013. nl80211_mgmt_unsubscribe(bss, "AP teardown");
  4014. nl80211_put_wiphy_data_ap(bss);
  4015. bss->beacon_set = 0;
  4016. }
  4017. static int nl80211_send_eapol_data(struct i802_bss *bss,
  4018. const u8 *addr, const u8 *data,
  4019. size_t data_len)
  4020. {
  4021. struct sockaddr_ll ll;
  4022. int ret;
  4023. if (bss->drv->eapol_tx_sock < 0) {
  4024. wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
  4025. return -1;
  4026. }
  4027. os_memset(&ll, 0, sizeof(ll));
  4028. ll.sll_family = AF_PACKET;
  4029. ll.sll_ifindex = bss->ifindex;
  4030. ll.sll_protocol = htons(ETH_P_PAE);
  4031. ll.sll_halen = ETH_ALEN;
  4032. os_memcpy(ll.sll_addr, addr, ETH_ALEN);
  4033. ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
  4034. (struct sockaddr *) &ll, sizeof(ll));
  4035. if (ret < 0)
  4036. wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
  4037. strerror(errno));
  4038. return ret;
  4039. }
  4040. static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  4041. static int wpa_driver_nl80211_hapd_send_eapol(
  4042. void *priv, const u8 *addr, const u8 *data,
  4043. size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
  4044. {
  4045. struct i802_bss *bss = priv;
  4046. struct wpa_driver_nl80211_data *drv = bss->drv;
  4047. struct ieee80211_hdr *hdr;
  4048. size_t len;
  4049. u8 *pos;
  4050. int res;
  4051. int qos = flags & WPA_STA_WMM;
  4052. if (drv->device_ap_sme || !drv->use_monitor)
  4053. return nl80211_send_eapol_data(bss, addr, data, data_len);
  4054. len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
  4055. data_len;
  4056. hdr = os_zalloc(len);
  4057. if (hdr == NULL) {
  4058. wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
  4059. (unsigned long) len);
  4060. return -1;
  4061. }
  4062. hdr->frame_control =
  4063. IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
  4064. hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
  4065. if (encrypt)
  4066. hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  4067. if (qos) {
  4068. hdr->frame_control |=
  4069. host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
  4070. }
  4071. memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
  4072. memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
  4073. memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
  4074. pos = (u8 *) (hdr + 1);
  4075. if (qos) {
  4076. /* Set highest priority in QoS header */
  4077. pos[0] = 7;
  4078. pos[1] = 0;
  4079. pos += 2;
  4080. }
  4081. memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
  4082. pos += sizeof(rfc1042_header);
  4083. WPA_PUT_BE16(pos, ETH_P_PAE);
  4084. pos += 2;
  4085. memcpy(pos, data, data_len);
  4086. res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
  4087. 0, 0, 0, 0, NULL, 0);
  4088. if (res < 0) {
  4089. wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
  4090. "failed: %d (%s)",
  4091. (unsigned long) len, errno, strerror(errno));
  4092. }
  4093. os_free(hdr);
  4094. return res;
  4095. }
  4096. static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
  4097. unsigned int total_flags,
  4098. unsigned int flags_or,
  4099. unsigned int flags_and)
  4100. {
  4101. struct i802_bss *bss = priv;
  4102. struct nl_msg *msg;
  4103. struct nlattr *flags;
  4104. struct nl80211_sta_flag_update upd;
  4105. wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
  4106. " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
  4107. bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
  4108. !!(total_flags & WPA_STA_AUTHORIZED));
  4109. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
  4110. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
  4111. goto fail;
  4112. /*
  4113. * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
  4114. * can be removed eventually.
  4115. */
  4116. flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
  4117. if (!flags ||
  4118. ((total_flags & WPA_STA_AUTHORIZED) &&
  4119. nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
  4120. ((total_flags & WPA_STA_WMM) &&
  4121. nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
  4122. ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
  4123. nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
  4124. ((total_flags & WPA_STA_MFP) &&
  4125. nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
  4126. ((total_flags & WPA_STA_TDLS_PEER) &&
  4127. nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
  4128. goto fail;
  4129. nla_nest_end(msg, flags);
  4130. os_memset(&upd, 0, sizeof(upd));
  4131. upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
  4132. upd.set = sta_flags_nl80211(flags_or);
  4133. if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
  4134. goto fail;
  4135. return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  4136. fail:
  4137. nlmsg_free(msg);
  4138. return -ENOBUFS;
  4139. }
  4140. static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
  4141. struct wpa_driver_associate_params *params)
  4142. {
  4143. enum nl80211_iftype nlmode, old_mode;
  4144. if (params->p2p) {
  4145. wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
  4146. "group (GO)");
  4147. nlmode = NL80211_IFTYPE_P2P_GO;
  4148. } else
  4149. nlmode = NL80211_IFTYPE_AP;
  4150. old_mode = drv->nlmode;
  4151. if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
  4152. nl80211_remove_monitor_interface(drv);
  4153. return -1;
  4154. }
  4155. if (params->freq.freq &&
  4156. nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
  4157. if (old_mode != nlmode)
  4158. wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
  4159. nl80211_remove_monitor_interface(drv);
  4160. return -1;
  4161. }
  4162. return 0;
  4163. }
  4164. static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
  4165. int reset_mode)
  4166. {
  4167. struct nl_msg *msg;
  4168. int ret;
  4169. msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
  4170. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4171. if (ret) {
  4172. wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
  4173. "(%s)", ret, strerror(-ret));
  4174. } else {
  4175. wpa_printf(MSG_DEBUG,
  4176. "nl80211: Leave IBSS request sent successfully");
  4177. }
  4178. if (reset_mode &&
  4179. wpa_driver_nl80211_set_mode(drv->first_bss,
  4180. NL80211_IFTYPE_STATION)) {
  4181. wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
  4182. "station mode");
  4183. }
  4184. return ret;
  4185. }
  4186. static int nl80211_ht_vht_overrides(struct nl_msg *msg,
  4187. struct wpa_driver_associate_params *params)
  4188. {
  4189. if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
  4190. return -1;
  4191. if (params->htcaps && params->htcaps_mask) {
  4192. int sz = sizeof(struct ieee80211_ht_capabilities);
  4193. wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
  4194. wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
  4195. params->htcaps_mask, sz);
  4196. if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
  4197. params->htcaps) ||
  4198. nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
  4199. params->htcaps_mask))
  4200. return -1;
  4201. }
  4202. #ifdef CONFIG_VHT_OVERRIDES
  4203. if (params->disable_vht) {
  4204. wpa_printf(MSG_DEBUG, " * VHT disabled");
  4205. if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
  4206. return -1;
  4207. }
  4208. if (params->vhtcaps && params->vhtcaps_mask) {
  4209. int sz = sizeof(struct ieee80211_vht_capabilities);
  4210. wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
  4211. wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
  4212. params->vhtcaps_mask, sz);
  4213. if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
  4214. params->vhtcaps) ||
  4215. nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
  4216. params->vhtcaps_mask))
  4217. return -1;
  4218. }
  4219. #endif /* CONFIG_VHT_OVERRIDES */
  4220. return 0;
  4221. }
  4222. static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
  4223. struct wpa_driver_associate_params *params)
  4224. {
  4225. struct nl_msg *msg;
  4226. int ret = -1;
  4227. int count = 0;
  4228. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
  4229. if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
  4230. wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
  4231. "IBSS mode");
  4232. return -1;
  4233. }
  4234. retry:
  4235. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
  4236. params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
  4237. goto fail;
  4238. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  4239. params->ssid, params->ssid_len);
  4240. if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
  4241. goto fail;
  4242. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  4243. drv->ssid_len = params->ssid_len;
  4244. if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
  4245. nl80211_put_beacon_int(msg, params->beacon_int))
  4246. goto fail;
  4247. ret = nl80211_set_conn_keys(params, msg);
  4248. if (ret)
  4249. goto fail;
  4250. if (params->bssid && params->fixed_bssid) {
  4251. wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
  4252. MAC2STR(params->bssid));
  4253. if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
  4254. goto fail;
  4255. }
  4256. if (params->fixed_freq) {
  4257. wpa_printf(MSG_DEBUG, " * fixed_freq");
  4258. if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
  4259. goto fail;
  4260. }
  4261. if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
  4262. params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
  4263. params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
  4264. params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
  4265. wpa_printf(MSG_DEBUG, " * control port");
  4266. if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
  4267. goto fail;
  4268. }
  4269. if (params->wpa_ie) {
  4270. wpa_hexdump(MSG_DEBUG,
  4271. " * Extra IEs for Beacon/Probe Response frames",
  4272. params->wpa_ie, params->wpa_ie_len);
  4273. if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  4274. params->wpa_ie))
  4275. goto fail;
  4276. }
  4277. ret = nl80211_ht_vht_overrides(msg, params);
  4278. if (ret < 0)
  4279. goto fail;
  4280. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4281. msg = NULL;
  4282. if (ret) {
  4283. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
  4284. ret, strerror(-ret));
  4285. count++;
  4286. if (ret == -EALREADY && count == 1) {
  4287. wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
  4288. "forced leave");
  4289. nl80211_leave_ibss(drv, 0);
  4290. nlmsg_free(msg);
  4291. goto retry;
  4292. }
  4293. } else {
  4294. wpa_printf(MSG_DEBUG,
  4295. "nl80211: Join IBSS request sent successfully");
  4296. }
  4297. fail:
  4298. nlmsg_free(msg);
  4299. return ret;
  4300. }
  4301. static int nl80211_put_fils_connect_params(struct wpa_driver_nl80211_data *drv,
  4302. struct wpa_driver_associate_params *params,
  4303. struct nl_msg *msg)
  4304. {
  4305. if (params->fils_erp_username_len) {
  4306. wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP EMSKname/username",
  4307. params->fils_erp_username,
  4308. params->fils_erp_username_len);
  4309. if (nla_put(msg, NL80211_ATTR_FILS_ERP_USERNAME,
  4310. params->fils_erp_username_len,
  4311. params->fils_erp_username))
  4312. return -1;
  4313. }
  4314. if (params->fils_erp_realm_len) {
  4315. wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP Realm",
  4316. params->fils_erp_realm,
  4317. params->fils_erp_realm_len);
  4318. if (nla_put(msg, NL80211_ATTR_FILS_ERP_REALM,
  4319. params->fils_erp_realm_len, params->fils_erp_realm))
  4320. return -1;
  4321. }
  4322. wpa_printf(MSG_DEBUG, " * FILS ERP next seq %u",
  4323. params->fils_erp_next_seq_num);
  4324. if (nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
  4325. params->fils_erp_next_seq_num))
  4326. return -1;
  4327. if (params->fils_erp_rrk_len) {
  4328. wpa_printf(MSG_DEBUG, " * FILS ERP rRK (len=%lu)",
  4329. (unsigned long) params->fils_erp_rrk_len);
  4330. if (nla_put(msg, NL80211_ATTR_FILS_ERP_RRK,
  4331. params->fils_erp_rrk_len, params->fils_erp_rrk))
  4332. return -1;
  4333. }
  4334. return 0;
  4335. }
  4336. static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
  4337. struct wpa_driver_associate_params *params,
  4338. struct nl_msg *msg)
  4339. {
  4340. if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
  4341. return -1;
  4342. if (params->bssid) {
  4343. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  4344. MAC2STR(params->bssid));
  4345. if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
  4346. return -1;
  4347. }
  4348. if (params->bssid_hint) {
  4349. wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
  4350. MAC2STR(params->bssid_hint));
  4351. if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
  4352. params->bssid_hint))
  4353. return -1;
  4354. }
  4355. if (params->freq.freq) {
  4356. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
  4357. if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
  4358. params->freq.freq))
  4359. return -1;
  4360. drv->assoc_freq = params->freq.freq;
  4361. } else
  4362. drv->assoc_freq = 0;
  4363. if (params->freq_hint) {
  4364. wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
  4365. if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
  4366. params->freq_hint))
  4367. return -1;
  4368. }
  4369. if (params->bg_scan_period >= 0) {
  4370. wpa_printf(MSG_DEBUG, " * bg scan period=%d",
  4371. params->bg_scan_period);
  4372. if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
  4373. params->bg_scan_period))
  4374. return -1;
  4375. }
  4376. if (params->ssid) {
  4377. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  4378. params->ssid, params->ssid_len);
  4379. if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
  4380. params->ssid))
  4381. return -1;
  4382. if (params->ssid_len > sizeof(drv->ssid))
  4383. return -1;
  4384. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  4385. drv->ssid_len = params->ssid_len;
  4386. }
  4387. wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
  4388. if (params->wpa_ie &&
  4389. nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
  4390. return -1;
  4391. if (params->wpa_proto) {
  4392. enum nl80211_wpa_versions ver = 0;
  4393. if (params->wpa_proto & WPA_PROTO_WPA)
  4394. ver |= NL80211_WPA_VERSION_1;
  4395. if (params->wpa_proto & WPA_PROTO_RSN)
  4396. ver |= NL80211_WPA_VERSION_2;
  4397. wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
  4398. if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
  4399. return -1;
  4400. }
  4401. if (params->pairwise_suite != WPA_CIPHER_NONE) {
  4402. u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
  4403. wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
  4404. if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
  4405. cipher))
  4406. return -1;
  4407. }
  4408. if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
  4409. !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
  4410. /*
  4411. * This is likely to work even though many drivers do not
  4412. * advertise support for operations without GTK.
  4413. */
  4414. wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
  4415. } else if (params->group_suite != WPA_CIPHER_NONE) {
  4416. u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
  4417. wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
  4418. if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
  4419. return -1;
  4420. }
  4421. if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
  4422. params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
  4423. params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
  4424. params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
  4425. params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
  4426. params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
  4427. params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
  4428. params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
  4429. params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
  4430. params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ||
  4431. params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA256 ||
  4432. params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA384 ||
  4433. params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA256 ||
  4434. params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA384) {
  4435. int mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
  4436. switch (params->key_mgmt_suite) {
  4437. case WPA_KEY_MGMT_CCKM:
  4438. mgmt = RSN_AUTH_KEY_MGMT_CCKM;
  4439. break;
  4440. case WPA_KEY_MGMT_IEEE8021X:
  4441. mgmt = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
  4442. break;
  4443. case WPA_KEY_MGMT_FT_IEEE8021X:
  4444. mgmt = RSN_AUTH_KEY_MGMT_FT_802_1X;
  4445. break;
  4446. case WPA_KEY_MGMT_FT_PSK:
  4447. mgmt = RSN_AUTH_KEY_MGMT_FT_PSK;
  4448. break;
  4449. case WPA_KEY_MGMT_IEEE8021X_SHA256:
  4450. mgmt = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
  4451. break;
  4452. case WPA_KEY_MGMT_PSK_SHA256:
  4453. mgmt = RSN_AUTH_KEY_MGMT_PSK_SHA256;
  4454. break;
  4455. case WPA_KEY_MGMT_OSEN:
  4456. mgmt = RSN_AUTH_KEY_MGMT_OSEN;
  4457. break;
  4458. case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
  4459. mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
  4460. break;
  4461. case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
  4462. mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
  4463. break;
  4464. case WPA_KEY_MGMT_FILS_SHA256:
  4465. mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA256;
  4466. break;
  4467. case WPA_KEY_MGMT_FILS_SHA384:
  4468. mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA384;
  4469. break;
  4470. case WPA_KEY_MGMT_FT_FILS_SHA256:
  4471. mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA256;
  4472. break;
  4473. case WPA_KEY_MGMT_FT_FILS_SHA384:
  4474. mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA384;
  4475. break;
  4476. case WPA_KEY_MGMT_PSK:
  4477. default:
  4478. mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
  4479. break;
  4480. }
  4481. wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
  4482. if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
  4483. return -1;
  4484. }
  4485. if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
  4486. return -1;
  4487. if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  4488. (params->pairwise_suite == WPA_CIPHER_NONE ||
  4489. params->pairwise_suite == WPA_CIPHER_WEP104 ||
  4490. params->pairwise_suite == WPA_CIPHER_WEP40) &&
  4491. (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
  4492. nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
  4493. return -1;
  4494. if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
  4495. nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
  4496. return -1;
  4497. if (params->rrm_used) {
  4498. u32 drv_rrm_flags = drv->capa.rrm_flags;
  4499. if ((!((drv_rrm_flags &
  4500. WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
  4501. (drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
  4502. !(drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) ||
  4503. nla_put_flag(msg, NL80211_ATTR_USE_RRM))
  4504. return -1;
  4505. }
  4506. if (nl80211_ht_vht_overrides(msg, params) < 0)
  4507. return -1;
  4508. if (params->p2p)
  4509. wpa_printf(MSG_DEBUG, " * P2P group");
  4510. if (params->pbss) {
  4511. wpa_printf(MSG_DEBUG, " * PBSS");
  4512. if (nla_put_flag(msg, NL80211_ATTR_PBSS))
  4513. return -1;
  4514. }
  4515. drv->connect_reassoc = 0;
  4516. if (params->prev_bssid) {
  4517. wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
  4518. MAC2STR(params->prev_bssid));
  4519. if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
  4520. params->prev_bssid))
  4521. return -1;
  4522. drv->connect_reassoc = 1;
  4523. }
  4524. if ((params->auth_alg & WPA_AUTH_ALG_FILS) &&
  4525. nl80211_put_fils_connect_params(drv, params, msg) != 0)
  4526. return -1;
  4527. return 0;
  4528. }
  4529. static int wpa_driver_nl80211_try_connect(
  4530. struct wpa_driver_nl80211_data *drv,
  4531. struct wpa_driver_associate_params *params)
  4532. {
  4533. struct nl_msg *msg;
  4534. enum nl80211_auth_type type;
  4535. int ret;
  4536. int algs;
  4537. #ifdef CONFIG_DRIVER_NL80211_QCA
  4538. if (params->req_key_mgmt_offload && params->psk &&
  4539. (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
  4540. params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
  4541. params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
  4542. wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
  4543. ret = issue_key_mgmt_set_key(drv, params->psk, 32);
  4544. if (ret)
  4545. return ret;
  4546. }
  4547. #endif /* CONFIG_DRIVER_NL80211_QCA */
  4548. wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
  4549. msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
  4550. if (!msg)
  4551. return -1;
  4552. ret = nl80211_connect_common(drv, params, msg);
  4553. if (ret)
  4554. goto fail;
  4555. algs = 0;
  4556. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  4557. algs++;
  4558. if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  4559. algs++;
  4560. if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  4561. algs++;
  4562. if (algs > 1) {
  4563. wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
  4564. "selection");
  4565. goto skip_auth_type;
  4566. }
  4567. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  4568. type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  4569. else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  4570. type = NL80211_AUTHTYPE_SHARED_KEY;
  4571. else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  4572. type = NL80211_AUTHTYPE_NETWORK_EAP;
  4573. else if (params->auth_alg & WPA_AUTH_ALG_FT)
  4574. type = NL80211_AUTHTYPE_FT;
  4575. else if (params->auth_alg & WPA_AUTH_ALG_FILS)
  4576. type = NL80211_AUTHTYPE_FILS_SK;
  4577. else
  4578. goto fail;
  4579. wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
  4580. if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
  4581. goto fail;
  4582. skip_auth_type:
  4583. ret = nl80211_set_conn_keys(params, msg);
  4584. if (ret)
  4585. goto fail;
  4586. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4587. msg = NULL;
  4588. if (ret) {
  4589. wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
  4590. "(%s)", ret, strerror(-ret));
  4591. } else {
  4592. wpa_printf(MSG_DEBUG,
  4593. "nl80211: Connect request send successfully");
  4594. }
  4595. fail:
  4596. nlmsg_free(msg);
  4597. return ret;
  4598. }
  4599. static int wpa_driver_nl80211_connect(
  4600. struct wpa_driver_nl80211_data *drv,
  4601. struct wpa_driver_associate_params *params)
  4602. {
  4603. int ret;
  4604. /* Store the connection attempted bssid for future use */
  4605. if (params->bssid)
  4606. os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
  4607. else
  4608. os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
  4609. ret = wpa_driver_nl80211_try_connect(drv, params);
  4610. if (ret == -EALREADY) {
  4611. /*
  4612. * cfg80211 does not currently accept new connections if
  4613. * we are already connected. As a workaround, force
  4614. * disconnection and try again.
  4615. */
  4616. wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
  4617. "disconnecting before reassociation "
  4618. "attempt");
  4619. if (wpa_driver_nl80211_disconnect(
  4620. drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
  4621. return -1;
  4622. ret = wpa_driver_nl80211_try_connect(drv, params);
  4623. }
  4624. return ret;
  4625. }
  4626. static int wpa_driver_nl80211_associate(
  4627. void *priv, struct wpa_driver_associate_params *params)
  4628. {
  4629. struct i802_bss *bss = priv;
  4630. struct wpa_driver_nl80211_data *drv = bss->drv;
  4631. int ret = -1;
  4632. struct nl_msg *msg;
  4633. nl80211_unmask_11b_rates(bss);
  4634. if (params->mode == IEEE80211_MODE_AP)
  4635. return wpa_driver_nl80211_ap(drv, params);
  4636. if (params->mode == IEEE80211_MODE_IBSS)
  4637. return wpa_driver_nl80211_ibss(drv, params);
  4638. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
  4639. enum nl80211_iftype nlmode = params->p2p ?
  4640. NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
  4641. if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
  4642. return -1;
  4643. return wpa_driver_nl80211_connect(drv, params);
  4644. }
  4645. nl80211_mark_disconnected(drv);
  4646. wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
  4647. drv->ifindex);
  4648. msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
  4649. if (!msg)
  4650. return -1;
  4651. ret = nl80211_connect_common(drv, params, msg);
  4652. if (ret)
  4653. goto fail;
  4654. if (params->fils_kek) {
  4655. wpa_printf(MSG_DEBUG, " * FILS KEK (len=%u)",
  4656. (unsigned int) params->fils_kek_len);
  4657. if (nla_put(msg, NL80211_ATTR_FILS_KEK, params->fils_kek_len,
  4658. params->fils_kek))
  4659. goto fail;
  4660. }
  4661. if (params->fils_nonces) {
  4662. wpa_hexdump(MSG_DEBUG, " * FILS nonces (for AAD)",
  4663. params->fils_nonces,
  4664. params->fils_nonces_len);
  4665. if (nla_put(msg, NL80211_ATTR_FILS_NONCES,
  4666. params->fils_nonces_len, params->fils_nonces))
  4667. goto fail;
  4668. }
  4669. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4670. msg = NULL;
  4671. if (ret) {
  4672. wpa_dbg(drv->ctx, MSG_DEBUG,
  4673. "nl80211: MLME command failed (assoc): ret=%d (%s)",
  4674. ret, strerror(-ret));
  4675. nl80211_dump_scan(drv);
  4676. } else {
  4677. wpa_printf(MSG_DEBUG,
  4678. "nl80211: Association request send successfully");
  4679. }
  4680. fail:
  4681. nlmsg_free(msg);
  4682. return ret;
  4683. }
  4684. static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
  4685. int ifindex, enum nl80211_iftype mode)
  4686. {
  4687. struct nl_msg *msg;
  4688. int ret = -ENOBUFS;
  4689. wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
  4690. ifindex, mode, nl80211_iftype_str(mode));
  4691. msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
  4692. if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
  4693. goto fail;
  4694. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4695. msg = NULL;
  4696. if (!ret)
  4697. return 0;
  4698. fail:
  4699. nlmsg_free(msg);
  4700. wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
  4701. " %d (%s)", ifindex, mode, ret, strerror(-ret));
  4702. return ret;
  4703. }
  4704. static int wpa_driver_nl80211_set_mode_impl(
  4705. struct i802_bss *bss,
  4706. enum nl80211_iftype nlmode,
  4707. struct hostapd_freq_params *desired_freq_params)
  4708. {
  4709. struct wpa_driver_nl80211_data *drv = bss->drv;
  4710. int ret = -1;
  4711. int i;
  4712. int was_ap = is_ap_interface(drv->nlmode);
  4713. int res;
  4714. int mode_switch_res;
  4715. if (TEST_FAIL())
  4716. return -1;
  4717. mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
  4718. if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
  4719. mode_switch_res = 0;
  4720. if (mode_switch_res == 0) {
  4721. drv->nlmode = nlmode;
  4722. ret = 0;
  4723. goto done;
  4724. }
  4725. if (mode_switch_res == -ENODEV)
  4726. return -1;
  4727. if (nlmode == drv->nlmode) {
  4728. wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
  4729. "requested mode - ignore error");
  4730. ret = 0;
  4731. goto done; /* Already in the requested mode */
  4732. }
  4733. /* mac80211 doesn't allow mode changes while the device is up, so
  4734. * take the device down, try to set the mode again, and bring the
  4735. * device back up.
  4736. */
  4737. wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
  4738. "interface down");
  4739. for (i = 0; i < 10; i++) {
  4740. res = i802_set_iface_flags(bss, 0);
  4741. if (res == -EACCES || res == -ENODEV)
  4742. break;
  4743. if (res != 0) {
  4744. wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
  4745. "interface down");
  4746. os_sleep(0, 100000);
  4747. continue;
  4748. }
  4749. /*
  4750. * Setting the mode will fail for some drivers if the phy is
  4751. * on a frequency that the mode is disallowed in.
  4752. */
  4753. if (desired_freq_params) {
  4754. res = nl80211_set_channel(bss, desired_freq_params, 0);
  4755. if (res) {
  4756. wpa_printf(MSG_DEBUG,
  4757. "nl80211: Failed to set frequency on interface");
  4758. }
  4759. }
  4760. /* Try to set the mode again while the interface is down */
  4761. mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
  4762. if (mode_switch_res == -EBUSY) {
  4763. wpa_printf(MSG_DEBUG,
  4764. "nl80211: Delaying mode set while interface going down");
  4765. os_sleep(0, 100000);
  4766. continue;
  4767. }
  4768. ret = mode_switch_res;
  4769. break;
  4770. }
  4771. if (!ret) {
  4772. wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
  4773. "interface is down");
  4774. drv->nlmode = nlmode;
  4775. drv->ignore_if_down_event = 1;
  4776. }
  4777. /* Bring the interface back up */
  4778. res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
  4779. if (res != 0) {
  4780. wpa_printf(MSG_DEBUG,
  4781. "nl80211: Failed to set interface up after switching mode");
  4782. ret = -1;
  4783. }
  4784. done:
  4785. if (ret) {
  4786. wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
  4787. "from %d failed", nlmode, drv->nlmode);
  4788. return ret;
  4789. }
  4790. if (is_p2p_net_interface(nlmode)) {
  4791. wpa_printf(MSG_DEBUG,
  4792. "nl80211: Interface %s mode change to P2P - disable 11b rates",
  4793. bss->ifname);
  4794. nl80211_disable_11b_rates(drv, drv->ifindex, 1);
  4795. } else if (drv->disabled_11b_rates) {
  4796. wpa_printf(MSG_DEBUG,
  4797. "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
  4798. bss->ifname);
  4799. nl80211_disable_11b_rates(drv, drv->ifindex, 0);
  4800. }
  4801. if (is_ap_interface(nlmode)) {
  4802. nl80211_mgmt_unsubscribe(bss, "start AP");
  4803. /* Setup additional AP mode functionality if needed */
  4804. if (nl80211_setup_ap(bss))
  4805. return -1;
  4806. } else if (was_ap) {
  4807. /* Remove additional AP mode functionality */
  4808. nl80211_teardown_ap(bss);
  4809. } else {
  4810. nl80211_mgmt_unsubscribe(bss, "mode change");
  4811. }
  4812. if (is_mesh_interface(nlmode) &&
  4813. nl80211_mgmt_subscribe_mesh(bss))
  4814. return -1;
  4815. if (!bss->in_deinit && !is_ap_interface(nlmode) &&
  4816. !is_mesh_interface(nlmode) &&
  4817. nl80211_mgmt_subscribe_non_ap(bss) < 0)
  4818. wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
  4819. "frame processing - ignore for now");
  4820. return 0;
  4821. }
  4822. int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
  4823. enum nl80211_iftype nlmode)
  4824. {
  4825. return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
  4826. }
  4827. static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
  4828. struct hostapd_freq_params *freq)
  4829. {
  4830. return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
  4831. freq);
  4832. }
  4833. static int wpa_driver_nl80211_get_capa(void *priv,
  4834. struct wpa_driver_capa *capa)
  4835. {
  4836. struct i802_bss *bss = priv;
  4837. struct wpa_driver_nl80211_data *drv = bss->drv;
  4838. if (!drv->has_capability)
  4839. return -1;
  4840. os_memcpy(capa, &drv->capa, sizeof(*capa));
  4841. if (drv->extended_capa && drv->extended_capa_mask) {
  4842. capa->extended_capa = drv->extended_capa;
  4843. capa->extended_capa_mask = drv->extended_capa_mask;
  4844. capa->extended_capa_len = drv->extended_capa_len;
  4845. }
  4846. return 0;
  4847. }
  4848. static int wpa_driver_nl80211_set_operstate(void *priv, int state)
  4849. {
  4850. struct i802_bss *bss = priv;
  4851. struct wpa_driver_nl80211_data *drv = bss->drv;
  4852. wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
  4853. bss->ifname, drv->operstate, state,
  4854. state ? "UP" : "DORMANT");
  4855. drv->operstate = state;
  4856. return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
  4857. state ? IF_OPER_UP : IF_OPER_DORMANT);
  4858. }
  4859. static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
  4860. {
  4861. struct i802_bss *bss = priv;
  4862. struct wpa_driver_nl80211_data *drv = bss->drv;
  4863. struct nl_msg *msg;
  4864. struct nl80211_sta_flag_update upd;
  4865. int ret;
  4866. if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
  4867. wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
  4868. return 0;
  4869. }
  4870. wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
  4871. MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
  4872. os_memset(&upd, 0, sizeof(upd));
  4873. upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
  4874. if (authorized)
  4875. upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
  4876. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
  4877. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
  4878. nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
  4879. nlmsg_free(msg);
  4880. return -ENOBUFS;
  4881. }
  4882. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4883. if (!ret)
  4884. return 0;
  4885. wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
  4886. ret, strerror(-ret));
  4887. return ret;
  4888. }
  4889. /* Set kernel driver on given frequency (MHz) */
  4890. static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
  4891. {
  4892. struct i802_bss *bss = priv;
  4893. return nl80211_set_channel(bss, freq, 0);
  4894. }
  4895. static inline int min_int(int a, int b)
  4896. {
  4897. if (a < b)
  4898. return a;
  4899. return b;
  4900. }
  4901. static int get_key_handler(struct nl_msg *msg, void *arg)
  4902. {
  4903. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  4904. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  4905. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  4906. genlmsg_attrlen(gnlh, 0), NULL);
  4907. /*
  4908. * TODO: validate the key index and mac address!
  4909. * Otherwise, there's a race condition as soon as
  4910. * the kernel starts sending key notifications.
  4911. */
  4912. if (tb[NL80211_ATTR_KEY_SEQ])
  4913. memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
  4914. min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
  4915. return NL_SKIP;
  4916. }
  4917. static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
  4918. int idx, u8 *seq)
  4919. {
  4920. struct i802_bss *bss = priv;
  4921. struct wpa_driver_nl80211_data *drv = bss->drv;
  4922. struct nl_msg *msg;
  4923. msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
  4924. NL80211_CMD_GET_KEY);
  4925. if (!msg ||
  4926. (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
  4927. nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
  4928. nlmsg_free(msg);
  4929. return -ENOBUFS;
  4930. }
  4931. memset(seq, 0, 6);
  4932. return send_and_recv_msgs(drv, msg, get_key_handler, seq);
  4933. }
  4934. static int i802_set_rts(void *priv, int rts)
  4935. {
  4936. struct i802_bss *bss = priv;
  4937. struct wpa_driver_nl80211_data *drv = bss->drv;
  4938. struct nl_msg *msg;
  4939. int ret;
  4940. u32 val;
  4941. if (rts >= 2347)
  4942. val = (u32) -1;
  4943. else
  4944. val = rts;
  4945. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
  4946. nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
  4947. nlmsg_free(msg);
  4948. return -ENOBUFS;
  4949. }
  4950. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4951. if (!ret)
  4952. return 0;
  4953. wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
  4954. "%d (%s)", rts, ret, strerror(-ret));
  4955. return ret;
  4956. }
  4957. static int i802_set_frag(void *priv, int frag)
  4958. {
  4959. struct i802_bss *bss = priv;
  4960. struct wpa_driver_nl80211_data *drv = bss->drv;
  4961. struct nl_msg *msg;
  4962. int ret;
  4963. u32 val;
  4964. if (frag >= 2346)
  4965. val = (u32) -1;
  4966. else
  4967. val = frag;
  4968. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
  4969. nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
  4970. nlmsg_free(msg);
  4971. return -ENOBUFS;
  4972. }
  4973. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4974. if (!ret)
  4975. return 0;
  4976. wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
  4977. "%d: %d (%s)", frag, ret, strerror(-ret));
  4978. return ret;
  4979. }
  4980. static int i802_flush(void *priv)
  4981. {
  4982. struct i802_bss *bss = priv;
  4983. struct nl_msg *msg;
  4984. int res;
  4985. wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
  4986. bss->ifname);
  4987. /*
  4988. * XXX: FIX! this needs to flush all VLANs too
  4989. */
  4990. msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
  4991. res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  4992. if (res) {
  4993. wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
  4994. "(%s)", res, strerror(-res));
  4995. }
  4996. return res;
  4997. }
  4998. static int get_sta_handler(struct nl_msg *msg, void *arg)
  4999. {
  5000. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  5001. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  5002. struct hostap_sta_driver_data *data = arg;
  5003. struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
  5004. static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
  5005. [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
  5006. [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
  5007. [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
  5008. [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
  5009. [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
  5010. [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
  5011. [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
  5012. [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
  5013. };
  5014. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  5015. genlmsg_attrlen(gnlh, 0), NULL);
  5016. /*
  5017. * TODO: validate the interface and mac address!
  5018. * Otherwise, there's a race condition as soon as
  5019. * the kernel starts sending station notifications.
  5020. */
  5021. if (!tb[NL80211_ATTR_STA_INFO]) {
  5022. wpa_printf(MSG_DEBUG, "sta stats missing!");
  5023. return NL_SKIP;
  5024. }
  5025. if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
  5026. tb[NL80211_ATTR_STA_INFO],
  5027. stats_policy)) {
  5028. wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
  5029. return NL_SKIP;
  5030. }
  5031. if (stats[NL80211_STA_INFO_INACTIVE_TIME])
  5032. data->inactive_msec =
  5033. nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
  5034. /* For backwards compatibility, fetch the 32-bit counters first. */
  5035. if (stats[NL80211_STA_INFO_RX_BYTES])
  5036. data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
  5037. if (stats[NL80211_STA_INFO_TX_BYTES])
  5038. data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
  5039. if (stats[NL80211_STA_INFO_RX_BYTES64] &&
  5040. stats[NL80211_STA_INFO_TX_BYTES64]) {
  5041. /*
  5042. * The driver supports 64-bit counters, so use them to override
  5043. * the 32-bit values.
  5044. */
  5045. data->rx_bytes =
  5046. nla_get_u64(stats[NL80211_STA_INFO_RX_BYTES64]);
  5047. data->tx_bytes =
  5048. nla_get_u64(stats[NL80211_STA_INFO_TX_BYTES64]);
  5049. data->bytes_64bit = 1;
  5050. }
  5051. if (stats[NL80211_STA_INFO_RX_PACKETS])
  5052. data->rx_packets =
  5053. nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
  5054. if (stats[NL80211_STA_INFO_TX_PACKETS])
  5055. data->tx_packets =
  5056. nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
  5057. if (stats[NL80211_STA_INFO_TX_FAILED])
  5058. data->tx_retry_failed =
  5059. nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
  5060. return NL_SKIP;
  5061. }
  5062. static int i802_read_sta_data(struct i802_bss *bss,
  5063. struct hostap_sta_driver_data *data,
  5064. const u8 *addr)
  5065. {
  5066. struct nl_msg *msg;
  5067. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
  5068. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
  5069. nlmsg_free(msg);
  5070. return -ENOBUFS;
  5071. }
  5072. return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
  5073. }
  5074. static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
  5075. int cw_min, int cw_max, int burst_time)
  5076. {
  5077. struct i802_bss *bss = priv;
  5078. struct wpa_driver_nl80211_data *drv = bss->drv;
  5079. struct nl_msg *msg;
  5080. struct nlattr *txq, *params;
  5081. msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
  5082. if (!msg)
  5083. return -1;
  5084. txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
  5085. if (!txq)
  5086. goto fail;
  5087. /* We are only sending parameters for a single TXQ at a time */
  5088. params = nla_nest_start(msg, 1);
  5089. if (!params)
  5090. goto fail;
  5091. switch (queue) {
  5092. case 0:
  5093. if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
  5094. goto fail;
  5095. break;
  5096. case 1:
  5097. if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
  5098. goto fail;
  5099. break;
  5100. case 2:
  5101. if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
  5102. goto fail;
  5103. break;
  5104. case 3:
  5105. if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
  5106. goto fail;
  5107. break;
  5108. }
  5109. /* Burst time is configured in units of 0.1 msec and TXOP parameter in
  5110. * 32 usec, so need to convert the value here. */
  5111. if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
  5112. (burst_time * 100 + 16) / 32) ||
  5113. nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
  5114. nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
  5115. nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
  5116. goto fail;
  5117. nla_nest_end(msg, params);
  5118. nla_nest_end(msg, txq);
  5119. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  5120. return 0;
  5121. msg = NULL;
  5122. fail:
  5123. nlmsg_free(msg);
  5124. return -1;
  5125. }
  5126. static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
  5127. const char *ifname, int vlan_id)
  5128. {
  5129. struct wpa_driver_nl80211_data *drv = bss->drv;
  5130. struct nl_msg *msg;
  5131. int ret;
  5132. wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
  5133. ", ifname=%s[%d], vlan_id=%d)",
  5134. bss->ifname, if_nametoindex(bss->ifname),
  5135. MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
  5136. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
  5137. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
  5138. nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
  5139. nlmsg_free(msg);
  5140. return -ENOBUFS;
  5141. }
  5142. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5143. if (ret < 0) {
  5144. wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
  5145. MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
  5146. MAC2STR(addr), ifname, vlan_id, ret,
  5147. strerror(-ret));
  5148. }
  5149. return ret;
  5150. }
  5151. static int i802_get_inact_sec(void *priv, const u8 *addr)
  5152. {
  5153. struct hostap_sta_driver_data data;
  5154. int ret;
  5155. os_memset(&data, 0, sizeof(data));
  5156. data.inactive_msec = (unsigned long) -1;
  5157. ret = i802_read_sta_data(priv, &data, addr);
  5158. if (ret == -ENOENT)
  5159. return -ENOENT;
  5160. if (ret || data.inactive_msec == (unsigned long) -1)
  5161. return -1;
  5162. return data.inactive_msec / 1000;
  5163. }
  5164. static int i802_sta_clear_stats(void *priv, const u8 *addr)
  5165. {
  5166. #if 0
  5167. /* TODO */
  5168. #endif
  5169. return 0;
  5170. }
  5171. static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
  5172. int reason)
  5173. {
  5174. struct i802_bss *bss = priv;
  5175. struct wpa_driver_nl80211_data *drv = bss->drv;
  5176. struct ieee80211_mgmt mgmt;
  5177. u8 channel;
  5178. if (ieee80211_freq_to_chan(bss->freq, &channel) ==
  5179. HOSTAPD_MODE_IEEE80211AD) {
  5180. /* Deauthentication is not used in DMG/IEEE 802.11ad;
  5181. * disassociate the STA instead. */
  5182. return i802_sta_disassoc(priv, own_addr, addr, reason);
  5183. }
  5184. if (is_mesh_interface(drv->nlmode))
  5185. return -1;
  5186. if (drv->device_ap_sme)
  5187. return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
  5188. memset(&mgmt, 0, sizeof(mgmt));
  5189. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  5190. WLAN_FC_STYPE_DEAUTH);
  5191. memcpy(mgmt.da, addr, ETH_ALEN);
  5192. memcpy(mgmt.sa, own_addr, ETH_ALEN);
  5193. memcpy(mgmt.bssid, own_addr, ETH_ALEN);
  5194. mgmt.u.deauth.reason_code = host_to_le16(reason);
  5195. return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
  5196. IEEE80211_HDRLEN +
  5197. sizeof(mgmt.u.deauth), 0, 0, 0, 0,
  5198. 0, NULL, 0);
  5199. }
  5200. static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
  5201. int reason)
  5202. {
  5203. struct i802_bss *bss = priv;
  5204. struct wpa_driver_nl80211_data *drv = bss->drv;
  5205. struct ieee80211_mgmt mgmt;
  5206. if (is_mesh_interface(drv->nlmode))
  5207. return -1;
  5208. if (drv->device_ap_sme)
  5209. return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
  5210. memset(&mgmt, 0, sizeof(mgmt));
  5211. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  5212. WLAN_FC_STYPE_DISASSOC);
  5213. memcpy(mgmt.da, addr, ETH_ALEN);
  5214. memcpy(mgmt.sa, own_addr, ETH_ALEN);
  5215. memcpy(mgmt.bssid, own_addr, ETH_ALEN);
  5216. mgmt.u.disassoc.reason_code = host_to_le16(reason);
  5217. return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
  5218. IEEE80211_HDRLEN +
  5219. sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
  5220. 0, NULL, 0);
  5221. }
  5222. static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
  5223. {
  5224. char buf[200], *pos, *end;
  5225. int i, res;
  5226. pos = buf;
  5227. end = pos + sizeof(buf);
  5228. for (i = 0; i < drv->num_if_indices; i++) {
  5229. if (!drv->if_indices[i])
  5230. continue;
  5231. res = os_snprintf(pos, end - pos, " %d(%d)",
  5232. drv->if_indices[i],
  5233. drv->if_indices_reason[i]);
  5234. if (os_snprintf_error(end - pos, res))
  5235. break;
  5236. pos += res;
  5237. }
  5238. *pos = '\0';
  5239. wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
  5240. drv->num_if_indices, buf);
  5241. }
  5242. static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
  5243. int ifidx_reason)
  5244. {
  5245. int i;
  5246. int *old, *old_reason;
  5247. wpa_printf(MSG_DEBUG,
  5248. "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
  5249. ifidx, ifidx_reason);
  5250. if (have_ifidx(drv, ifidx, ifidx_reason)) {
  5251. wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
  5252. ifidx);
  5253. return;
  5254. }
  5255. for (i = 0; i < drv->num_if_indices; i++) {
  5256. if (drv->if_indices[i] == 0) {
  5257. drv->if_indices[i] = ifidx;
  5258. drv->if_indices_reason[i] = ifidx_reason;
  5259. dump_ifidx(drv);
  5260. return;
  5261. }
  5262. }
  5263. if (drv->if_indices != drv->default_if_indices)
  5264. old = drv->if_indices;
  5265. else
  5266. old = NULL;
  5267. if (drv->if_indices_reason != drv->default_if_indices_reason)
  5268. old_reason = drv->if_indices_reason;
  5269. else
  5270. old_reason = NULL;
  5271. drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
  5272. sizeof(int));
  5273. drv->if_indices_reason = os_realloc_array(old_reason,
  5274. drv->num_if_indices + 1,
  5275. sizeof(int));
  5276. if (!drv->if_indices) {
  5277. if (!old)
  5278. drv->if_indices = drv->default_if_indices;
  5279. else
  5280. drv->if_indices = old;
  5281. }
  5282. if (!drv->if_indices_reason) {
  5283. if (!old_reason)
  5284. drv->if_indices_reason = drv->default_if_indices_reason;
  5285. else
  5286. drv->if_indices_reason = old_reason;
  5287. }
  5288. if (!drv->if_indices || !drv->if_indices_reason) {
  5289. wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
  5290. "interfaces");
  5291. wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
  5292. return;
  5293. }
  5294. if (!old)
  5295. os_memcpy(drv->if_indices, drv->default_if_indices,
  5296. sizeof(drv->default_if_indices));
  5297. if (!old_reason)
  5298. os_memcpy(drv->if_indices_reason,
  5299. drv->default_if_indices_reason,
  5300. sizeof(drv->default_if_indices_reason));
  5301. drv->if_indices[drv->num_if_indices] = ifidx;
  5302. drv->if_indices_reason[drv->num_if_indices] = ifidx_reason;
  5303. drv->num_if_indices++;
  5304. dump_ifidx(drv);
  5305. }
  5306. static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
  5307. int ifidx_reason)
  5308. {
  5309. int i;
  5310. for (i = 0; i < drv->num_if_indices; i++) {
  5311. if ((drv->if_indices[i] == ifidx || ifidx == IFIDX_ANY) &&
  5312. (drv->if_indices_reason[i] == ifidx_reason ||
  5313. ifidx_reason == IFIDX_ANY)) {
  5314. drv->if_indices[i] = 0;
  5315. break;
  5316. }
  5317. }
  5318. dump_ifidx(drv);
  5319. }
  5320. static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
  5321. int ifidx_reason)
  5322. {
  5323. int i;
  5324. for (i = 0; i < drv->num_if_indices; i++)
  5325. if (drv->if_indices[i] == ifidx &&
  5326. (drv->if_indices_reason[i] == ifidx_reason ||
  5327. ifidx_reason == IFIDX_ANY))
  5328. return 1;
  5329. return 0;
  5330. }
  5331. static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
  5332. const char *bridge_ifname, char *ifname_wds)
  5333. {
  5334. struct i802_bss *bss = priv;
  5335. struct wpa_driver_nl80211_data *drv = bss->drv;
  5336. char name[IFNAMSIZ + 1];
  5337. os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
  5338. if (ifname_wds)
  5339. os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
  5340. wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
  5341. " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
  5342. if (val) {
  5343. if (!if_nametoindex(name)) {
  5344. if (nl80211_create_iface(drv, name,
  5345. NL80211_IFTYPE_AP_VLAN,
  5346. bss->addr, 1, NULL, NULL, 0) <
  5347. 0)
  5348. return -1;
  5349. if (bridge_ifname &&
  5350. linux_br_add_if(drv->global->ioctl_sock,
  5351. bridge_ifname, name) < 0)
  5352. return -1;
  5353. }
  5354. if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
  5355. wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
  5356. "interface %s up", name);
  5357. }
  5358. return i802_set_sta_vlan(priv, addr, name, 0);
  5359. } else {
  5360. if (bridge_ifname)
  5361. linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
  5362. name);
  5363. i802_set_sta_vlan(priv, addr, bss->ifname, 0);
  5364. nl80211_remove_iface(drv, if_nametoindex(name));
  5365. return 0;
  5366. }
  5367. }
  5368. static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
  5369. {
  5370. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  5371. struct sockaddr_ll lladdr;
  5372. unsigned char buf[3000];
  5373. int len;
  5374. socklen_t fromlen = sizeof(lladdr);
  5375. len = recvfrom(sock, buf, sizeof(buf), 0,
  5376. (struct sockaddr *)&lladdr, &fromlen);
  5377. if (len < 0) {
  5378. wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
  5379. strerror(errno));
  5380. return;
  5381. }
  5382. if (have_ifidx(drv, lladdr.sll_ifindex, IFIDX_ANY))
  5383. drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
  5384. }
  5385. static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
  5386. struct i802_bss *bss,
  5387. const char *brname, const char *ifname)
  5388. {
  5389. int br_ifindex;
  5390. char in_br[IFNAMSIZ];
  5391. os_strlcpy(bss->brname, brname, IFNAMSIZ);
  5392. br_ifindex = if_nametoindex(brname);
  5393. if (br_ifindex == 0) {
  5394. /*
  5395. * Bridge was configured, but the bridge device does
  5396. * not exist. Try to add it now.
  5397. */
  5398. if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
  5399. wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
  5400. "bridge interface %s: %s",
  5401. brname, strerror(errno));
  5402. return -1;
  5403. }
  5404. bss->added_bridge = 1;
  5405. br_ifindex = if_nametoindex(brname);
  5406. add_ifidx(drv, br_ifindex, drv->ifindex);
  5407. }
  5408. bss->br_ifindex = br_ifindex;
  5409. if (linux_br_get(in_br, ifname) == 0) {
  5410. if (os_strcmp(in_br, brname) == 0)
  5411. return 0; /* already in the bridge */
  5412. wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
  5413. "bridge %s", ifname, in_br);
  5414. if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
  5415. 0) {
  5416. wpa_printf(MSG_ERROR, "nl80211: Failed to "
  5417. "remove interface %s from bridge "
  5418. "%s: %s",
  5419. ifname, brname, strerror(errno));
  5420. return -1;
  5421. }
  5422. }
  5423. wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
  5424. ifname, brname);
  5425. if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
  5426. wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
  5427. "into bridge %s: %s",
  5428. ifname, brname, strerror(errno));
  5429. return -1;
  5430. }
  5431. bss->added_if_into_bridge = 1;
  5432. return 0;
  5433. }
  5434. static void *i802_init(struct hostapd_data *hapd,
  5435. struct wpa_init_params *params)
  5436. {
  5437. struct wpa_driver_nl80211_data *drv;
  5438. struct i802_bss *bss;
  5439. size_t i;
  5440. char master_ifname[IFNAMSIZ];
  5441. int ifindex, br_ifindex = 0;
  5442. int br_added = 0;
  5443. bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
  5444. params->global_priv, 1,
  5445. params->bssid, params->driver_params);
  5446. if (bss == NULL)
  5447. return NULL;
  5448. drv = bss->drv;
  5449. if (linux_br_get(master_ifname, params->ifname) == 0) {
  5450. wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
  5451. params->ifname, master_ifname);
  5452. br_ifindex = if_nametoindex(master_ifname);
  5453. os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
  5454. } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
  5455. linux_master_get(master_ifname, params->ifname) == 0) {
  5456. wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
  5457. params->ifname, master_ifname);
  5458. /* start listening for EAPOL on the master interface */
  5459. add_ifidx(drv, if_nametoindex(master_ifname), drv->ifindex);
  5460. /* check if master itself is under bridge */
  5461. if (linux_br_get(master_ifname, master_ifname) == 0) {
  5462. wpa_printf(MSG_DEBUG, "nl80211: which is in bridge %s",
  5463. master_ifname);
  5464. br_ifindex = if_nametoindex(master_ifname);
  5465. os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
  5466. }
  5467. } else {
  5468. master_ifname[0] = '\0';
  5469. }
  5470. bss->br_ifindex = br_ifindex;
  5471. for (i = 0; i < params->num_bridge; i++) {
  5472. if (params->bridge[i]) {
  5473. ifindex = if_nametoindex(params->bridge[i]);
  5474. if (ifindex)
  5475. add_ifidx(drv, ifindex, drv->ifindex);
  5476. if (ifindex == br_ifindex)
  5477. br_added = 1;
  5478. }
  5479. }
  5480. /* start listening for EAPOL on the default AP interface */
  5481. add_ifidx(drv, drv->ifindex, IFIDX_ANY);
  5482. if (params->num_bridge && params->bridge[0]) {
  5483. if (i802_check_bridge(drv, bss, params->bridge[0],
  5484. params->ifname) < 0)
  5485. goto failed;
  5486. if (os_strcmp(params->bridge[0], master_ifname) != 0)
  5487. br_added = 1;
  5488. }
  5489. if (!br_added && br_ifindex &&
  5490. (params->num_bridge == 0 || !params->bridge[0]))
  5491. add_ifidx(drv, br_ifindex, drv->ifindex);
  5492. #ifdef CONFIG_LIBNL3_ROUTE
  5493. if (bss->added_if_into_bridge) {
  5494. drv->rtnl_sk = nl_socket_alloc();
  5495. if (drv->rtnl_sk == NULL) {
  5496. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
  5497. goto failed;
  5498. }
  5499. if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
  5500. wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
  5501. strerror(errno));
  5502. goto failed;
  5503. }
  5504. }
  5505. #endif /* CONFIG_LIBNL3_ROUTE */
  5506. drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
  5507. if (drv->eapol_sock < 0) {
  5508. wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
  5509. strerror(errno));
  5510. goto failed;
  5511. }
  5512. if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
  5513. {
  5514. wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
  5515. goto failed;
  5516. }
  5517. if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  5518. params->own_addr))
  5519. goto failed;
  5520. os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
  5521. memcpy(bss->addr, params->own_addr, ETH_ALEN);
  5522. return bss;
  5523. failed:
  5524. wpa_driver_nl80211_deinit(bss);
  5525. return NULL;
  5526. }
  5527. static void i802_deinit(void *priv)
  5528. {
  5529. struct i802_bss *bss = priv;
  5530. wpa_driver_nl80211_deinit(bss);
  5531. }
  5532. static enum nl80211_iftype wpa_driver_nl80211_if_type(
  5533. enum wpa_driver_if_type type)
  5534. {
  5535. switch (type) {
  5536. case WPA_IF_STATION:
  5537. return NL80211_IFTYPE_STATION;
  5538. case WPA_IF_P2P_CLIENT:
  5539. case WPA_IF_P2P_GROUP:
  5540. return NL80211_IFTYPE_P2P_CLIENT;
  5541. case WPA_IF_AP_VLAN:
  5542. return NL80211_IFTYPE_AP_VLAN;
  5543. case WPA_IF_AP_BSS:
  5544. return NL80211_IFTYPE_AP;
  5545. case WPA_IF_P2P_GO:
  5546. return NL80211_IFTYPE_P2P_GO;
  5547. case WPA_IF_P2P_DEVICE:
  5548. return NL80211_IFTYPE_P2P_DEVICE;
  5549. case WPA_IF_MESH:
  5550. return NL80211_IFTYPE_MESH_POINT;
  5551. default:
  5552. return -1;
  5553. }
  5554. }
  5555. static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
  5556. {
  5557. struct wpa_driver_nl80211_data *drv;
  5558. dl_list_for_each(drv, &global->interfaces,
  5559. struct wpa_driver_nl80211_data, list) {
  5560. if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
  5561. return 1;
  5562. }
  5563. return 0;
  5564. }
  5565. static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
  5566. {
  5567. unsigned int idx;
  5568. if (!drv->global)
  5569. return -1;
  5570. os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
  5571. for (idx = 0; idx < 64; idx++) {
  5572. new_addr[0] = drv->first_bss->addr[0] | 0x02;
  5573. new_addr[0] ^= idx << 2;
  5574. if (!nl80211_addr_in_use(drv->global, new_addr))
  5575. break;
  5576. }
  5577. if (idx == 64)
  5578. return -1;
  5579. wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
  5580. MACSTR, MAC2STR(new_addr));
  5581. return 0;
  5582. }
  5583. struct wdev_info {
  5584. u64 wdev_id;
  5585. int wdev_id_set;
  5586. u8 macaddr[ETH_ALEN];
  5587. };
  5588. static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
  5589. {
  5590. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  5591. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  5592. struct wdev_info *wi = arg;
  5593. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  5594. genlmsg_attrlen(gnlh, 0), NULL);
  5595. if (tb[NL80211_ATTR_WDEV]) {
  5596. wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
  5597. wi->wdev_id_set = 1;
  5598. }
  5599. if (tb[NL80211_ATTR_MAC])
  5600. os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
  5601. ETH_ALEN);
  5602. return NL_SKIP;
  5603. }
  5604. static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
  5605. const char *ifname, const u8 *addr,
  5606. void *bss_ctx, void **drv_priv,
  5607. char *force_ifname, u8 *if_addr,
  5608. const char *bridge, int use_existing,
  5609. int setup_ap)
  5610. {
  5611. enum nl80211_iftype nlmode;
  5612. struct i802_bss *bss = priv;
  5613. struct wpa_driver_nl80211_data *drv = bss->drv;
  5614. int ifidx;
  5615. int added = 1;
  5616. if (addr)
  5617. os_memcpy(if_addr, addr, ETH_ALEN);
  5618. nlmode = wpa_driver_nl80211_if_type(type);
  5619. if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
  5620. struct wdev_info p2pdev_info;
  5621. os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
  5622. ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
  5623. 0, nl80211_wdev_handler,
  5624. &p2pdev_info, use_existing);
  5625. if (!p2pdev_info.wdev_id_set || ifidx != 0) {
  5626. wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
  5627. ifname);
  5628. return -1;
  5629. }
  5630. drv->global->if_add_wdevid = p2pdev_info.wdev_id;
  5631. drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
  5632. if (!is_zero_ether_addr(p2pdev_info.macaddr))
  5633. os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
  5634. wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
  5635. ifname,
  5636. (long long unsigned int) p2pdev_info.wdev_id);
  5637. } else {
  5638. ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
  5639. 0, NULL, NULL, use_existing);
  5640. if (use_existing && ifidx == -ENFILE) {
  5641. added = 0;
  5642. ifidx = if_nametoindex(ifname);
  5643. } else if (ifidx < 0) {
  5644. return -1;
  5645. }
  5646. }
  5647. if (!addr) {
  5648. if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
  5649. os_memcpy(if_addr, bss->addr, ETH_ALEN);
  5650. else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
  5651. ifname, if_addr) < 0) {
  5652. if (added)
  5653. nl80211_remove_iface(drv, ifidx);
  5654. return -1;
  5655. }
  5656. }
  5657. if (!addr &&
  5658. (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
  5659. type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
  5660. type == WPA_IF_STATION)) {
  5661. /* Enforce unique address */
  5662. u8 new_addr[ETH_ALEN];
  5663. if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
  5664. new_addr) < 0) {
  5665. if (added)
  5666. nl80211_remove_iface(drv, ifidx);
  5667. return -1;
  5668. }
  5669. if (nl80211_addr_in_use(drv->global, new_addr)) {
  5670. wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
  5671. "for interface %s type %d", ifname, type);
  5672. if (nl80211_vif_addr(drv, new_addr) < 0) {
  5673. if (added)
  5674. nl80211_remove_iface(drv, ifidx);
  5675. return -1;
  5676. }
  5677. if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
  5678. new_addr) < 0) {
  5679. if (added)
  5680. nl80211_remove_iface(drv, ifidx);
  5681. return -1;
  5682. }
  5683. }
  5684. os_memcpy(if_addr, new_addr, ETH_ALEN);
  5685. }
  5686. if (type == WPA_IF_AP_BSS && setup_ap) {
  5687. struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
  5688. if (new_bss == NULL) {
  5689. if (added)
  5690. nl80211_remove_iface(drv, ifidx);
  5691. return -1;
  5692. }
  5693. if (bridge &&
  5694. i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
  5695. wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
  5696. "interface %s to a bridge %s",
  5697. ifname, bridge);
  5698. if (added)
  5699. nl80211_remove_iface(drv, ifidx);
  5700. os_free(new_bss);
  5701. return -1;
  5702. }
  5703. if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
  5704. {
  5705. if (added)
  5706. nl80211_remove_iface(drv, ifidx);
  5707. os_free(new_bss);
  5708. return -1;
  5709. }
  5710. os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
  5711. os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
  5712. new_bss->ifindex = ifidx;
  5713. new_bss->drv = drv;
  5714. new_bss->next = drv->first_bss->next;
  5715. new_bss->freq = drv->first_bss->freq;
  5716. new_bss->ctx = bss_ctx;
  5717. new_bss->added_if = added;
  5718. drv->first_bss->next = new_bss;
  5719. if (drv_priv)
  5720. *drv_priv = new_bss;
  5721. nl80211_init_bss(new_bss);
  5722. /* Subscribe management frames for this WPA_IF_AP_BSS */
  5723. if (nl80211_setup_ap(new_bss))
  5724. return -1;
  5725. }
  5726. if (drv->global)
  5727. drv->global->if_add_ifindex = ifidx;
  5728. /*
  5729. * Some virtual interfaces need to process EAPOL packets and events on
  5730. * the parent interface. This is used mainly with hostapd.
  5731. */
  5732. if (ifidx > 0 &&
  5733. (drv->hostapd ||
  5734. nlmode == NL80211_IFTYPE_AP_VLAN ||
  5735. nlmode == NL80211_IFTYPE_WDS ||
  5736. nlmode == NL80211_IFTYPE_MONITOR))
  5737. add_ifidx(drv, ifidx, IFIDX_ANY);
  5738. return 0;
  5739. }
  5740. static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
  5741. enum wpa_driver_if_type type,
  5742. const char *ifname)
  5743. {
  5744. struct wpa_driver_nl80211_data *drv = bss->drv;
  5745. int ifindex = if_nametoindex(ifname);
  5746. wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
  5747. __func__, type, ifname, ifindex, bss->added_if);
  5748. if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
  5749. nl80211_remove_iface(drv, ifindex);
  5750. else if (ifindex > 0 && !bss->added_if) {
  5751. struct wpa_driver_nl80211_data *drv2;
  5752. dl_list_for_each(drv2, &drv->global->interfaces,
  5753. struct wpa_driver_nl80211_data, list) {
  5754. del_ifidx(drv2, ifindex, IFIDX_ANY);
  5755. del_ifidx(drv2, IFIDX_ANY, ifindex);
  5756. }
  5757. }
  5758. if (type != WPA_IF_AP_BSS)
  5759. return 0;
  5760. if (bss->added_if_into_bridge) {
  5761. if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
  5762. bss->ifname) < 0)
  5763. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  5764. "interface %s from bridge %s: %s",
  5765. bss->ifname, bss->brname, strerror(errno));
  5766. }
  5767. if (bss->added_bridge) {
  5768. if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
  5769. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  5770. "bridge %s: %s",
  5771. bss->brname, strerror(errno));
  5772. }
  5773. if (bss != drv->first_bss) {
  5774. struct i802_bss *tbss;
  5775. wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
  5776. for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
  5777. if (tbss->next == bss) {
  5778. tbss->next = bss->next;
  5779. /* Unsubscribe management frames */
  5780. nl80211_teardown_ap(bss);
  5781. nl80211_destroy_bss(bss);
  5782. if (!bss->added_if)
  5783. i802_set_iface_flags(bss, 0);
  5784. os_free(bss);
  5785. bss = NULL;
  5786. break;
  5787. }
  5788. }
  5789. if (bss)
  5790. wpa_printf(MSG_INFO, "nl80211: %s - could not find "
  5791. "BSS %p in the list", __func__, bss);
  5792. } else {
  5793. wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
  5794. nl80211_teardown_ap(bss);
  5795. if (!bss->added_if && !drv->first_bss->next)
  5796. wpa_driver_nl80211_del_beacon(bss);
  5797. nl80211_destroy_bss(bss);
  5798. if (!bss->added_if)
  5799. i802_set_iface_flags(bss, 0);
  5800. if (drv->first_bss->next) {
  5801. drv->first_bss = drv->first_bss->next;
  5802. drv->ctx = drv->first_bss->ctx;
  5803. os_free(bss);
  5804. } else {
  5805. wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
  5806. }
  5807. }
  5808. return 0;
  5809. }
  5810. static int cookie_handler(struct nl_msg *msg, void *arg)
  5811. {
  5812. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  5813. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  5814. u64 *cookie = arg;
  5815. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  5816. genlmsg_attrlen(gnlh, 0), NULL);
  5817. if (tb[NL80211_ATTR_COOKIE])
  5818. *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
  5819. return NL_SKIP;
  5820. }
  5821. static int nl80211_send_frame_cmd(struct i802_bss *bss,
  5822. unsigned int freq, unsigned int wait,
  5823. const u8 *buf, size_t buf_len,
  5824. u64 *cookie_out, int no_cck, int no_ack,
  5825. int offchanok, const u16 *csa_offs,
  5826. size_t csa_offs_len)
  5827. {
  5828. struct wpa_driver_nl80211_data *drv = bss->drv;
  5829. struct nl_msg *msg;
  5830. u64 cookie;
  5831. int ret = -1;
  5832. wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
  5833. "no_ack=%d offchanok=%d",
  5834. freq, wait, no_cck, no_ack, offchanok);
  5835. wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
  5836. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
  5837. (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
  5838. (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
  5839. (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
  5840. drv->test_use_roc_tx) &&
  5841. nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
  5842. (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
  5843. (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
  5844. (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
  5845. csa_offs_len * sizeof(u16), csa_offs)) ||
  5846. nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
  5847. goto fail;
  5848. cookie = 0;
  5849. ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
  5850. msg = NULL;
  5851. if (ret) {
  5852. wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
  5853. "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
  5854. freq, wait);
  5855. } else {
  5856. wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
  5857. "cookie 0x%llx", no_ack ? " (no ACK)" : "",
  5858. (long long unsigned int) cookie);
  5859. if (cookie_out)
  5860. *cookie_out = no_ack ? (u64) -1 : cookie;
  5861. if (drv->num_send_action_cookies == MAX_SEND_ACTION_COOKIES) {
  5862. wpa_printf(MSG_DEBUG,
  5863. "nl80211: Drop oldest pending send action cookie 0x%llx",
  5864. (long long unsigned int)
  5865. drv->send_action_cookies[0]);
  5866. os_memmove(&drv->send_action_cookies[0],
  5867. &drv->send_action_cookies[1],
  5868. (MAX_SEND_ACTION_COOKIES - 1) *
  5869. sizeof(u64));
  5870. drv->num_send_action_cookies--;
  5871. }
  5872. drv->send_action_cookies[drv->num_send_action_cookies] = cookie;
  5873. drv->num_send_action_cookies++;
  5874. }
  5875. fail:
  5876. nlmsg_free(msg);
  5877. return ret;
  5878. }
  5879. static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
  5880. unsigned int freq,
  5881. unsigned int wait_time,
  5882. const u8 *dst, const u8 *src,
  5883. const u8 *bssid,
  5884. const u8 *data, size_t data_len,
  5885. int no_cck)
  5886. {
  5887. struct wpa_driver_nl80211_data *drv = bss->drv;
  5888. int ret = -1;
  5889. u8 *buf;
  5890. struct ieee80211_hdr *hdr;
  5891. wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
  5892. "freq=%u MHz wait=%d ms no_cck=%d)",
  5893. drv->ifindex, freq, wait_time, no_cck);
  5894. buf = os_zalloc(24 + data_len);
  5895. if (buf == NULL)
  5896. return ret;
  5897. os_memcpy(buf + 24, data, data_len);
  5898. hdr = (struct ieee80211_hdr *) buf;
  5899. hdr->frame_control =
  5900. IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
  5901. os_memcpy(hdr->addr1, dst, ETH_ALEN);
  5902. os_memcpy(hdr->addr2, src, ETH_ALEN);
  5903. os_memcpy(hdr->addr3, bssid, ETH_ALEN);
  5904. if (os_memcmp(bss->addr, src, ETH_ALEN) != 0) {
  5905. wpa_printf(MSG_DEBUG, "nl80211: Use random TA " MACSTR,
  5906. MAC2STR(src));
  5907. os_memcpy(bss->rand_addr, src, ETH_ALEN);
  5908. } else {
  5909. os_memset(bss->rand_addr, 0, ETH_ALEN);
  5910. }
  5911. if (is_ap_interface(drv->nlmode) &&
  5912. (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
  5913. (int) freq == bss->freq || drv->device_ap_sme ||
  5914. !drv->use_monitor))
  5915. ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
  5916. 0, freq, no_cck, 1,
  5917. wait_time, NULL, 0);
  5918. else
  5919. ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
  5920. 24 + data_len,
  5921. &drv->send_action_cookie,
  5922. no_cck, 0, 1, NULL, 0);
  5923. os_free(buf);
  5924. return ret;
  5925. }
  5926. static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
  5927. {
  5928. struct wpa_driver_nl80211_data *drv = bss->drv;
  5929. struct nl_msg *msg;
  5930. int ret;
  5931. wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
  5932. (long long unsigned int) cookie);
  5933. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
  5934. nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
  5935. nlmsg_free(msg);
  5936. return;
  5937. }
  5938. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5939. if (ret)
  5940. wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
  5941. "(%s)", ret, strerror(-ret));
  5942. }
  5943. static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
  5944. {
  5945. struct i802_bss *bss = priv;
  5946. struct wpa_driver_nl80211_data *drv = bss->drv;
  5947. unsigned int i;
  5948. u64 cookie;
  5949. /* Cancel the last pending TX cookie */
  5950. nl80211_frame_wait_cancel(bss, drv->send_action_cookie);
  5951. /*
  5952. * Cancel the other pending TX cookies, if any. This is needed since
  5953. * the driver may keep a list of all pending offchannel TX operations
  5954. * and free up the radio only once they have expired or cancelled.
  5955. */
  5956. for (i = drv->num_send_action_cookies; i > 0; i--) {
  5957. cookie = drv->send_action_cookies[i - 1];
  5958. if (cookie != drv->send_action_cookie)
  5959. nl80211_frame_wait_cancel(bss, cookie);
  5960. }
  5961. drv->num_send_action_cookies = 0;
  5962. }
  5963. static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
  5964. unsigned int duration)
  5965. {
  5966. struct i802_bss *bss = priv;
  5967. struct wpa_driver_nl80211_data *drv = bss->drv;
  5968. struct nl_msg *msg;
  5969. int ret;
  5970. u64 cookie;
  5971. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
  5972. nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
  5973. nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
  5974. nlmsg_free(msg);
  5975. return -1;
  5976. }
  5977. cookie = 0;
  5978. ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
  5979. if (ret == 0) {
  5980. wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
  5981. "0x%llx for freq=%u MHz duration=%u",
  5982. (long long unsigned int) cookie, freq, duration);
  5983. drv->remain_on_chan_cookie = cookie;
  5984. drv->pending_remain_on_chan = 1;
  5985. return 0;
  5986. }
  5987. wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
  5988. "(freq=%d duration=%u): %d (%s)",
  5989. freq, duration, ret, strerror(-ret));
  5990. return -1;
  5991. }
  5992. static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
  5993. {
  5994. struct i802_bss *bss = priv;
  5995. struct wpa_driver_nl80211_data *drv = bss->drv;
  5996. struct nl_msg *msg;
  5997. int ret;
  5998. if (!drv->pending_remain_on_chan) {
  5999. wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
  6000. "to cancel");
  6001. return -1;
  6002. }
  6003. wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
  6004. "0x%llx",
  6005. (long long unsigned int) drv->remain_on_chan_cookie);
  6006. msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
  6007. if (!msg ||
  6008. nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
  6009. nlmsg_free(msg);
  6010. return -1;
  6011. }
  6012. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6013. if (ret == 0)
  6014. return 0;
  6015. wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
  6016. "%d (%s)", ret, strerror(-ret));
  6017. return -1;
  6018. }
  6019. static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
  6020. {
  6021. struct wpa_driver_nl80211_data *drv = bss->drv;
  6022. if (!report) {
  6023. if (bss->nl_preq && drv->device_ap_sme &&
  6024. is_ap_interface(drv->nlmode) && !bss->in_deinit &&
  6025. !bss->static_ap) {
  6026. /*
  6027. * Do not disable Probe Request reporting that was
  6028. * enabled in nl80211_setup_ap().
  6029. */
  6030. wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
  6031. "Probe Request reporting nl_preq=%p while "
  6032. "in AP mode", bss->nl_preq);
  6033. } else if (bss->nl_preq) {
  6034. wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
  6035. "reporting nl_preq=%p", bss->nl_preq);
  6036. nl80211_destroy_eloop_handle(&bss->nl_preq);
  6037. }
  6038. return 0;
  6039. }
  6040. if (bss->nl_preq) {
  6041. wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
  6042. "already on! nl_preq=%p", bss->nl_preq);
  6043. return 0;
  6044. }
  6045. bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
  6046. if (bss->nl_preq == NULL)
  6047. return -1;
  6048. wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
  6049. "reporting nl_preq=%p", bss->nl_preq);
  6050. if (nl80211_register_frame(bss, bss->nl_preq,
  6051. (WLAN_FC_TYPE_MGMT << 2) |
  6052. (WLAN_FC_STYPE_PROBE_REQ << 4),
  6053. NULL, 0) < 0)
  6054. goto out_err;
  6055. nl80211_register_eloop_read(&bss->nl_preq,
  6056. wpa_driver_nl80211_event_receive,
  6057. bss->nl_cb);
  6058. return 0;
  6059. out_err:
  6060. nl_destroy_handles(&bss->nl_preq);
  6061. return -1;
  6062. }
  6063. static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
  6064. int ifindex, int disabled)
  6065. {
  6066. struct nl_msg *msg;
  6067. struct nlattr *bands, *band;
  6068. int ret;
  6069. wpa_printf(MSG_DEBUG,
  6070. "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
  6071. ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
  6072. "no NL80211_TXRATE_LEGACY constraint");
  6073. msg = nl80211_ifindex_msg(drv, ifindex, 0,
  6074. NL80211_CMD_SET_TX_BITRATE_MASK);
  6075. if (!msg)
  6076. return -1;
  6077. bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
  6078. if (!bands)
  6079. goto fail;
  6080. /*
  6081. * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
  6082. * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
  6083. * rates. All 5 GHz rates are left enabled.
  6084. */
  6085. band = nla_nest_start(msg, NL80211_BAND_2GHZ);
  6086. if (!band ||
  6087. (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
  6088. "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
  6089. goto fail;
  6090. nla_nest_end(msg, band);
  6091. nla_nest_end(msg, bands);
  6092. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6093. if (ret) {
  6094. wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
  6095. "(%s)", ret, strerror(-ret));
  6096. } else
  6097. drv->disabled_11b_rates = disabled;
  6098. return ret;
  6099. fail:
  6100. nlmsg_free(msg);
  6101. return -1;
  6102. }
  6103. static int wpa_driver_nl80211_deinit_ap(void *priv)
  6104. {
  6105. struct i802_bss *bss = priv;
  6106. struct wpa_driver_nl80211_data *drv = bss->drv;
  6107. if (!is_ap_interface(drv->nlmode))
  6108. return -1;
  6109. wpa_driver_nl80211_del_beacon(bss);
  6110. bss->beacon_set = 0;
  6111. /*
  6112. * If the P2P GO interface was dynamically added, then it is
  6113. * possible that the interface change to station is not possible.
  6114. */
  6115. if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
  6116. return 0;
  6117. return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
  6118. }
  6119. static int wpa_driver_nl80211_stop_ap(void *priv)
  6120. {
  6121. struct i802_bss *bss = priv;
  6122. struct wpa_driver_nl80211_data *drv = bss->drv;
  6123. if (!is_ap_interface(drv->nlmode))
  6124. return -1;
  6125. wpa_driver_nl80211_del_beacon(bss);
  6126. bss->beacon_set = 0;
  6127. return 0;
  6128. }
  6129. static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
  6130. {
  6131. struct i802_bss *bss = priv;
  6132. struct wpa_driver_nl80211_data *drv = bss->drv;
  6133. if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
  6134. return -1;
  6135. /*
  6136. * If the P2P Client interface was dynamically added, then it is
  6137. * possible that the interface change to station is not possible.
  6138. */
  6139. if (bss->if_dynamic)
  6140. return 0;
  6141. return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
  6142. }
  6143. static void wpa_driver_nl80211_resume(void *priv)
  6144. {
  6145. struct i802_bss *bss = priv;
  6146. enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
  6147. if (i802_set_iface_flags(bss, 1))
  6148. wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
  6149. if (is_p2p_net_interface(nlmode))
  6150. nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
  6151. }
  6152. static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
  6153. {
  6154. struct i802_bss *bss = priv;
  6155. struct wpa_driver_nl80211_data *drv = bss->drv;
  6156. struct nl_msg *msg;
  6157. struct nlattr *cqm;
  6158. wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
  6159. "hysteresis=%d", threshold, hysteresis);
  6160. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
  6161. !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
  6162. nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
  6163. nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
  6164. nlmsg_free(msg);
  6165. return -1;
  6166. }
  6167. nla_nest_end(msg, cqm);
  6168. return send_and_recv_msgs(drv, msg, NULL, NULL);
  6169. }
  6170. static int get_channel_width(struct nl_msg *msg, void *arg)
  6171. {
  6172. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  6173. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  6174. struct wpa_signal_info *sig_change = arg;
  6175. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  6176. genlmsg_attrlen(gnlh, 0), NULL);
  6177. sig_change->center_frq1 = -1;
  6178. sig_change->center_frq2 = -1;
  6179. sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
  6180. if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
  6181. sig_change->chanwidth = convert2width(
  6182. nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
  6183. if (tb[NL80211_ATTR_CENTER_FREQ1])
  6184. sig_change->center_frq1 =
  6185. nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
  6186. if (tb[NL80211_ATTR_CENTER_FREQ2])
  6187. sig_change->center_frq2 =
  6188. nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
  6189. }
  6190. return NL_SKIP;
  6191. }
  6192. static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
  6193. struct wpa_signal_info *sig)
  6194. {
  6195. struct nl_msg *msg;
  6196. msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
  6197. return send_and_recv_msgs(drv, msg, get_channel_width, sig);
  6198. }
  6199. static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
  6200. {
  6201. struct i802_bss *bss = priv;
  6202. struct wpa_driver_nl80211_data *drv = bss->drv;
  6203. int res;
  6204. os_memset(si, 0, sizeof(*si));
  6205. res = nl80211_get_link_signal(drv, si);
  6206. if (res) {
  6207. if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
  6208. drv->nlmode != NL80211_IFTYPE_MESH_POINT)
  6209. return res;
  6210. si->current_signal = 0;
  6211. }
  6212. res = nl80211_get_channel_width(drv, si);
  6213. if (res != 0)
  6214. return res;
  6215. return nl80211_get_link_noise(drv, si);
  6216. }
  6217. static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
  6218. int encrypt)
  6219. {
  6220. struct i802_bss *bss = priv;
  6221. return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
  6222. 0, 0, 0, 0, NULL, 0);
  6223. }
  6224. static int nl80211_set_param(void *priv, const char *param)
  6225. {
  6226. struct i802_bss *bss = priv;
  6227. struct wpa_driver_nl80211_data *drv = bss->drv;
  6228. if (param == NULL)
  6229. return 0;
  6230. wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
  6231. #ifdef CONFIG_P2P
  6232. if (os_strstr(param, "use_p2p_group_interface=1")) {
  6233. wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
  6234. "interface");
  6235. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
  6236. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
  6237. }
  6238. #endif /* CONFIG_P2P */
  6239. if (os_strstr(param, "use_monitor=1"))
  6240. drv->use_monitor = 1;
  6241. if (os_strstr(param, "force_connect_cmd=1")) {
  6242. drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
  6243. drv->force_connect_cmd = 1;
  6244. }
  6245. if (os_strstr(param, "force_bss_selection=1"))
  6246. drv->capa.flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
  6247. if (os_strstr(param, "no_offchannel_tx=1")) {
  6248. drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
  6249. drv->test_use_roc_tx = 1;
  6250. }
  6251. return 0;
  6252. }
  6253. static void * nl80211_global_init(void *ctx)
  6254. {
  6255. struct nl80211_global *global;
  6256. struct netlink_config *cfg;
  6257. global = os_zalloc(sizeof(*global));
  6258. if (global == NULL)
  6259. return NULL;
  6260. global->ctx = ctx;
  6261. global->ioctl_sock = -1;
  6262. dl_list_init(&global->interfaces);
  6263. global->if_add_ifindex = -1;
  6264. cfg = os_zalloc(sizeof(*cfg));
  6265. if (cfg == NULL)
  6266. goto err;
  6267. cfg->ctx = global;
  6268. cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
  6269. cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
  6270. global->netlink = netlink_init(cfg);
  6271. if (global->netlink == NULL) {
  6272. os_free(cfg);
  6273. goto err;
  6274. }
  6275. if (wpa_driver_nl80211_init_nl_global(global) < 0)
  6276. goto err;
  6277. global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
  6278. if (global->ioctl_sock < 0) {
  6279. wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
  6280. strerror(errno));
  6281. goto err;
  6282. }
  6283. return global;
  6284. err:
  6285. nl80211_global_deinit(global);
  6286. return NULL;
  6287. }
  6288. static void nl80211_global_deinit(void *priv)
  6289. {
  6290. struct nl80211_global *global = priv;
  6291. if (global == NULL)
  6292. return;
  6293. if (!dl_list_empty(&global->interfaces)) {
  6294. wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
  6295. "nl80211_global_deinit",
  6296. dl_list_len(&global->interfaces));
  6297. }
  6298. if (global->netlink)
  6299. netlink_deinit(global->netlink);
  6300. nl_destroy_handles(&global->nl);
  6301. if (global->nl_event)
  6302. nl80211_destroy_eloop_handle(&global->nl_event);
  6303. nl_cb_put(global->nl_cb);
  6304. if (global->ioctl_sock >= 0)
  6305. close(global->ioctl_sock);
  6306. os_free(global);
  6307. }
  6308. static const char * nl80211_get_radio_name(void *priv)
  6309. {
  6310. struct i802_bss *bss = priv;
  6311. struct wpa_driver_nl80211_data *drv = bss->drv;
  6312. return drv->phyname;
  6313. }
  6314. static int nl80211_pmkid(struct i802_bss *bss, int cmd,
  6315. struct wpa_pmkid_params *params)
  6316. {
  6317. struct nl_msg *msg;
  6318. if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
  6319. (params->pmkid &&
  6320. nla_put(msg, NL80211_ATTR_PMKID, 16, params->pmkid)) ||
  6321. (params->bssid &&
  6322. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid)) ||
  6323. (params->ssid_len &&
  6324. nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) ||
  6325. (params->fils_cache_id &&
  6326. nla_put(msg, NL80211_ATTR_FILS_CACHE_ID, 2,
  6327. params->fils_cache_id)) ||
  6328. (params->pmk_len &&
  6329. nla_put(msg, NL80211_ATTR_PMK, params->pmk_len, params->pmk))) {
  6330. nlmsg_free(msg);
  6331. return -ENOBUFS;
  6332. }
  6333. return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  6334. }
  6335. static int nl80211_add_pmkid(void *priv, struct wpa_pmkid_params *params)
  6336. {
  6337. struct i802_bss *bss = priv;
  6338. if (params->bssid)
  6339. wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR,
  6340. MAC2STR(params->bssid));
  6341. else if (params->fils_cache_id && params->ssid_len) {
  6342. wpa_printf(MSG_DEBUG,
  6343. "nl80211: Add PMKSA for cache id %02x%02x SSID %s",
  6344. params->fils_cache_id[0], params->fils_cache_id[1],
  6345. wpa_ssid_txt(params->ssid, params->ssid_len));
  6346. }
  6347. return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, params);
  6348. }
  6349. static int nl80211_remove_pmkid(void *priv, struct wpa_pmkid_params *params)
  6350. {
  6351. struct i802_bss *bss = priv;
  6352. if (params->bssid)
  6353. wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
  6354. MAC2STR(params->bssid));
  6355. else if (params->fils_cache_id && params->ssid_len) {
  6356. wpa_printf(MSG_DEBUG,
  6357. "nl80211: Delete PMKSA for cache id %02x%02x SSID %s",
  6358. params->fils_cache_id[0], params->fils_cache_id[1],
  6359. wpa_ssid_txt(params->ssid, params->ssid_len));
  6360. }
  6361. return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, params);
  6362. }
  6363. static int nl80211_flush_pmkid(void *priv)
  6364. {
  6365. struct i802_bss *bss = priv;
  6366. struct nl_msg *msg;
  6367. wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
  6368. msg = nl80211_bss_msg(bss, 0, NL80211_CMD_FLUSH_PMKSA);
  6369. if (!msg)
  6370. return -ENOBUFS;
  6371. return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  6372. }
  6373. static void clean_survey_results(struct survey_results *survey_results)
  6374. {
  6375. struct freq_survey *survey, *tmp;
  6376. if (dl_list_empty(&survey_results->survey_list))
  6377. return;
  6378. dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
  6379. struct freq_survey, list) {
  6380. dl_list_del(&survey->list);
  6381. os_free(survey);
  6382. }
  6383. }
  6384. static void add_survey(struct nlattr **sinfo, u32 ifidx,
  6385. struct dl_list *survey_list)
  6386. {
  6387. struct freq_survey *survey;
  6388. survey = os_zalloc(sizeof(struct freq_survey));
  6389. if (!survey)
  6390. return;
  6391. survey->ifidx = ifidx;
  6392. survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
  6393. survey->filled = 0;
  6394. if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
  6395. survey->nf = (int8_t)
  6396. nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
  6397. survey->filled |= SURVEY_HAS_NF;
  6398. }
  6399. if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
  6400. survey->channel_time =
  6401. nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
  6402. survey->filled |= SURVEY_HAS_CHAN_TIME;
  6403. }
  6404. if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
  6405. survey->channel_time_busy =
  6406. nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
  6407. survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
  6408. }
  6409. if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
  6410. survey->channel_time_rx =
  6411. nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
  6412. survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
  6413. }
  6414. if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
  6415. survey->channel_time_tx =
  6416. nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
  6417. survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
  6418. }
  6419. wpa_printf(MSG_DEBUG, "nl80211: Freq survey dump event (freq=%d MHz noise=%d channel_time=%ld busy_time=%ld tx_time=%ld rx_time=%ld filled=%04x)",
  6420. survey->freq,
  6421. survey->nf,
  6422. (unsigned long int) survey->channel_time,
  6423. (unsigned long int) survey->channel_time_busy,
  6424. (unsigned long int) survey->channel_time_tx,
  6425. (unsigned long int) survey->channel_time_rx,
  6426. survey->filled);
  6427. dl_list_add_tail(survey_list, &survey->list);
  6428. }
  6429. static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
  6430. unsigned int freq_filter)
  6431. {
  6432. if (!freq_filter)
  6433. return 1;
  6434. return freq_filter == surveyed_freq;
  6435. }
  6436. static int survey_handler(struct nl_msg *msg, void *arg)
  6437. {
  6438. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  6439. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  6440. struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
  6441. struct survey_results *survey_results;
  6442. u32 surveyed_freq = 0;
  6443. u32 ifidx;
  6444. static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
  6445. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  6446. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  6447. };
  6448. survey_results = (struct survey_results *) arg;
  6449. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  6450. genlmsg_attrlen(gnlh, 0), NULL);
  6451. if (!tb[NL80211_ATTR_IFINDEX])
  6452. return NL_SKIP;
  6453. ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
  6454. if (!tb[NL80211_ATTR_SURVEY_INFO])
  6455. return NL_SKIP;
  6456. if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
  6457. tb[NL80211_ATTR_SURVEY_INFO],
  6458. survey_policy))
  6459. return NL_SKIP;
  6460. if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
  6461. wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
  6462. return NL_SKIP;
  6463. }
  6464. surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
  6465. if (!check_survey_ok(sinfo, surveyed_freq,
  6466. survey_results->freq_filter))
  6467. return NL_SKIP;
  6468. if (survey_results->freq_filter &&
  6469. survey_results->freq_filter != surveyed_freq) {
  6470. wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
  6471. surveyed_freq);
  6472. return NL_SKIP;
  6473. }
  6474. add_survey(sinfo, ifidx, &survey_results->survey_list);
  6475. return NL_SKIP;
  6476. }
  6477. static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
  6478. {
  6479. struct i802_bss *bss = priv;
  6480. struct wpa_driver_nl80211_data *drv = bss->drv;
  6481. struct nl_msg *msg;
  6482. int err;
  6483. union wpa_event_data data;
  6484. struct survey_results *survey_results;
  6485. os_memset(&data, 0, sizeof(data));
  6486. survey_results = &data.survey_results;
  6487. dl_list_init(&survey_results->survey_list);
  6488. msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
  6489. if (!msg)
  6490. return -ENOBUFS;
  6491. if (freq)
  6492. data.survey_results.freq_filter = freq;
  6493. do {
  6494. wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
  6495. err = send_and_recv_msgs(drv, msg, survey_handler,
  6496. survey_results);
  6497. } while (err > 0);
  6498. if (err)
  6499. wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
  6500. else
  6501. wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
  6502. clean_survey_results(survey_results);
  6503. return err;
  6504. }
  6505. static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
  6506. const u8 *kck, size_t kck_len,
  6507. const u8 *replay_ctr)
  6508. {
  6509. struct i802_bss *bss = priv;
  6510. struct wpa_driver_nl80211_data *drv = bss->drv;
  6511. struct nlattr *replay_nested;
  6512. struct nl_msg *msg;
  6513. int ret;
  6514. if (!drv->set_rekey_offload)
  6515. return;
  6516. wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
  6517. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
  6518. !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
  6519. nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
  6520. (kck_len && nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck)) ||
  6521. nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
  6522. replay_ctr)) {
  6523. nl80211_nlmsg_clear(msg);
  6524. nlmsg_free(msg);
  6525. return;
  6526. }
  6527. nla_nest_end(msg, replay_nested);
  6528. ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
  6529. if (ret == -EOPNOTSUPP) {
  6530. wpa_printf(MSG_DEBUG,
  6531. "nl80211: Driver does not support rekey offload");
  6532. drv->set_rekey_offload = 0;
  6533. }
  6534. }
  6535. static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
  6536. const u8 *addr, int qos)
  6537. {
  6538. /* send data frame to poll STA and check whether
  6539. * this frame is ACKed */
  6540. struct {
  6541. struct ieee80211_hdr hdr;
  6542. u16 qos_ctl;
  6543. } STRUCT_PACKED nulldata;
  6544. size_t size;
  6545. /* Send data frame to poll STA and check whether this frame is ACKed */
  6546. os_memset(&nulldata, 0, sizeof(nulldata));
  6547. if (qos) {
  6548. nulldata.hdr.frame_control =
  6549. IEEE80211_FC(WLAN_FC_TYPE_DATA,
  6550. WLAN_FC_STYPE_QOS_NULL);
  6551. size = sizeof(nulldata);
  6552. } else {
  6553. nulldata.hdr.frame_control =
  6554. IEEE80211_FC(WLAN_FC_TYPE_DATA,
  6555. WLAN_FC_STYPE_NULLFUNC);
  6556. size = sizeof(struct ieee80211_hdr);
  6557. }
  6558. nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
  6559. os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
  6560. os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
  6561. os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
  6562. if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
  6563. 0, 0, NULL, 0) < 0)
  6564. wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
  6565. "send poll frame");
  6566. }
  6567. static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
  6568. int qos)
  6569. {
  6570. struct i802_bss *bss = priv;
  6571. struct wpa_driver_nl80211_data *drv = bss->drv;
  6572. struct nl_msg *msg;
  6573. int ret;
  6574. if (!drv->poll_command_supported) {
  6575. nl80211_send_null_frame(bss, own_addr, addr, qos);
  6576. return;
  6577. }
  6578. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
  6579. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
  6580. nlmsg_free(msg);
  6581. return;
  6582. }
  6583. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6584. if (ret < 0) {
  6585. wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
  6586. MACSTR " failed: ret=%d (%s)",
  6587. MAC2STR(addr), ret, strerror(-ret));
  6588. }
  6589. }
  6590. static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
  6591. {
  6592. struct nl_msg *msg;
  6593. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
  6594. nla_put_u32(msg, NL80211_ATTR_PS_STATE,
  6595. enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
  6596. nlmsg_free(msg);
  6597. return -ENOBUFS;
  6598. }
  6599. return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  6600. }
  6601. static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
  6602. int ctwindow)
  6603. {
  6604. struct i802_bss *bss = priv;
  6605. wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
  6606. "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
  6607. if (opp_ps != -1 || ctwindow != -1) {
  6608. #ifdef ANDROID_P2P
  6609. wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
  6610. #else /* ANDROID_P2P */
  6611. return -1; /* Not yet supported */
  6612. #endif /* ANDROID_P2P */
  6613. }
  6614. if (legacy_ps == -1)
  6615. return 0;
  6616. if (legacy_ps != 0 && legacy_ps != 1)
  6617. return -1; /* Not yet supported */
  6618. return nl80211_set_power_save(bss, legacy_ps);
  6619. }
  6620. static int nl80211_start_radar_detection(void *priv,
  6621. struct hostapd_freq_params *freq)
  6622. {
  6623. struct i802_bss *bss = priv;
  6624. struct wpa_driver_nl80211_data *drv = bss->drv;
  6625. struct nl_msg *msg;
  6626. int ret;
  6627. wpa_printf(MSG_DEBUG, "nl80211: Start radar detection (CAC) %d MHz (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
  6628. freq->freq, freq->ht_enabled, freq->vht_enabled,
  6629. freq->bandwidth, freq->center_freq1, freq->center_freq2);
  6630. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
  6631. wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
  6632. "detection");
  6633. return -1;
  6634. }
  6635. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
  6636. nl80211_put_freq_params(msg, freq) < 0) {
  6637. nlmsg_free(msg);
  6638. return -1;
  6639. }
  6640. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6641. if (ret == 0)
  6642. return 0;
  6643. wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
  6644. "%d (%s)", ret, strerror(-ret));
  6645. return -1;
  6646. }
  6647. #ifdef CONFIG_TDLS
  6648. static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
  6649. u8 dialog_token, u16 status_code,
  6650. u32 peer_capab, int initiator, const u8 *buf,
  6651. size_t len)
  6652. {
  6653. struct i802_bss *bss = priv;
  6654. struct wpa_driver_nl80211_data *drv = bss->drv;
  6655. struct nl_msg *msg;
  6656. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
  6657. return -EOPNOTSUPP;
  6658. if (!dst)
  6659. return -EINVAL;
  6660. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
  6661. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
  6662. nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
  6663. nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
  6664. nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
  6665. goto fail;
  6666. if (peer_capab) {
  6667. /*
  6668. * The internal enum tdls_peer_capability definition is
  6669. * currently identical with the nl80211 enum
  6670. * nl80211_tdls_peer_capability, so no conversion is needed
  6671. * here.
  6672. */
  6673. if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
  6674. peer_capab))
  6675. goto fail;
  6676. }
  6677. if ((initiator &&
  6678. nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
  6679. nla_put(msg, NL80211_ATTR_IE, len, buf))
  6680. goto fail;
  6681. return send_and_recv_msgs(drv, msg, NULL, NULL);
  6682. fail:
  6683. nlmsg_free(msg);
  6684. return -ENOBUFS;
  6685. }
  6686. static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
  6687. {
  6688. struct i802_bss *bss = priv;
  6689. struct wpa_driver_nl80211_data *drv = bss->drv;
  6690. struct nl_msg *msg;
  6691. enum nl80211_tdls_operation nl80211_oper;
  6692. int res;
  6693. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
  6694. return -EOPNOTSUPP;
  6695. switch (oper) {
  6696. case TDLS_DISCOVERY_REQ:
  6697. nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
  6698. break;
  6699. case TDLS_SETUP:
  6700. nl80211_oper = NL80211_TDLS_SETUP;
  6701. break;
  6702. case TDLS_TEARDOWN:
  6703. nl80211_oper = NL80211_TDLS_TEARDOWN;
  6704. break;
  6705. case TDLS_ENABLE_LINK:
  6706. nl80211_oper = NL80211_TDLS_ENABLE_LINK;
  6707. break;
  6708. case TDLS_DISABLE_LINK:
  6709. nl80211_oper = NL80211_TDLS_DISABLE_LINK;
  6710. break;
  6711. case TDLS_ENABLE:
  6712. return 0;
  6713. case TDLS_DISABLE:
  6714. return 0;
  6715. default:
  6716. return -EINVAL;
  6717. }
  6718. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
  6719. nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
  6720. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
  6721. nlmsg_free(msg);
  6722. return -ENOBUFS;
  6723. }
  6724. res = send_and_recv_msgs(drv, msg, NULL, NULL);
  6725. wpa_printf(MSG_DEBUG, "nl80211: TDLS_OPER: oper=%d mac=" MACSTR
  6726. " --> res=%d (%s)", nl80211_oper, MAC2STR(peer), res,
  6727. strerror(-res));
  6728. return res;
  6729. }
  6730. static int
  6731. nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
  6732. const struct hostapd_freq_params *params)
  6733. {
  6734. struct i802_bss *bss = priv;
  6735. struct wpa_driver_nl80211_data *drv = bss->drv;
  6736. struct nl_msg *msg;
  6737. int ret = -ENOBUFS;
  6738. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
  6739. !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
  6740. return -EOPNOTSUPP;
  6741. wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
  6742. " oper_class=%u freq=%u",
  6743. MAC2STR(addr), oper_class, params->freq);
  6744. msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
  6745. if (!msg ||
  6746. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
  6747. nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
  6748. (ret = nl80211_put_freq_params(msg, params))) {
  6749. nlmsg_free(msg);
  6750. wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
  6751. return ret;
  6752. }
  6753. return send_and_recv_msgs(drv, msg, NULL, NULL);
  6754. }
  6755. static int
  6756. nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
  6757. {
  6758. struct i802_bss *bss = priv;
  6759. struct wpa_driver_nl80211_data *drv = bss->drv;
  6760. struct nl_msg *msg;
  6761. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
  6762. !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
  6763. return -EOPNOTSUPP;
  6764. wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
  6765. MAC2STR(addr));
  6766. msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
  6767. if (!msg ||
  6768. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
  6769. nlmsg_free(msg);
  6770. wpa_printf(MSG_DEBUG,
  6771. "nl80211: Could not build TDLS cancel chan switch");
  6772. return -ENOBUFS;
  6773. }
  6774. return send_and_recv_msgs(drv, msg, NULL, NULL);
  6775. }
  6776. #endif /* CONFIG TDLS */
  6777. static int driver_nl80211_set_key(const char *ifname, void *priv,
  6778. enum wpa_alg alg, const u8 *addr,
  6779. int key_idx, int set_tx,
  6780. const u8 *seq, size_t seq_len,
  6781. const u8 *key, size_t key_len)
  6782. {
  6783. struct i802_bss *bss = priv;
  6784. return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
  6785. set_tx, seq, seq_len, key, key_len);
  6786. }
  6787. static int driver_nl80211_scan2(void *priv,
  6788. struct wpa_driver_scan_params *params)
  6789. {
  6790. struct i802_bss *bss = priv;
  6791. #ifdef CONFIG_DRIVER_NL80211_QCA
  6792. struct wpa_driver_nl80211_data *drv = bss->drv;
  6793. /*
  6794. * Do a vendor specific scan if possible. If only_new_results is
  6795. * set, do a normal scan since a kernel (cfg80211) BSS cache flush
  6796. * cannot be achieved through a vendor scan. The below condition may
  6797. * need to be modified if new scan flags are added in the future whose
  6798. * functionality can only be achieved through a normal scan.
  6799. */
  6800. if (drv->scan_vendor_cmd_avail && !params->only_new_results)
  6801. return wpa_driver_nl80211_vendor_scan(bss, params);
  6802. #endif /* CONFIG_DRIVER_NL80211_QCA */
  6803. return wpa_driver_nl80211_scan(bss, params);
  6804. }
  6805. static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
  6806. int reason_code)
  6807. {
  6808. struct i802_bss *bss = priv;
  6809. return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
  6810. }
  6811. static int driver_nl80211_authenticate(void *priv,
  6812. struct wpa_driver_auth_params *params)
  6813. {
  6814. struct i802_bss *bss = priv;
  6815. return wpa_driver_nl80211_authenticate(bss, params);
  6816. }
  6817. static void driver_nl80211_deinit(void *priv)
  6818. {
  6819. struct i802_bss *bss = priv;
  6820. wpa_driver_nl80211_deinit(bss);
  6821. }
  6822. static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
  6823. const char *ifname)
  6824. {
  6825. struct i802_bss *bss = priv;
  6826. return wpa_driver_nl80211_if_remove(bss, type, ifname);
  6827. }
  6828. static int driver_nl80211_send_mlme(void *priv, const u8 *data,
  6829. size_t data_len, int noack,
  6830. unsigned int freq,
  6831. const u16 *csa_offs, size_t csa_offs_len)
  6832. {
  6833. struct i802_bss *bss = priv;
  6834. return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
  6835. freq, 0, 0, 0, csa_offs,
  6836. csa_offs_len);
  6837. }
  6838. static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
  6839. {
  6840. struct i802_bss *bss = priv;
  6841. return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
  6842. }
  6843. static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
  6844. const char *ifname, int vlan_id)
  6845. {
  6846. struct i802_bss *bss = priv;
  6847. return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
  6848. }
  6849. static int driver_nl80211_read_sta_data(void *priv,
  6850. struct hostap_sta_driver_data *data,
  6851. const u8 *addr)
  6852. {
  6853. struct i802_bss *bss = priv;
  6854. os_memset(data, 0, sizeof(*data));
  6855. return i802_read_sta_data(bss, data, addr);
  6856. }
  6857. static int driver_nl80211_send_action(void *priv, unsigned int freq,
  6858. unsigned int wait_time,
  6859. const u8 *dst, const u8 *src,
  6860. const u8 *bssid,
  6861. const u8 *data, size_t data_len,
  6862. int no_cck)
  6863. {
  6864. struct i802_bss *bss = priv;
  6865. return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
  6866. bssid, data, data_len, no_cck);
  6867. }
  6868. static int driver_nl80211_probe_req_report(void *priv, int report)
  6869. {
  6870. struct i802_bss *bss = priv;
  6871. return wpa_driver_nl80211_probe_req_report(bss, report);
  6872. }
  6873. static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
  6874. const u8 *ies, size_t ies_len)
  6875. {
  6876. int ret;
  6877. struct nl_msg *msg;
  6878. struct i802_bss *bss = priv;
  6879. struct wpa_driver_nl80211_data *drv = bss->drv;
  6880. u16 mdid = WPA_GET_LE16(md);
  6881. wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
  6882. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
  6883. nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
  6884. nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
  6885. nlmsg_free(msg);
  6886. return -ENOBUFS;
  6887. }
  6888. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6889. if (ret) {
  6890. wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
  6891. "err=%d (%s)", ret, strerror(-ret));
  6892. }
  6893. return ret;
  6894. }
  6895. static const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
  6896. {
  6897. struct i802_bss *bss = priv;
  6898. struct wpa_driver_nl80211_data *drv = bss->drv;
  6899. if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
  6900. return NULL;
  6901. return bss->addr;
  6902. }
  6903. static const char * scan_state_str(enum scan_states scan_state)
  6904. {
  6905. switch (scan_state) {
  6906. case NO_SCAN:
  6907. return "NO_SCAN";
  6908. case SCAN_REQUESTED:
  6909. return "SCAN_REQUESTED";
  6910. case SCAN_STARTED:
  6911. return "SCAN_STARTED";
  6912. case SCAN_COMPLETED:
  6913. return "SCAN_COMPLETED";
  6914. case SCAN_ABORTED:
  6915. return "SCAN_ABORTED";
  6916. case SCHED_SCAN_STARTED:
  6917. return "SCHED_SCAN_STARTED";
  6918. case SCHED_SCAN_STOPPED:
  6919. return "SCHED_SCAN_STOPPED";
  6920. case SCHED_SCAN_RESULTS:
  6921. return "SCHED_SCAN_RESULTS";
  6922. }
  6923. return "??";
  6924. }
  6925. static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
  6926. {
  6927. struct i802_bss *bss = priv;
  6928. struct wpa_driver_nl80211_data *drv = bss->drv;
  6929. int res;
  6930. char *pos, *end;
  6931. pos = buf;
  6932. end = buf + buflen;
  6933. res = os_snprintf(pos, end - pos,
  6934. "ifindex=%d\n"
  6935. "ifname=%s\n"
  6936. "brname=%s\n"
  6937. "addr=" MACSTR "\n"
  6938. "freq=%d\n"
  6939. "%s%s%s%s%s",
  6940. bss->ifindex,
  6941. bss->ifname,
  6942. bss->brname,
  6943. MAC2STR(bss->addr),
  6944. bss->freq,
  6945. bss->beacon_set ? "beacon_set=1\n" : "",
  6946. bss->added_if_into_bridge ?
  6947. "added_if_into_bridge=1\n" : "",
  6948. bss->added_bridge ? "added_bridge=1\n" : "",
  6949. bss->in_deinit ? "in_deinit=1\n" : "",
  6950. bss->if_dynamic ? "if_dynamic=1\n" : "");
  6951. if (os_snprintf_error(end - pos, res))
  6952. return pos - buf;
  6953. pos += res;
  6954. if (bss->wdev_id_set) {
  6955. res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
  6956. (unsigned long long) bss->wdev_id);
  6957. if (os_snprintf_error(end - pos, res))
  6958. return pos - buf;
  6959. pos += res;
  6960. }
  6961. res = os_snprintf(pos, end - pos,
  6962. "phyname=%s\n"
  6963. "perm_addr=" MACSTR "\n"
  6964. "drv_ifindex=%d\n"
  6965. "operstate=%d\n"
  6966. "scan_state=%s\n"
  6967. "auth_bssid=" MACSTR "\n"
  6968. "auth_attempt_bssid=" MACSTR "\n"
  6969. "bssid=" MACSTR "\n"
  6970. "prev_bssid=" MACSTR "\n"
  6971. "associated=%d\n"
  6972. "assoc_freq=%u\n"
  6973. "monitor_sock=%d\n"
  6974. "monitor_ifidx=%d\n"
  6975. "monitor_refcount=%d\n"
  6976. "last_mgmt_freq=%u\n"
  6977. "eapol_tx_sock=%d\n"
  6978. "%s%s%s%s%s%s%s%s%s%s%s%s%s",
  6979. drv->phyname,
  6980. MAC2STR(drv->perm_addr),
  6981. drv->ifindex,
  6982. drv->operstate,
  6983. scan_state_str(drv->scan_state),
  6984. MAC2STR(drv->auth_bssid),
  6985. MAC2STR(drv->auth_attempt_bssid),
  6986. MAC2STR(drv->bssid),
  6987. MAC2STR(drv->prev_bssid),
  6988. drv->associated,
  6989. drv->assoc_freq,
  6990. drv->monitor_sock,
  6991. drv->monitor_ifidx,
  6992. drv->monitor_refcount,
  6993. drv->last_mgmt_freq,
  6994. drv->eapol_tx_sock,
  6995. drv->ignore_if_down_event ?
  6996. "ignore_if_down_event=1\n" : "",
  6997. drv->scan_complete_events ?
  6998. "scan_complete_events=1\n" : "",
  6999. drv->disabled_11b_rates ?
  7000. "disabled_11b_rates=1\n" : "",
  7001. drv->pending_remain_on_chan ?
  7002. "pending_remain_on_chan=1\n" : "",
  7003. drv->in_interface_list ? "in_interface_list=1\n" : "",
  7004. drv->device_ap_sme ? "device_ap_sme=1\n" : "",
  7005. drv->poll_command_supported ?
  7006. "poll_command_supported=1\n" : "",
  7007. drv->data_tx_status ? "data_tx_status=1\n" : "",
  7008. drv->scan_for_auth ? "scan_for_auth=1\n" : "",
  7009. drv->retry_auth ? "retry_auth=1\n" : "",
  7010. drv->use_monitor ? "use_monitor=1\n" : "",
  7011. drv->ignore_next_local_disconnect ?
  7012. "ignore_next_local_disconnect=1\n" : "",
  7013. drv->ignore_next_local_deauth ?
  7014. "ignore_next_local_deauth=1\n" : "");
  7015. if (os_snprintf_error(end - pos, res))
  7016. return pos - buf;
  7017. pos += res;
  7018. if (drv->has_capability) {
  7019. res = os_snprintf(pos, end - pos,
  7020. "capa.key_mgmt=0x%x\n"
  7021. "capa.enc=0x%x\n"
  7022. "capa.auth=0x%x\n"
  7023. "capa.flags=0x%llx\n"
  7024. "capa.rrm_flags=0x%x\n"
  7025. "capa.max_scan_ssids=%d\n"
  7026. "capa.max_sched_scan_ssids=%d\n"
  7027. "capa.sched_scan_supported=%d\n"
  7028. "capa.max_match_sets=%d\n"
  7029. "capa.max_remain_on_chan=%u\n"
  7030. "capa.max_stations=%u\n"
  7031. "capa.probe_resp_offloads=0x%x\n"
  7032. "capa.max_acl_mac_addrs=%u\n"
  7033. "capa.num_multichan_concurrent=%u\n"
  7034. "capa.mac_addr_rand_sched_scan_supported=%d\n"
  7035. "capa.mac_addr_rand_scan_supported=%d\n"
  7036. "capa.conc_capab=%u\n"
  7037. "capa.max_conc_chan_2_4=%u\n"
  7038. "capa.max_conc_chan_5_0=%u\n"
  7039. "capa.max_sched_scan_plans=%u\n"
  7040. "capa.max_sched_scan_plan_interval=%u\n"
  7041. "capa.max_sched_scan_plan_iterations=%u\n",
  7042. drv->capa.key_mgmt,
  7043. drv->capa.enc,
  7044. drv->capa.auth,
  7045. (unsigned long long) drv->capa.flags,
  7046. drv->capa.rrm_flags,
  7047. drv->capa.max_scan_ssids,
  7048. drv->capa.max_sched_scan_ssids,
  7049. drv->capa.sched_scan_supported,
  7050. drv->capa.max_match_sets,
  7051. drv->capa.max_remain_on_chan,
  7052. drv->capa.max_stations,
  7053. drv->capa.probe_resp_offloads,
  7054. drv->capa.max_acl_mac_addrs,
  7055. drv->capa.num_multichan_concurrent,
  7056. drv->capa.mac_addr_rand_sched_scan_supported,
  7057. drv->capa.mac_addr_rand_scan_supported,
  7058. drv->capa.conc_capab,
  7059. drv->capa.max_conc_chan_2_4,
  7060. drv->capa.max_conc_chan_5_0,
  7061. drv->capa.max_sched_scan_plans,
  7062. drv->capa.max_sched_scan_plan_interval,
  7063. drv->capa.max_sched_scan_plan_iterations);
  7064. if (os_snprintf_error(end - pos, res))
  7065. return pos - buf;
  7066. pos += res;
  7067. }
  7068. return pos - buf;
  7069. }
  7070. static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
  7071. {
  7072. if ((settings->head &&
  7073. nla_put(msg, NL80211_ATTR_BEACON_HEAD,
  7074. settings->head_len, settings->head)) ||
  7075. (settings->tail &&
  7076. nla_put(msg, NL80211_ATTR_BEACON_TAIL,
  7077. settings->tail_len, settings->tail)) ||
  7078. (settings->beacon_ies &&
  7079. nla_put(msg, NL80211_ATTR_IE,
  7080. settings->beacon_ies_len, settings->beacon_ies)) ||
  7081. (settings->proberesp_ies &&
  7082. nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
  7083. settings->proberesp_ies_len, settings->proberesp_ies)) ||
  7084. (settings->assocresp_ies &&
  7085. nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
  7086. settings->assocresp_ies_len, settings->assocresp_ies)) ||
  7087. (settings->probe_resp &&
  7088. nla_put(msg, NL80211_ATTR_PROBE_RESP,
  7089. settings->probe_resp_len, settings->probe_resp)))
  7090. return -ENOBUFS;
  7091. return 0;
  7092. }
  7093. static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
  7094. {
  7095. struct nl_msg *msg;
  7096. struct i802_bss *bss = priv;
  7097. struct wpa_driver_nl80211_data *drv = bss->drv;
  7098. struct nlattr *beacon_csa;
  7099. int ret = -ENOBUFS;
  7100. int csa_off_len = 0;
  7101. int i;
  7102. wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
  7103. settings->cs_count, settings->block_tx,
  7104. settings->freq_params.freq, settings->freq_params.bandwidth,
  7105. settings->freq_params.center_freq1,
  7106. settings->freq_params.center_freq2);
  7107. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
  7108. wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
  7109. return -EOPNOTSUPP;
  7110. }
  7111. if ((drv->nlmode != NL80211_IFTYPE_AP) &&
  7112. (drv->nlmode != NL80211_IFTYPE_P2P_GO))
  7113. return -EOPNOTSUPP;
  7114. /*
  7115. * Remove empty counters, assuming Probe Response and Beacon frame
  7116. * counters match. This implementation assumes that there are only two
  7117. * counters.
  7118. */
  7119. if (settings->counter_offset_beacon[0] &&
  7120. !settings->counter_offset_beacon[1]) {
  7121. csa_off_len = 1;
  7122. } else if (settings->counter_offset_beacon[1] &&
  7123. !settings->counter_offset_beacon[0]) {
  7124. csa_off_len = 1;
  7125. settings->counter_offset_beacon[0] =
  7126. settings->counter_offset_beacon[1];
  7127. settings->counter_offset_presp[0] =
  7128. settings->counter_offset_presp[1];
  7129. } else if (settings->counter_offset_beacon[1] &&
  7130. settings->counter_offset_beacon[0]) {
  7131. csa_off_len = 2;
  7132. } else {
  7133. wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
  7134. return -EINVAL;
  7135. }
  7136. /* Check CSA counters validity */
  7137. if (drv->capa.max_csa_counters &&
  7138. csa_off_len > drv->capa.max_csa_counters) {
  7139. wpa_printf(MSG_ERROR,
  7140. "nl80211: Too many CSA counters provided");
  7141. return -EINVAL;
  7142. }
  7143. if (!settings->beacon_csa.tail)
  7144. return -EINVAL;
  7145. for (i = 0; i < csa_off_len; i++) {
  7146. u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
  7147. u16 csa_c_off_presp = settings->counter_offset_presp[i];
  7148. if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
  7149. (settings->beacon_csa.tail[csa_c_off_bcn] !=
  7150. settings->cs_count))
  7151. return -EINVAL;
  7152. if (settings->beacon_csa.probe_resp &&
  7153. ((settings->beacon_csa.probe_resp_len <=
  7154. csa_c_off_presp) ||
  7155. (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
  7156. settings->cs_count)))
  7157. return -EINVAL;
  7158. }
  7159. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
  7160. nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
  7161. settings->cs_count) ||
  7162. (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
  7163. (settings->block_tx &&
  7164. nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
  7165. goto error;
  7166. /* beacon_after params */
  7167. ret = set_beacon_data(msg, &settings->beacon_after);
  7168. if (ret)
  7169. goto error;
  7170. /* beacon_csa params */
  7171. beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
  7172. if (!beacon_csa)
  7173. goto fail;
  7174. ret = set_beacon_data(msg, &settings->beacon_csa);
  7175. if (ret)
  7176. goto error;
  7177. if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
  7178. csa_off_len * sizeof(u16),
  7179. settings->counter_offset_beacon) ||
  7180. (settings->beacon_csa.probe_resp &&
  7181. nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
  7182. csa_off_len * sizeof(u16),
  7183. settings->counter_offset_presp)))
  7184. goto fail;
  7185. nla_nest_end(msg, beacon_csa);
  7186. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7187. if (ret) {
  7188. wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
  7189. ret, strerror(-ret));
  7190. }
  7191. return ret;
  7192. fail:
  7193. ret = -ENOBUFS;
  7194. error:
  7195. nlmsg_free(msg);
  7196. wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
  7197. return ret;
  7198. }
  7199. static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
  7200. u8 user_priority, u16 admitted_time)
  7201. {
  7202. struct i802_bss *bss = priv;
  7203. struct wpa_driver_nl80211_data *drv = bss->drv;
  7204. struct nl_msg *msg;
  7205. int ret;
  7206. wpa_printf(MSG_DEBUG,
  7207. "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
  7208. tsid, admitted_time, user_priority);
  7209. if (!is_sta_interface(drv->nlmode))
  7210. return -ENOTSUP;
  7211. msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
  7212. if (!msg ||
  7213. nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
  7214. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
  7215. nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
  7216. nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
  7217. nlmsg_free(msg);
  7218. return -ENOBUFS;
  7219. }
  7220. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7221. if (ret)
  7222. wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
  7223. ret, strerror(-ret));
  7224. return ret;
  7225. }
  7226. static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
  7227. {
  7228. struct i802_bss *bss = priv;
  7229. struct wpa_driver_nl80211_data *drv = bss->drv;
  7230. struct nl_msg *msg;
  7231. int ret;
  7232. wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
  7233. if (!is_sta_interface(drv->nlmode))
  7234. return -ENOTSUP;
  7235. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
  7236. nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
  7237. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
  7238. nlmsg_free(msg);
  7239. return -ENOBUFS;
  7240. }
  7241. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7242. if (ret)
  7243. wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
  7244. ret, strerror(-ret));
  7245. return ret;
  7246. }
  7247. #ifdef CONFIG_TESTING_OPTIONS
  7248. static int cmd_reply_handler(struct nl_msg *msg, void *arg)
  7249. {
  7250. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  7251. struct wpabuf *buf = arg;
  7252. if (!buf)
  7253. return NL_SKIP;
  7254. if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
  7255. wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
  7256. return NL_SKIP;
  7257. }
  7258. wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
  7259. genlmsg_attrlen(gnlh, 0));
  7260. return NL_SKIP;
  7261. }
  7262. #endif /* CONFIG_TESTING_OPTIONS */
  7263. static int vendor_reply_handler(struct nl_msg *msg, void *arg)
  7264. {
  7265. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  7266. struct nlattr *nl_vendor_reply, *nl;
  7267. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  7268. struct wpabuf *buf = arg;
  7269. int rem;
  7270. if (!buf)
  7271. return NL_SKIP;
  7272. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  7273. genlmsg_attrlen(gnlh, 0), NULL);
  7274. nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
  7275. if (!nl_vendor_reply)
  7276. return NL_SKIP;
  7277. if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
  7278. wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
  7279. return NL_SKIP;
  7280. }
  7281. nla_for_each_nested(nl, nl_vendor_reply, rem) {
  7282. wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
  7283. }
  7284. return NL_SKIP;
  7285. }
  7286. static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
  7287. unsigned int subcmd, const u8 *data,
  7288. size_t data_len, struct wpabuf *buf)
  7289. {
  7290. struct i802_bss *bss = priv;
  7291. struct wpa_driver_nl80211_data *drv = bss->drv;
  7292. struct nl_msg *msg;
  7293. int ret;
  7294. #ifdef CONFIG_TESTING_OPTIONS
  7295. if (vendor_id == 0xffffffff) {
  7296. msg = nlmsg_alloc();
  7297. if (!msg)
  7298. return -ENOMEM;
  7299. nl80211_cmd(drv, msg, 0, subcmd);
  7300. if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
  7301. 0)
  7302. goto fail;
  7303. ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
  7304. if (ret)
  7305. wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
  7306. ret);
  7307. return ret;
  7308. }
  7309. #endif /* CONFIG_TESTING_OPTIONS */
  7310. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
  7311. nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
  7312. nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
  7313. (data &&
  7314. nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
  7315. goto fail;
  7316. ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
  7317. if (ret)
  7318. wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
  7319. ret);
  7320. return ret;
  7321. fail:
  7322. nlmsg_free(msg);
  7323. return -ENOBUFS;
  7324. }
  7325. static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
  7326. u8 qos_map_set_len)
  7327. {
  7328. struct i802_bss *bss = priv;
  7329. struct wpa_driver_nl80211_data *drv = bss->drv;
  7330. struct nl_msg *msg;
  7331. int ret;
  7332. wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
  7333. qos_map_set, qos_map_set_len);
  7334. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
  7335. nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
  7336. nlmsg_free(msg);
  7337. return -ENOBUFS;
  7338. }
  7339. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7340. if (ret)
  7341. wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
  7342. return ret;
  7343. }
  7344. static int nl80211_set_wowlan(void *priv,
  7345. const struct wowlan_triggers *triggers)
  7346. {
  7347. struct i802_bss *bss = priv;
  7348. struct wpa_driver_nl80211_data *drv = bss->drv;
  7349. struct nl_msg *msg;
  7350. struct nlattr *wowlan_triggers;
  7351. int ret;
  7352. wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
  7353. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
  7354. !(wowlan_triggers = nla_nest_start(msg,
  7355. NL80211_ATTR_WOWLAN_TRIGGERS)) ||
  7356. (triggers->any &&
  7357. nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
  7358. (triggers->disconnect &&
  7359. nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
  7360. (triggers->magic_pkt &&
  7361. nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
  7362. (triggers->gtk_rekey_failure &&
  7363. nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
  7364. (triggers->eap_identity_req &&
  7365. nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
  7366. (triggers->four_way_handshake &&
  7367. nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
  7368. (triggers->rfkill_release &&
  7369. nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
  7370. nlmsg_free(msg);
  7371. return -ENOBUFS;
  7372. }
  7373. nla_nest_end(msg, wowlan_triggers);
  7374. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7375. if (ret)
  7376. wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
  7377. return ret;
  7378. }
  7379. #ifdef CONFIG_DRIVER_NL80211_QCA
  7380. static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
  7381. {
  7382. struct i802_bss *bss = priv;
  7383. struct wpa_driver_nl80211_data *drv = bss->drv;
  7384. struct nl_msg *msg;
  7385. struct nlattr *params;
  7386. wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
  7387. if (!drv->roaming_vendor_cmd_avail) {
  7388. wpa_printf(MSG_DEBUG,
  7389. "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
  7390. return -1;
  7391. }
  7392. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
  7393. nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
  7394. nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
  7395. QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
  7396. !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
  7397. nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
  7398. allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
  7399. QCA_ROAMING_NOT_ALLOWED) ||
  7400. (bssid &&
  7401. nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
  7402. nlmsg_free(msg);
  7403. return -1;
  7404. }
  7405. nla_nest_end(msg, params);
  7406. return send_and_recv_msgs(drv, msg, NULL, NULL);
  7407. }
  7408. #endif /* CONFIG_DRIVER_NL80211_QCA */
  7409. static int nl80211_set_mac_addr(void *priv, const u8 *addr)
  7410. {
  7411. struct i802_bss *bss = priv;
  7412. struct wpa_driver_nl80211_data *drv = bss->drv;
  7413. int new_addr = addr != NULL;
  7414. if (TEST_FAIL())
  7415. return -1;
  7416. if (!addr)
  7417. addr = drv->perm_addr;
  7418. if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
  7419. return -1;
  7420. if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
  7421. {
  7422. wpa_printf(MSG_DEBUG,
  7423. "nl80211: failed to set_mac_addr for %s to " MACSTR,
  7424. bss->ifname, MAC2STR(addr));
  7425. if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
  7426. 1) < 0) {
  7427. wpa_printf(MSG_DEBUG,
  7428. "nl80211: Could not restore interface UP after failed set_mac_addr");
  7429. }
  7430. return -1;
  7431. }
  7432. wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
  7433. bss->ifname, MAC2STR(addr));
  7434. drv->addr_changed = new_addr;
  7435. os_memcpy(bss->addr, addr, ETH_ALEN);
  7436. if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
  7437. {
  7438. wpa_printf(MSG_DEBUG,
  7439. "nl80211: Could not restore interface UP after set_mac_addr");
  7440. }
  7441. return 0;
  7442. }
  7443. #ifdef CONFIG_MESH
  7444. static int wpa_driver_nl80211_init_mesh(void *priv)
  7445. {
  7446. if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
  7447. wpa_printf(MSG_INFO,
  7448. "nl80211: Failed to set interface into mesh mode");
  7449. return -1;
  7450. }
  7451. return 0;
  7452. }
  7453. static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
  7454. size_t mesh_id_len)
  7455. {
  7456. if (mesh_id) {
  7457. wpa_hexdump_ascii(MSG_DEBUG, " * Mesh ID (SSID)",
  7458. mesh_id, mesh_id_len);
  7459. return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
  7460. }
  7461. return 0;
  7462. }
  7463. static int nl80211_put_mesh_config(struct nl_msg *msg,
  7464. struct wpa_driver_mesh_bss_params *params)
  7465. {
  7466. struct nlattr *container;
  7467. container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
  7468. if (!container)
  7469. return -1;
  7470. if (((params->flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
  7471. nla_put_u32(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
  7472. params->auto_plinks)) ||
  7473. ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS) &&
  7474. nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
  7475. params->max_peer_links)) ||
  7476. ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_RSSI_THRESHOLD) &&
  7477. nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
  7478. params->rssi_threshold)))
  7479. return -1;
  7480. /*
  7481. * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
  7482. * the timer could disconnect stations even in that case.
  7483. */
  7484. if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT) &&
  7485. nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
  7486. params->peer_link_timeout)) {
  7487. wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
  7488. return -1;
  7489. }
  7490. if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE) &&
  7491. nla_put_u16(msg, NL80211_MESHCONF_HT_OPMODE, params->ht_opmode)) {
  7492. wpa_printf(MSG_ERROR, "nl80211: Failed to set HT_OP_MODE");
  7493. return -1;
  7494. }
  7495. nla_nest_end(msg, container);
  7496. return 0;
  7497. }
  7498. static int nl80211_join_mesh(struct i802_bss *bss,
  7499. struct wpa_driver_mesh_join_params *params)
  7500. {
  7501. struct wpa_driver_nl80211_data *drv = bss->drv;
  7502. struct nl_msg *msg;
  7503. struct nlattr *container;
  7504. int ret = -1;
  7505. wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
  7506. msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
  7507. if (!msg ||
  7508. nl80211_put_freq_params(msg, &params->freq) ||
  7509. nl80211_put_basic_rates(msg, params->basic_rates) ||
  7510. nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
  7511. nl80211_put_beacon_int(msg, params->beacon_int) ||
  7512. nl80211_put_dtim_period(msg, params->dtim_period))
  7513. goto fail;
  7514. wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
  7515. container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
  7516. if (!container)
  7517. goto fail;
  7518. if (params->ies) {
  7519. wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
  7520. if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
  7521. params->ies))
  7522. goto fail;
  7523. }
  7524. /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
  7525. if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
  7526. if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
  7527. nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
  7528. goto fail;
  7529. }
  7530. if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
  7531. nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
  7532. goto fail;
  7533. if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
  7534. nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
  7535. goto fail;
  7536. nla_nest_end(msg, container);
  7537. params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
  7538. params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT;
  7539. params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS;
  7540. if (nl80211_put_mesh_config(msg, &params->conf) < 0)
  7541. goto fail;
  7542. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7543. msg = NULL;
  7544. if (ret) {
  7545. wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
  7546. ret, strerror(-ret));
  7547. goto fail;
  7548. }
  7549. ret = 0;
  7550. drv->assoc_freq = bss->freq = params->freq.freq;
  7551. wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
  7552. fail:
  7553. nlmsg_free(msg);
  7554. return ret;
  7555. }
  7556. static int
  7557. wpa_driver_nl80211_join_mesh(void *priv,
  7558. struct wpa_driver_mesh_join_params *params)
  7559. {
  7560. struct i802_bss *bss = priv;
  7561. int ret, timeout;
  7562. timeout = params->conf.peer_link_timeout;
  7563. /* Disable kernel inactivity timer */
  7564. if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
  7565. params->conf.peer_link_timeout = 0;
  7566. ret = nl80211_join_mesh(bss, params);
  7567. if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
  7568. wpa_printf(MSG_DEBUG,
  7569. "nl80211: Mesh join retry for peer_link_timeout");
  7570. /*
  7571. * Old kernel does not support setting
  7572. * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
  7573. * into future from peer_link_timeout.
  7574. */
  7575. params->conf.peer_link_timeout = timeout + 60;
  7576. ret = nl80211_join_mesh(priv, params);
  7577. }
  7578. params->conf.peer_link_timeout = timeout;
  7579. return ret;
  7580. }
  7581. static int wpa_driver_nl80211_leave_mesh(void *priv)
  7582. {
  7583. struct i802_bss *bss = priv;
  7584. struct wpa_driver_nl80211_data *drv = bss->drv;
  7585. struct nl_msg *msg;
  7586. int ret;
  7587. wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
  7588. msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
  7589. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7590. if (ret) {
  7591. wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
  7592. ret, strerror(-ret));
  7593. } else {
  7594. wpa_printf(MSG_DEBUG,
  7595. "nl80211: mesh leave request send successfully");
  7596. }
  7597. if (wpa_driver_nl80211_set_mode(drv->first_bss,
  7598. NL80211_IFTYPE_STATION)) {
  7599. wpa_printf(MSG_INFO,
  7600. "nl80211: Failed to set interface into station mode");
  7601. }
  7602. return ret;
  7603. }
  7604. #endif /* CONFIG_MESH */
  7605. static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
  7606. const u8 *ipaddr, int prefixlen,
  7607. const u8 *addr)
  7608. {
  7609. #ifdef CONFIG_LIBNL3_ROUTE
  7610. struct i802_bss *bss = priv;
  7611. struct wpa_driver_nl80211_data *drv = bss->drv;
  7612. struct rtnl_neigh *rn;
  7613. struct nl_addr *nl_ipaddr = NULL;
  7614. struct nl_addr *nl_lladdr = NULL;
  7615. int family, addrsize;
  7616. int res;
  7617. if (!ipaddr || prefixlen == 0 || !addr)
  7618. return -EINVAL;
  7619. if (bss->br_ifindex == 0) {
  7620. wpa_printf(MSG_DEBUG,
  7621. "nl80211: bridge must be set before adding an ip neigh to it");
  7622. return -1;
  7623. }
  7624. if (!drv->rtnl_sk) {
  7625. wpa_printf(MSG_DEBUG,
  7626. "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
  7627. return -1;
  7628. }
  7629. if (version == 4) {
  7630. family = AF_INET;
  7631. addrsize = 4;
  7632. } else if (version == 6) {
  7633. family = AF_INET6;
  7634. addrsize = 16;
  7635. } else {
  7636. return -EINVAL;
  7637. }
  7638. rn = rtnl_neigh_alloc();
  7639. if (rn == NULL)
  7640. return -ENOMEM;
  7641. /* set the destination ip address for neigh */
  7642. nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
  7643. if (nl_ipaddr == NULL) {
  7644. wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
  7645. res = -ENOMEM;
  7646. goto errout;
  7647. }
  7648. nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
  7649. res = rtnl_neigh_set_dst(rn, nl_ipaddr);
  7650. if (res) {
  7651. wpa_printf(MSG_DEBUG,
  7652. "nl80211: neigh set destination addr failed");
  7653. goto errout;
  7654. }
  7655. /* set the corresponding lladdr for neigh */
  7656. nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
  7657. if (nl_lladdr == NULL) {
  7658. wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
  7659. res = -ENOMEM;
  7660. goto errout;
  7661. }
  7662. rtnl_neigh_set_lladdr(rn, nl_lladdr);
  7663. rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
  7664. rtnl_neigh_set_state(rn, NUD_PERMANENT);
  7665. res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
  7666. if (res) {
  7667. wpa_printf(MSG_DEBUG,
  7668. "nl80211: Adding bridge ip neigh failed: %s",
  7669. strerror(errno));
  7670. }
  7671. errout:
  7672. if (nl_lladdr)
  7673. nl_addr_put(nl_lladdr);
  7674. if (nl_ipaddr)
  7675. nl_addr_put(nl_ipaddr);
  7676. if (rn)
  7677. rtnl_neigh_put(rn);
  7678. return res;
  7679. #else /* CONFIG_LIBNL3_ROUTE */
  7680. return -1;
  7681. #endif /* CONFIG_LIBNL3_ROUTE */
  7682. }
  7683. static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
  7684. const u8 *ipaddr)
  7685. {
  7686. #ifdef CONFIG_LIBNL3_ROUTE
  7687. struct i802_bss *bss = priv;
  7688. struct wpa_driver_nl80211_data *drv = bss->drv;
  7689. struct rtnl_neigh *rn;
  7690. struct nl_addr *nl_ipaddr;
  7691. int family, addrsize;
  7692. int res;
  7693. if (!ipaddr)
  7694. return -EINVAL;
  7695. if (version == 4) {
  7696. family = AF_INET;
  7697. addrsize = 4;
  7698. } else if (version == 6) {
  7699. family = AF_INET6;
  7700. addrsize = 16;
  7701. } else {
  7702. return -EINVAL;
  7703. }
  7704. if (bss->br_ifindex == 0) {
  7705. wpa_printf(MSG_DEBUG,
  7706. "nl80211: bridge must be set to delete an ip neigh");
  7707. return -1;
  7708. }
  7709. if (!drv->rtnl_sk) {
  7710. wpa_printf(MSG_DEBUG,
  7711. "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
  7712. return -1;
  7713. }
  7714. rn = rtnl_neigh_alloc();
  7715. if (rn == NULL)
  7716. return -ENOMEM;
  7717. /* set the destination ip address for neigh */
  7718. nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
  7719. if (nl_ipaddr == NULL) {
  7720. wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
  7721. res = -ENOMEM;
  7722. goto errout;
  7723. }
  7724. res = rtnl_neigh_set_dst(rn, nl_ipaddr);
  7725. if (res) {
  7726. wpa_printf(MSG_DEBUG,
  7727. "nl80211: neigh set destination addr failed");
  7728. goto errout;
  7729. }
  7730. rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
  7731. res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
  7732. if (res) {
  7733. wpa_printf(MSG_DEBUG,
  7734. "nl80211: Deleting bridge ip neigh failed: %s",
  7735. strerror(errno));
  7736. }
  7737. errout:
  7738. if (nl_ipaddr)
  7739. nl_addr_put(nl_ipaddr);
  7740. if (rn)
  7741. rtnl_neigh_put(rn);
  7742. return res;
  7743. #else /* CONFIG_LIBNL3_ROUTE */
  7744. return -1;
  7745. #endif /* CONFIG_LIBNL3_ROUTE */
  7746. }
  7747. static int linux_write_system_file(const char *path, unsigned int val)
  7748. {
  7749. char buf[50];
  7750. int fd, len;
  7751. len = os_snprintf(buf, sizeof(buf), "%u\n", val);
  7752. if (os_snprintf_error(sizeof(buf), len))
  7753. return -1;
  7754. fd = open(path, O_WRONLY);
  7755. if (fd < 0)
  7756. return -1;
  7757. if (write(fd, buf, len) < 0) {
  7758. wpa_printf(MSG_DEBUG,
  7759. "nl80211: Failed to write Linux system file: %s with the value of %d",
  7760. path, val);
  7761. close(fd);
  7762. return -1;
  7763. }
  7764. close(fd);
  7765. return 0;
  7766. }
  7767. static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
  7768. {
  7769. switch (attr) {
  7770. case DRV_BR_PORT_ATTR_PROXYARP:
  7771. return "proxyarp_wifi";
  7772. case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
  7773. return "hairpin_mode";
  7774. }
  7775. return NULL;
  7776. }
  7777. static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
  7778. unsigned int val)
  7779. {
  7780. struct i802_bss *bss = priv;
  7781. char path[128];
  7782. const char *attr_txt;
  7783. attr_txt = drv_br_port_attr_str(attr);
  7784. if (attr_txt == NULL)
  7785. return -EINVAL;
  7786. os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
  7787. bss->ifname, attr_txt);
  7788. if (linux_write_system_file(path, val))
  7789. return -1;
  7790. return 0;
  7791. }
  7792. static const char * drv_br_net_param_str(enum drv_br_net_param param)
  7793. {
  7794. switch (param) {
  7795. case DRV_BR_NET_PARAM_GARP_ACCEPT:
  7796. return "arp_accept";
  7797. default:
  7798. return NULL;
  7799. }
  7800. }
  7801. static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
  7802. unsigned int val)
  7803. {
  7804. struct i802_bss *bss = priv;
  7805. char path[128];
  7806. const char *param_txt;
  7807. int ip_version = 4;
  7808. if (param == DRV_BR_MULTICAST_SNOOPING) {
  7809. os_snprintf(path, sizeof(path),
  7810. "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
  7811. bss->brname);
  7812. goto set_val;
  7813. }
  7814. param_txt = drv_br_net_param_str(param);
  7815. if (param_txt == NULL)
  7816. return -EINVAL;
  7817. switch (param) {
  7818. case DRV_BR_NET_PARAM_GARP_ACCEPT:
  7819. ip_version = 4;
  7820. break;
  7821. default:
  7822. return -EINVAL;
  7823. }
  7824. os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
  7825. ip_version, bss->brname, param_txt);
  7826. set_val:
  7827. if (linux_write_system_file(path, val))
  7828. return -1;
  7829. return 0;
  7830. }
  7831. #ifdef CONFIG_DRIVER_NL80211_QCA
  7832. static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
  7833. {
  7834. switch (hw_mode) {
  7835. case HOSTAPD_MODE_IEEE80211B:
  7836. return QCA_ACS_MODE_IEEE80211B;
  7837. case HOSTAPD_MODE_IEEE80211G:
  7838. return QCA_ACS_MODE_IEEE80211G;
  7839. case HOSTAPD_MODE_IEEE80211A:
  7840. return QCA_ACS_MODE_IEEE80211A;
  7841. case HOSTAPD_MODE_IEEE80211AD:
  7842. return QCA_ACS_MODE_IEEE80211AD;
  7843. case HOSTAPD_MODE_IEEE80211ANY:
  7844. return QCA_ACS_MODE_IEEE80211ANY;
  7845. default:
  7846. return -1;
  7847. }
  7848. }
  7849. static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
  7850. {
  7851. int i, len, ret;
  7852. u32 *freqs;
  7853. if (!freq_list)
  7854. return 0;
  7855. len = int_array_len(freq_list);
  7856. freqs = os_malloc(sizeof(u32) * len);
  7857. if (!freqs)
  7858. return -1;
  7859. for (i = 0; i < len; i++)
  7860. freqs[i] = freq_list[i];
  7861. ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
  7862. sizeof(u32) * len, freqs);
  7863. os_free(freqs);
  7864. return ret;
  7865. }
  7866. static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
  7867. {
  7868. struct i802_bss *bss = priv;
  7869. struct wpa_driver_nl80211_data *drv = bss->drv;
  7870. struct nl_msg *msg;
  7871. struct nlattr *data;
  7872. int ret;
  7873. int mode;
  7874. mode = hw_mode_to_qca_acs(params->hw_mode);
  7875. if (mode < 0)
  7876. return -1;
  7877. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
  7878. nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
  7879. nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
  7880. QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
  7881. !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
  7882. nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
  7883. (params->ht_enabled &&
  7884. nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
  7885. (params->ht40_enabled &&
  7886. nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
  7887. (params->vht_enabled &&
  7888. nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
  7889. nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
  7890. params->ch_width) ||
  7891. (params->ch_list_len &&
  7892. nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST, params->ch_list_len,
  7893. params->ch_list)) ||
  7894. add_acs_freq_list(msg, params->freq_list)) {
  7895. nlmsg_free(msg);
  7896. return -ENOBUFS;
  7897. }
  7898. nla_nest_end(msg, data);
  7899. wpa_printf(MSG_DEBUG,
  7900. "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d CH_LIST_LEN: %u",
  7901. params->hw_mode, params->ht_enabled, params->ht40_enabled,
  7902. params->vht_enabled, params->ch_width, params->ch_list_len);
  7903. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7904. if (ret) {
  7905. wpa_printf(MSG_DEBUG,
  7906. "nl80211: Failed to invoke driver ACS function: %s",
  7907. strerror(errno));
  7908. }
  7909. return ret;
  7910. }
  7911. static int nl80211_set_band(void *priv, enum set_band band)
  7912. {
  7913. struct i802_bss *bss = priv;
  7914. struct wpa_driver_nl80211_data *drv = bss->drv;
  7915. struct nl_msg *msg;
  7916. struct nlattr *data;
  7917. int ret;
  7918. enum qca_set_band qca_band;
  7919. if (!drv->setband_vendor_cmd_avail)
  7920. return -1;
  7921. switch (band) {
  7922. case WPA_SETBAND_AUTO:
  7923. qca_band = QCA_SETBAND_AUTO;
  7924. break;
  7925. case WPA_SETBAND_5G:
  7926. qca_band = QCA_SETBAND_5G;
  7927. break;
  7928. case WPA_SETBAND_2G:
  7929. qca_band = QCA_SETBAND_2G;
  7930. break;
  7931. default:
  7932. return -1;
  7933. }
  7934. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
  7935. nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
  7936. nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
  7937. QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
  7938. !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
  7939. nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE, qca_band)) {
  7940. nlmsg_free(msg);
  7941. return -ENOBUFS;
  7942. }
  7943. nla_nest_end(msg, data);
  7944. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7945. if (ret) {
  7946. wpa_printf(MSG_DEBUG,
  7947. "nl80211: Driver setband function failed: %s",
  7948. strerror(errno));
  7949. }
  7950. return ret;
  7951. }
  7952. struct nl80211_pcl {
  7953. unsigned int num;
  7954. unsigned int *freq_list;
  7955. };
  7956. static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
  7957. {
  7958. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  7959. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  7960. struct nl80211_pcl *param = arg;
  7961. struct nlattr *nl_vend, *attr;
  7962. enum qca_iface_type iface_type;
  7963. struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
  7964. unsigned int num, max_num;
  7965. u32 *freqs;
  7966. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  7967. genlmsg_attrlen(gnlh, 0), NULL);
  7968. nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
  7969. if (!nl_vend)
  7970. return NL_SKIP;
  7971. nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
  7972. nla_data(nl_vend), nla_len(nl_vend), NULL);
  7973. attr = tb_vendor[
  7974. QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
  7975. if (!attr) {
  7976. wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
  7977. param->num = 0;
  7978. return NL_SKIP;
  7979. }
  7980. iface_type = (enum qca_iface_type) nla_get_u32(attr);
  7981. wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
  7982. iface_type);
  7983. attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
  7984. if (!attr) {
  7985. wpa_printf(MSG_ERROR,
  7986. "nl80211: preferred_freq_list couldn't be found");
  7987. param->num = 0;
  7988. return NL_SKIP;
  7989. }
  7990. /*
  7991. * param->num has the maximum number of entries for which there
  7992. * is room in the freq_list provided by the caller.
  7993. */
  7994. freqs = nla_data(attr);
  7995. max_num = nla_len(attr) / sizeof(u32);
  7996. if (max_num > param->num)
  7997. max_num = param->num;
  7998. for (num = 0; num < max_num; num++)
  7999. param->freq_list[num] = freqs[num];
  8000. param->num = num;
  8001. return NL_SKIP;
  8002. }
  8003. static int nl80211_get_pref_freq_list(void *priv,
  8004. enum wpa_driver_if_type if_type,
  8005. unsigned int *num,
  8006. unsigned int *freq_list)
  8007. {
  8008. struct i802_bss *bss = priv;
  8009. struct wpa_driver_nl80211_data *drv = bss->drv;
  8010. struct nl_msg *msg;
  8011. int ret;
  8012. unsigned int i;
  8013. struct nlattr *params;
  8014. struct nl80211_pcl param;
  8015. enum qca_iface_type iface_type;
  8016. if (!drv->get_pref_freq_list)
  8017. return -1;
  8018. switch (if_type) {
  8019. case WPA_IF_STATION:
  8020. iface_type = QCA_IFACE_TYPE_STA;
  8021. break;
  8022. case WPA_IF_AP_BSS:
  8023. iface_type = QCA_IFACE_TYPE_AP;
  8024. break;
  8025. case WPA_IF_P2P_GO:
  8026. iface_type = QCA_IFACE_TYPE_P2P_GO;
  8027. break;
  8028. case WPA_IF_P2P_CLIENT:
  8029. iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
  8030. break;
  8031. case WPA_IF_IBSS:
  8032. iface_type = QCA_IFACE_TYPE_IBSS;
  8033. break;
  8034. case WPA_IF_TDLS:
  8035. iface_type = QCA_IFACE_TYPE_TDLS;
  8036. break;
  8037. default:
  8038. return -1;
  8039. }
  8040. param.num = *num;
  8041. param.freq_list = freq_list;
  8042. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
  8043. nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
  8044. nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
  8045. nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
  8046. QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
  8047. !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
  8048. nla_put_u32(msg,
  8049. QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
  8050. iface_type)) {
  8051. wpa_printf(MSG_ERROR,
  8052. "%s: err in adding vendor_cmd and vendor_data",
  8053. __func__);
  8054. nlmsg_free(msg);
  8055. return -1;
  8056. }
  8057. nla_nest_end(msg, params);
  8058. os_memset(freq_list, 0, *num * sizeof(freq_list[0]));
  8059. ret = send_and_recv_msgs(drv, msg, preferred_freq_info_handler, &param);
  8060. if (ret) {
  8061. wpa_printf(MSG_ERROR,
  8062. "%s: err in send_and_recv_msgs", __func__);
  8063. return ret;
  8064. }
  8065. *num = param.num;
  8066. for (i = 0; i < *num; i++) {
  8067. wpa_printf(MSG_DEBUG, "nl80211: preferred_channel_list[%d]=%d",
  8068. i, freq_list[i]);
  8069. }
  8070. return 0;
  8071. }
  8072. static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
  8073. {
  8074. struct i802_bss *bss = priv;
  8075. struct wpa_driver_nl80211_data *drv = bss->drv;
  8076. struct nl_msg *msg;
  8077. int ret;
  8078. struct nlattr *params;
  8079. if (!drv->set_prob_oper_freq)
  8080. return -1;
  8081. wpa_printf(MSG_DEBUG,
  8082. "nl80211: Set P2P probable operating freq %u for ifindex %d",
  8083. freq, bss->ifindex);
  8084. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
  8085. nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
  8086. nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
  8087. QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
  8088. !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
  8089. nla_put_u32(msg,
  8090. QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
  8091. QCA_IFACE_TYPE_P2P_CLIENT) ||
  8092. nla_put_u32(msg,
  8093. QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
  8094. freq)) {
  8095. wpa_printf(MSG_ERROR,
  8096. "%s: err in adding vendor_cmd and vendor_data",
  8097. __func__);
  8098. nlmsg_free(msg);
  8099. return -1;
  8100. }
  8101. nla_nest_end(msg, params);
  8102. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  8103. msg = NULL;
  8104. if (ret) {
  8105. wpa_printf(MSG_ERROR, "%s: err in send_and_recv_msgs",
  8106. __func__);
  8107. return ret;
  8108. }
  8109. nlmsg_free(msg);
  8110. return 0;
  8111. }
  8112. static int nl80211_p2p_lo_start(void *priv, unsigned int freq,
  8113. unsigned int period, unsigned int interval,
  8114. unsigned int count, const u8 *device_types,
  8115. size_t dev_types_len,
  8116. const u8 *ies, size_t ies_len)
  8117. {
  8118. struct i802_bss *bss = priv;
  8119. struct wpa_driver_nl80211_data *drv = bss->drv;
  8120. struct nl_msg *msg;
  8121. struct nlattr *container;
  8122. int ret;
  8123. wpa_printf(MSG_DEBUG,
  8124. "nl80211: Start P2P Listen offload: freq=%u, period=%u, interval=%u, count=%u",
  8125. freq, period, interval, count);
  8126. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
  8127. return -1;
  8128. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
  8129. nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
  8130. nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
  8131. QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_START))
  8132. goto fail;
  8133. container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
  8134. if (!container)
  8135. goto fail;
  8136. if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_CHANNEL,
  8137. freq) ||
  8138. nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_PERIOD,
  8139. period) ||
  8140. nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_INTERVAL,
  8141. interval) ||
  8142. nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_COUNT,
  8143. count) ||
  8144. nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_DEVICE_TYPES,
  8145. dev_types_len, device_types) ||
  8146. nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_VENDOR_IE,
  8147. ies_len, ies))
  8148. goto fail;
  8149. nla_nest_end(msg, container);
  8150. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  8151. msg = NULL;
  8152. if (ret) {
  8153. wpa_printf(MSG_DEBUG,
  8154. "nl80211: Failed to send P2P Listen offload vendor command");
  8155. goto fail;
  8156. }
  8157. return 0;
  8158. fail:
  8159. nlmsg_free(msg);
  8160. return -1;
  8161. }
  8162. static int nl80211_p2p_lo_stop(void *priv)
  8163. {
  8164. struct i802_bss *bss = priv;
  8165. struct wpa_driver_nl80211_data *drv = bss->drv;
  8166. struct nl_msg *msg;
  8167. wpa_printf(MSG_DEBUG, "nl80211: Stop P2P Listen offload");
  8168. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
  8169. return -1;
  8170. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
  8171. nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
  8172. nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
  8173. QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_STOP)) {
  8174. nlmsg_free(msg);
  8175. return -1;
  8176. }
  8177. return send_and_recv_msgs(drv, msg, NULL, NULL);
  8178. }
  8179. static int nl80211_set_tdls_mode(void *priv, int tdls_external_control)
  8180. {
  8181. struct i802_bss *bss = priv;
  8182. struct wpa_driver_nl80211_data *drv = bss->drv;
  8183. struct nl_msg *msg;
  8184. struct nlattr *params;
  8185. int ret;
  8186. u32 tdls_mode;
  8187. wpa_printf(MSG_DEBUG,
  8188. "nl80211: Set TDKS mode: tdls_external_control=%d",
  8189. tdls_external_control);
  8190. if (tdls_external_control == 1)
  8191. tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_IMPLICIT |
  8192. QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXTERNAL;
  8193. else
  8194. tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXPLICIT;
  8195. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
  8196. nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
  8197. nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
  8198. QCA_NL80211_VENDOR_SUBCMD_CONFIGURE_TDLS))
  8199. goto fail;
  8200. params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
  8201. if (!params)
  8202. goto fail;
  8203. if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TDLS_CONFIG_TRIGGER_MODE,
  8204. tdls_mode))
  8205. goto fail;
  8206. nla_nest_end(msg, params);
  8207. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  8208. msg = NULL;
  8209. if (ret) {
  8210. wpa_printf(MSG_ERROR,
  8211. "nl80211: Set TDLS mode failed: ret=%d (%s)",
  8212. ret, strerror(-ret));
  8213. goto fail;
  8214. }
  8215. return 0;
  8216. fail:
  8217. nlmsg_free(msg);
  8218. return -1;
  8219. }
  8220. #ifdef CONFIG_MBO
  8221. static enum mbo_transition_reject_reason
  8222. nl80211_mbo_reject_reason_mapping(enum qca_wlan_btm_candidate_status status)
  8223. {
  8224. switch (status) {
  8225. case QCA_STATUS_REJECT_EXCESSIVE_FRAME_LOSS_EXPECTED:
  8226. return MBO_TRANSITION_REJECT_REASON_FRAME_LOSS;
  8227. case QCA_STATUS_REJECT_EXCESSIVE_DELAY_EXPECTED:
  8228. return MBO_TRANSITION_REJECT_REASON_DELAY;
  8229. case QCA_STATUS_REJECT_INSUFFICIENT_QOS_CAPACITY:
  8230. return MBO_TRANSITION_REJECT_REASON_QOS_CAPACITY;
  8231. case QCA_STATUS_REJECT_LOW_RSSI:
  8232. return MBO_TRANSITION_REJECT_REASON_RSSI;
  8233. case QCA_STATUS_REJECT_HIGH_INTERFERENCE:
  8234. return MBO_TRANSITION_REJECT_REASON_INTERFERENCE;
  8235. case QCA_STATUS_REJECT_UNKNOWN:
  8236. default:
  8237. return MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
  8238. }
  8239. }
  8240. static void nl80211_parse_btm_candidate_info(struct candidate_list *candidate,
  8241. struct nlattr *tb[], int num)
  8242. {
  8243. enum qca_wlan_btm_candidate_status status;
  8244. char buf[50];
  8245. os_memcpy(candidate->bssid,
  8246. nla_data(tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID]),
  8247. ETH_ALEN);
  8248. status = nla_get_u32(
  8249. tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS]);
  8250. candidate->is_accept = status == QCA_STATUS_ACCEPT;
  8251. candidate->reject_reason = nl80211_mbo_reject_reason_mapping(status);
  8252. if (candidate->is_accept)
  8253. os_snprintf(buf, sizeof(buf), "Accepted");
  8254. else
  8255. os_snprintf(buf, sizeof(buf),
  8256. "Rejected, Reject_reason: %d",
  8257. candidate->reject_reason);
  8258. wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR " %s",
  8259. num, MAC2STR(candidate->bssid), buf);
  8260. }
  8261. static int
  8262. nl80211_get_bss_transition_status_handler(struct nl_msg *msg, void *arg)
  8263. {
  8264. struct wpa_bss_candidate_info *info = arg;
  8265. struct candidate_list *candidate = info->candidates;
  8266. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  8267. struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
  8268. struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1];
  8269. static struct nla_policy policy[
  8270. QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1] = {
  8271. [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] = {
  8272. .minlen = ETH_ALEN
  8273. },
  8274. [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS] = {
  8275. .type = NLA_U32,
  8276. },
  8277. };
  8278. struct nlattr *attr;
  8279. int rem;
  8280. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  8281. u8 num;
  8282. num = info->num; /* number of candidates sent to driver */
  8283. info->num = 0;
  8284. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  8285. genlmsg_attrlen(gnlh, 0), NULL);
  8286. if (!tb_msg[NL80211_ATTR_VENDOR_DATA] ||
  8287. nla_parse_nested(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
  8288. tb_msg[NL80211_ATTR_VENDOR_DATA], NULL) ||
  8289. !tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO])
  8290. return NL_SKIP;
  8291. wpa_printf(MSG_DEBUG,
  8292. "nl80211: WNM Candidate list received from driver");
  8293. nla_for_each_nested(attr,
  8294. tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO],
  8295. rem) {
  8296. if (info->num >= num ||
  8297. nla_parse_nested(
  8298. tb, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX,
  8299. attr, policy) ||
  8300. !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] ||
  8301. !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS])
  8302. break;
  8303. nl80211_parse_btm_candidate_info(candidate, tb, info->num);
  8304. candidate++;
  8305. info->num++;
  8306. }
  8307. return NL_SKIP;
  8308. }
  8309. static struct wpa_bss_candidate_info *
  8310. nl80211_get_bss_transition_status(void *priv, struct wpa_bss_trans_info *params)
  8311. {
  8312. struct i802_bss *bss = priv;
  8313. struct wpa_driver_nl80211_data *drv = bss->drv;
  8314. struct nl_msg *msg;
  8315. struct nlattr *attr, *attr1, *attr2;
  8316. struct wpa_bss_candidate_info *info;
  8317. u8 i;
  8318. int ret;
  8319. u8 *pos;
  8320. if (!drv->fetch_bss_trans_status)
  8321. return NULL;
  8322. info = os_zalloc(sizeof(*info));
  8323. if (!info)
  8324. return NULL;
  8325. /* Allocate memory for number of candidates sent to driver */
  8326. info->candidates = os_calloc(params->n_candidates,
  8327. sizeof(*info->candidates));
  8328. if (!info->candidates) {
  8329. os_free(info);
  8330. return NULL;
  8331. }
  8332. /* Copy the number of candidates being sent to driver. This is used in
  8333. * nl80211_get_bss_transition_status_handler() to limit the number of
  8334. * candidates that can be populated in info->candidates and will be
  8335. * later overwritten with the actual number of candidates received from
  8336. * the driver.
  8337. */
  8338. info->num = params->n_candidates;
  8339. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
  8340. nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
  8341. nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
  8342. QCA_NL80211_VENDOR_SUBCMD_FETCH_BSS_TRANSITION_STATUS))
  8343. goto fail;
  8344. attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
  8345. if (!attr)
  8346. goto fail;
  8347. if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_BTM_MBO_TRANSITION_REASON,
  8348. params->mbo_transition_reason))
  8349. goto fail;
  8350. attr1 = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO);
  8351. if (!attr1)
  8352. goto fail;
  8353. wpa_printf(MSG_DEBUG,
  8354. "nl80211: WNM Candidate list info sending to driver: mbo_transition_reason: %d n_candidates: %d",
  8355. params->mbo_transition_reason, params->n_candidates);
  8356. pos = params->bssid;
  8357. for (i = 0; i < params->n_candidates; i++) {
  8358. wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR, i,
  8359. MAC2STR(pos));
  8360. attr2 = nla_nest_start(msg, i);
  8361. if (!attr2 ||
  8362. nla_put(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID,
  8363. ETH_ALEN, pos))
  8364. goto fail;
  8365. pos += ETH_ALEN;
  8366. nla_nest_end(msg, attr2);
  8367. }
  8368. nla_nest_end(msg, attr1);
  8369. nla_nest_end(msg, attr);
  8370. ret = send_and_recv_msgs(drv, msg,
  8371. nl80211_get_bss_transition_status_handler,
  8372. info);
  8373. msg = NULL;
  8374. if (ret) {
  8375. wpa_printf(MSG_ERROR,
  8376. "nl80211: WNM Get BSS transition status failed: ret=%d (%s)",
  8377. ret, strerror(-ret));
  8378. goto fail;
  8379. }
  8380. return info;
  8381. fail:
  8382. nlmsg_free(msg);
  8383. os_free(info->candidates);
  8384. os_free(info);
  8385. return NULL;
  8386. }
  8387. /**
  8388. * nl80211_ignore_assoc_disallow - Configure driver to ignore assoc_disallow
  8389. * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
  8390. * @ignore_assoc_disallow: 0 to not ignore, 1 to ignore
  8391. * Returns: 0 on success, -1 on failure
  8392. */
  8393. static int nl80211_ignore_assoc_disallow(void *priv, int ignore_disallow)
  8394. {
  8395. struct i802_bss *bss = priv;
  8396. struct wpa_driver_nl80211_data *drv = bss->drv;
  8397. struct nl_msg *msg;
  8398. struct nlattr *attr;
  8399. int ret = -1;
  8400. if (!drv->set_wifi_conf_vendor_cmd_avail)
  8401. return -1;
  8402. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
  8403. nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
  8404. nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
  8405. QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION))
  8406. goto fail;
  8407. attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
  8408. if (!attr)
  8409. goto fail;
  8410. wpa_printf(MSG_DEBUG, "nl80211: Set ignore_assoc_disallow %d",
  8411. ignore_disallow);
  8412. if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_IGNORE_ASSOC_DISALLOWED,
  8413. ignore_disallow))
  8414. goto fail;
  8415. nla_nest_end(msg, attr);
  8416. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  8417. msg = NULL;
  8418. if (ret) {
  8419. wpa_printf(MSG_ERROR,
  8420. "nl80211: Set ignore_assoc_disallow failed: ret=%d (%s)",
  8421. ret, strerror(-ret));
  8422. goto fail;
  8423. }
  8424. fail:
  8425. nlmsg_free(msg);
  8426. return ret;
  8427. }
  8428. #endif /* CONFIG_MBO */
  8429. #endif /* CONFIG_DRIVER_NL80211_QCA */
  8430. static int nl80211_write_to_file(const char *name, unsigned int val)
  8431. {
  8432. int fd, len;
  8433. char tmp[128];
  8434. fd = open(name, O_RDWR);
  8435. if (fd < 0) {
  8436. wpa_printf(MSG_ERROR, "nl80211: Failed to open %s: %s",
  8437. name, strerror(errno));
  8438. return fd;
  8439. }
  8440. len = os_snprintf(tmp, sizeof(tmp), "%u\n", val);
  8441. len = write(fd, tmp, len);
  8442. if (len < 0)
  8443. wpa_printf(MSG_ERROR, "nl80211: Failed to write to %s: %s",
  8444. name, strerror(errno));
  8445. close(fd);
  8446. return 0;
  8447. }
  8448. static int nl80211_configure_data_frame_filters(void *priv, u32 filter_flags)
  8449. {
  8450. struct i802_bss *bss = priv;
  8451. char path[128];
  8452. int ret;
  8453. wpa_printf(MSG_DEBUG, "nl80211: Data frame filter flags=0x%x",
  8454. filter_flags);
  8455. /* Configure filtering of unicast frame encrypted using GTK */
  8456. ret = os_snprintf(path, sizeof(path),
  8457. "/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
  8458. bss->ifname);
  8459. if (os_snprintf_error(sizeof(path), ret))
  8460. return -1;
  8461. ret = nl80211_write_to_file(path,
  8462. !!(filter_flags &
  8463. WPA_DATA_FRAME_FILTER_FLAG_GTK));
  8464. if (ret) {
  8465. wpa_printf(MSG_ERROR,
  8466. "nl80211: Failed to set IPv4 unicast in multicast filter");
  8467. return ret;
  8468. }
  8469. os_snprintf(path, sizeof(path),
  8470. "/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
  8471. bss->ifname);
  8472. ret = nl80211_write_to_file(path,
  8473. !!(filter_flags &
  8474. WPA_DATA_FRAME_FILTER_FLAG_GTK));
  8475. if (ret) {
  8476. wpa_printf(MSG_ERROR,
  8477. "nl80211: Failed to set IPv6 unicast in multicast filter");
  8478. return ret;
  8479. }
  8480. /* Configure filtering of unicast frame encrypted using GTK */
  8481. os_snprintf(path, sizeof(path),
  8482. "/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
  8483. bss->ifname);
  8484. ret = nl80211_write_to_file(path,
  8485. !!(filter_flags &
  8486. WPA_DATA_FRAME_FILTER_FLAG_ARP));
  8487. if (ret) {
  8488. wpa_printf(MSG_ERROR,
  8489. "nl80211: Failed set gratuitous ARP filter");
  8490. return ret;
  8491. }
  8492. /* Configure filtering of IPv6 NA frames */
  8493. os_snprintf(path, sizeof(path),
  8494. "/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
  8495. bss->ifname);
  8496. ret = nl80211_write_to_file(path,
  8497. !!(filter_flags &
  8498. WPA_DATA_FRAME_FILTER_FLAG_NA));
  8499. if (ret) {
  8500. wpa_printf(MSG_ERROR,
  8501. "nl80211: Failed to set unsolicited NA filter");
  8502. return ret;
  8503. }
  8504. return 0;
  8505. }
  8506. static int nl80211_get_ext_capab(void *priv, enum wpa_driver_if_type type,
  8507. const u8 **ext_capa, const u8 **ext_capa_mask,
  8508. unsigned int *ext_capa_len)
  8509. {
  8510. struct i802_bss *bss = priv;
  8511. struct wpa_driver_nl80211_data *drv = bss->drv;
  8512. enum nl80211_iftype nlmode;
  8513. unsigned int i;
  8514. if (!ext_capa || !ext_capa_mask || !ext_capa_len)
  8515. return -1;
  8516. nlmode = wpa_driver_nl80211_if_type(type);
  8517. /* By default, use the per-radio values */
  8518. *ext_capa = drv->extended_capa;
  8519. *ext_capa_mask = drv->extended_capa_mask;
  8520. *ext_capa_len = drv->extended_capa_len;
  8521. /* Replace the default value if a per-interface type value exists */
  8522. for (i = 0; i < drv->num_iface_ext_capa; i++) {
  8523. if (nlmode == drv->iface_ext_capa[i].iftype) {
  8524. *ext_capa = drv->iface_ext_capa[i].ext_capa;
  8525. *ext_capa_mask = drv->iface_ext_capa[i].ext_capa_mask;
  8526. *ext_capa_len = drv->iface_ext_capa[i].ext_capa_len;
  8527. break;
  8528. }
  8529. }
  8530. return 0;
  8531. }
  8532. const struct wpa_driver_ops wpa_driver_nl80211_ops = {
  8533. .name = "nl80211",
  8534. .desc = "Linux nl80211/cfg80211",
  8535. .get_bssid = wpa_driver_nl80211_get_bssid,
  8536. .get_ssid = wpa_driver_nl80211_get_ssid,
  8537. .set_key = driver_nl80211_set_key,
  8538. .scan2 = driver_nl80211_scan2,
  8539. .sched_scan = wpa_driver_nl80211_sched_scan,
  8540. .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
  8541. .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
  8542. .abort_scan = wpa_driver_nl80211_abort_scan,
  8543. .deauthenticate = driver_nl80211_deauthenticate,
  8544. .authenticate = driver_nl80211_authenticate,
  8545. .associate = wpa_driver_nl80211_associate,
  8546. .global_init = nl80211_global_init,
  8547. .global_deinit = nl80211_global_deinit,
  8548. .init2 = wpa_driver_nl80211_init,
  8549. .deinit = driver_nl80211_deinit,
  8550. .get_capa = wpa_driver_nl80211_get_capa,
  8551. .set_operstate = wpa_driver_nl80211_set_operstate,
  8552. .set_supp_port = wpa_driver_nl80211_set_supp_port,
  8553. .set_country = wpa_driver_nl80211_set_country,
  8554. .get_country = wpa_driver_nl80211_get_country,
  8555. .set_ap = wpa_driver_nl80211_set_ap,
  8556. .set_acl = wpa_driver_nl80211_set_acl,
  8557. .if_add = wpa_driver_nl80211_if_add,
  8558. .if_remove = driver_nl80211_if_remove,
  8559. .send_mlme = driver_nl80211_send_mlme,
  8560. .get_hw_feature_data = nl80211_get_hw_feature_data,
  8561. .sta_add = wpa_driver_nl80211_sta_add,
  8562. .sta_remove = driver_nl80211_sta_remove,
  8563. .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
  8564. .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
  8565. .hapd_init = i802_init,
  8566. .hapd_deinit = i802_deinit,
  8567. .set_wds_sta = i802_set_wds_sta,
  8568. .get_seqnum = i802_get_seqnum,
  8569. .flush = i802_flush,
  8570. .get_inact_sec = i802_get_inact_sec,
  8571. .sta_clear_stats = i802_sta_clear_stats,
  8572. .set_rts = i802_set_rts,
  8573. .set_frag = i802_set_frag,
  8574. .set_tx_queue_params = i802_set_tx_queue_params,
  8575. .set_sta_vlan = driver_nl80211_set_sta_vlan,
  8576. .sta_deauth = i802_sta_deauth,
  8577. .sta_disassoc = i802_sta_disassoc,
  8578. .read_sta_data = driver_nl80211_read_sta_data,
  8579. .set_freq = i802_set_freq,
  8580. .send_action = driver_nl80211_send_action,
  8581. .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
  8582. .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
  8583. .cancel_remain_on_channel =
  8584. wpa_driver_nl80211_cancel_remain_on_channel,
  8585. .probe_req_report = driver_nl80211_probe_req_report,
  8586. .deinit_ap = wpa_driver_nl80211_deinit_ap,
  8587. .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
  8588. .resume = wpa_driver_nl80211_resume,
  8589. .signal_monitor = nl80211_signal_monitor,
  8590. .signal_poll = nl80211_signal_poll,
  8591. .send_frame = nl80211_send_frame,
  8592. .set_param = nl80211_set_param,
  8593. .get_radio_name = nl80211_get_radio_name,
  8594. .add_pmkid = nl80211_add_pmkid,
  8595. .remove_pmkid = nl80211_remove_pmkid,
  8596. .flush_pmkid = nl80211_flush_pmkid,
  8597. .set_rekey_info = nl80211_set_rekey_info,
  8598. .poll_client = nl80211_poll_client,
  8599. .set_p2p_powersave = nl80211_set_p2p_powersave,
  8600. .start_dfs_cac = nl80211_start_radar_detection,
  8601. .stop_ap = wpa_driver_nl80211_stop_ap,
  8602. #ifdef CONFIG_TDLS
  8603. .send_tdls_mgmt = nl80211_send_tdls_mgmt,
  8604. .tdls_oper = nl80211_tdls_oper,
  8605. .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
  8606. .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
  8607. #endif /* CONFIG_TDLS */
  8608. .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
  8609. .get_mac_addr = wpa_driver_nl80211_get_macaddr,
  8610. .get_survey = wpa_driver_nl80211_get_survey,
  8611. .status = wpa_driver_nl80211_status,
  8612. .switch_channel = nl80211_switch_channel,
  8613. #ifdef ANDROID_P2P
  8614. .set_noa = wpa_driver_set_p2p_noa,
  8615. .get_noa = wpa_driver_get_p2p_noa,
  8616. .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
  8617. #endif /* ANDROID_P2P */
  8618. #ifdef ANDROID
  8619. #ifndef ANDROID_LIB_STUB
  8620. .driver_cmd = wpa_driver_nl80211_driver_cmd,
  8621. #endif /* !ANDROID_LIB_STUB */
  8622. #endif /* ANDROID */
  8623. .vendor_cmd = nl80211_vendor_cmd,
  8624. .set_qos_map = nl80211_set_qos_map,
  8625. .set_wowlan = nl80211_set_wowlan,
  8626. .set_mac_addr = nl80211_set_mac_addr,
  8627. #ifdef CONFIG_MESH
  8628. .init_mesh = wpa_driver_nl80211_init_mesh,
  8629. .join_mesh = wpa_driver_nl80211_join_mesh,
  8630. .leave_mesh = wpa_driver_nl80211_leave_mesh,
  8631. #endif /* CONFIG_MESH */
  8632. .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
  8633. .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
  8634. .br_port_set_attr = wpa_driver_br_port_set_attr,
  8635. .br_set_net_param = wpa_driver_br_set_net_param,
  8636. .add_tx_ts = nl80211_add_ts,
  8637. .del_tx_ts = nl80211_del_ts,
  8638. .get_ifindex = nl80211_get_ifindex,
  8639. #ifdef CONFIG_DRIVER_NL80211_QCA
  8640. .roaming = nl80211_roaming,
  8641. .do_acs = wpa_driver_do_acs,
  8642. .set_band = nl80211_set_band,
  8643. .get_pref_freq_list = nl80211_get_pref_freq_list,
  8644. .set_prob_oper_freq = nl80211_set_prob_oper_freq,
  8645. .p2p_lo_start = nl80211_p2p_lo_start,
  8646. .p2p_lo_stop = nl80211_p2p_lo_stop,
  8647. .set_default_scan_ies = nl80211_set_default_scan_ies,
  8648. .set_tdls_mode = nl80211_set_tdls_mode,
  8649. #ifdef CONFIG_MBO
  8650. .get_bss_transition_status = nl80211_get_bss_transition_status,
  8651. .ignore_assoc_disallow = nl80211_ignore_assoc_disallow,
  8652. #endif /* CONFIG_MBO */
  8653. #endif /* CONFIG_DRIVER_NL80211_QCA */
  8654. .configure_data_frame_filters = nl80211_configure_data_frame_filters,
  8655. .get_ext_capab = nl80211_get_ext_capab,
  8656. };