driver_nl80211.c 274 KB

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