driver_nl80211.c 317 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094
  1. /*
  2. * Driver interaction with Linux nl80211/cfg80211
  3. * Copyright (c) 2002-2014, 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/ioctl.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <fcntl.h>
  17. #include <net/if.h>
  18. #include <netlink/genl/genl.h>
  19. #include <netlink/genl/family.h>
  20. #include <netlink/genl/ctrl.h>
  21. #include <linux/rtnetlink.h>
  22. #include <netpacket/packet.h>
  23. #include <linux/filter.h>
  24. #include <linux/errqueue.h>
  25. #include "nl80211_copy.h"
  26. #include "common.h"
  27. #include "eloop.h"
  28. #include "utils/list.h"
  29. #include "common/qca-vendor.h"
  30. #include "common/ieee802_11_defs.h"
  31. #include "common/ieee802_11_common.h"
  32. #include "l2_packet/l2_packet.h"
  33. #include "netlink.h"
  34. #include "linux_ioctl.h"
  35. #include "radiotap.h"
  36. #include "radiotap_iter.h"
  37. #include "rfkill.h"
  38. #include "driver.h"
  39. #ifndef SO_WIFI_STATUS
  40. # if defined(__sparc__)
  41. # define SO_WIFI_STATUS 0x0025
  42. # elif defined(__parisc__)
  43. # define SO_WIFI_STATUS 0x4022
  44. # else
  45. # define SO_WIFI_STATUS 41
  46. # endif
  47. # define SCM_WIFI_STATUS SO_WIFI_STATUS
  48. #endif
  49. #ifndef SO_EE_ORIGIN_TXSTATUS
  50. #define SO_EE_ORIGIN_TXSTATUS 4
  51. #endif
  52. #ifndef PACKET_TX_TIMESTAMP
  53. #define PACKET_TX_TIMESTAMP 16
  54. #endif
  55. #ifdef ANDROID
  56. #include "android_drv.h"
  57. #endif /* ANDROID */
  58. #ifdef CONFIG_LIBNL20
  59. /* libnl 2.0 compatibility code */
  60. #define nl_handle nl_sock
  61. #define nl80211_handle_alloc nl_socket_alloc_cb
  62. #define nl80211_handle_destroy nl_socket_free
  63. #else
  64. /*
  65. * libnl 1.1 has a bug, it tries to allocate socket numbers densely
  66. * but when you free a socket again it will mess up its bitmap and
  67. * and use the wrong number the next time it needs a socket ID.
  68. * Therefore, we wrap the handle alloc/destroy and add our own pid
  69. * accounting.
  70. */
  71. static uint32_t port_bitmap[32] = { 0 };
  72. static struct nl_handle *nl80211_handle_alloc(void *cb)
  73. {
  74. struct nl_handle *handle;
  75. uint32_t pid = getpid() & 0x3FFFFF;
  76. int i;
  77. handle = nl_handle_alloc_cb(cb);
  78. for (i = 0; i < 1024; i++) {
  79. if (port_bitmap[i / 32] & (1 << (i % 32)))
  80. continue;
  81. port_bitmap[i / 32] |= 1 << (i % 32);
  82. pid += i << 22;
  83. break;
  84. }
  85. nl_socket_set_local_port(handle, pid);
  86. return handle;
  87. }
  88. static void nl80211_handle_destroy(struct nl_handle *handle)
  89. {
  90. uint32_t port = nl_socket_get_local_port(handle);
  91. port >>= 22;
  92. port_bitmap[port / 32] &= ~(1 << (port % 32));
  93. nl_handle_destroy(handle);
  94. }
  95. #endif /* CONFIG_LIBNL20 */
  96. #ifdef ANDROID
  97. /* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
  98. static int android_nl_socket_set_nonblocking(struct nl_handle *handle)
  99. {
  100. return fcntl(nl_socket_get_fd(handle), F_SETFL, O_NONBLOCK);
  101. }
  102. #undef nl_socket_set_nonblocking
  103. #define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
  104. #endif /* ANDROID */
  105. static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
  106. {
  107. struct nl_handle *handle;
  108. handle = nl80211_handle_alloc(cb);
  109. if (handle == NULL) {
  110. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  111. "callbacks (%s)", dbg);
  112. return NULL;
  113. }
  114. if (genl_connect(handle)) {
  115. wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
  116. "netlink (%s)", dbg);
  117. nl80211_handle_destroy(handle);
  118. return NULL;
  119. }
  120. return handle;
  121. }
  122. static void nl_destroy_handles(struct nl_handle **handle)
  123. {
  124. if (*handle == NULL)
  125. return;
  126. nl80211_handle_destroy(*handle);
  127. *handle = NULL;
  128. }
  129. #if __WORDSIZE == 64
  130. #define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
  131. #else
  132. #define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
  133. #endif
  134. static void nl80211_register_eloop_read(struct nl_handle **handle,
  135. eloop_sock_handler handler,
  136. void *eloop_data)
  137. {
  138. nl_socket_set_nonblocking(*handle);
  139. eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
  140. eloop_data, *handle);
  141. *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
  142. }
  143. static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
  144. {
  145. *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
  146. eloop_unregister_read_sock(nl_socket_get_fd(*handle));
  147. nl_destroy_handles(handle);
  148. }
  149. #ifndef IFF_LOWER_UP
  150. #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
  151. #endif
  152. #ifndef IFF_DORMANT
  153. #define IFF_DORMANT 0x20000 /* driver signals dormant */
  154. #endif
  155. #ifndef IF_OPER_DORMANT
  156. #define IF_OPER_DORMANT 5
  157. #endif
  158. #ifndef IF_OPER_UP
  159. #define IF_OPER_UP 6
  160. #endif
  161. struct nl80211_global {
  162. struct dl_list interfaces;
  163. int if_add_ifindex;
  164. u64 if_add_wdevid;
  165. int if_add_wdevid_set;
  166. struct netlink_data *netlink;
  167. struct nl_cb *nl_cb;
  168. struct nl_handle *nl;
  169. int nl80211_id;
  170. int ioctl_sock; /* socket for ioctl() use */
  171. struct nl_handle *nl_event;
  172. };
  173. struct nl80211_wiphy_data {
  174. struct dl_list list;
  175. struct dl_list bsss;
  176. struct dl_list drvs;
  177. struct nl_handle *nl_beacons;
  178. struct nl_cb *nl_cb;
  179. int wiphy_idx;
  180. };
  181. static void nl80211_global_deinit(void *priv);
  182. struct i802_bss {
  183. struct wpa_driver_nl80211_data *drv;
  184. struct i802_bss *next;
  185. int ifindex;
  186. u64 wdev_id;
  187. char ifname[IFNAMSIZ + 1];
  188. char brname[IFNAMSIZ];
  189. unsigned int beacon_set:1;
  190. unsigned int added_if_into_bridge:1;
  191. unsigned int added_bridge:1;
  192. unsigned int in_deinit:1;
  193. unsigned int wdev_id_set:1;
  194. unsigned int added_if:1;
  195. u8 addr[ETH_ALEN];
  196. int freq;
  197. int if_dynamic;
  198. void *ctx;
  199. struct nl_handle *nl_preq, *nl_mgmt;
  200. struct nl_cb *nl_cb;
  201. struct nl80211_wiphy_data *wiphy_data;
  202. struct dl_list wiphy_list;
  203. };
  204. struct wpa_driver_nl80211_data {
  205. struct nl80211_global *global;
  206. struct dl_list list;
  207. struct dl_list wiphy_list;
  208. char phyname[32];
  209. void *ctx;
  210. int ifindex;
  211. int if_removed;
  212. int if_disabled;
  213. int ignore_if_down_event;
  214. struct rfkill_data *rfkill;
  215. struct wpa_driver_capa capa;
  216. u8 *extended_capa, *extended_capa_mask;
  217. unsigned int extended_capa_len;
  218. int has_capability;
  219. int operstate;
  220. int scan_complete_events;
  221. enum scan_states {
  222. NO_SCAN, SCAN_REQUESTED, SCAN_STARTED, SCAN_COMPLETED,
  223. SCAN_ABORTED, SCHED_SCAN_STARTED, SCHED_SCAN_STOPPED,
  224. SCHED_SCAN_RESULTS
  225. } scan_state;
  226. struct nl_cb *nl_cb;
  227. u8 auth_bssid[ETH_ALEN];
  228. u8 auth_attempt_bssid[ETH_ALEN];
  229. u8 bssid[ETH_ALEN];
  230. u8 prev_bssid[ETH_ALEN];
  231. int associated;
  232. u8 ssid[32];
  233. size_t ssid_len;
  234. enum nl80211_iftype nlmode;
  235. enum nl80211_iftype ap_scan_as_station;
  236. unsigned int assoc_freq;
  237. int monitor_sock;
  238. int monitor_ifidx;
  239. int monitor_refcount;
  240. unsigned int disabled_11b_rates:1;
  241. unsigned int pending_remain_on_chan:1;
  242. unsigned int in_interface_list:1;
  243. unsigned int device_ap_sme:1;
  244. unsigned int poll_command_supported:1;
  245. unsigned int data_tx_status:1;
  246. unsigned int scan_for_auth:1;
  247. unsigned int retry_auth:1;
  248. unsigned int use_monitor:1;
  249. unsigned int ignore_next_local_disconnect:1;
  250. unsigned int allow_p2p_device:1;
  251. unsigned int hostapd:1;
  252. unsigned int start_mode_ap:1;
  253. unsigned int start_iface_up:1;
  254. unsigned int test_use_roc_tx:1;
  255. unsigned int ignore_deauth_event:1;
  256. unsigned int dfs_vendor_cmd_avail:1;
  257. u64 remain_on_chan_cookie;
  258. u64 send_action_cookie;
  259. unsigned int last_mgmt_freq;
  260. struct wpa_driver_scan_filter *filter_ssids;
  261. size_t num_filter_ssids;
  262. struct i802_bss *first_bss;
  263. int eapol_tx_sock;
  264. int eapol_sock; /* socket for EAPOL frames */
  265. int default_if_indices[16];
  266. int *if_indices;
  267. int num_if_indices;
  268. /* From failed authentication command */
  269. int auth_freq;
  270. u8 auth_bssid_[ETH_ALEN];
  271. u8 auth_ssid[32];
  272. size_t auth_ssid_len;
  273. int auth_alg;
  274. u8 *auth_ie;
  275. size_t auth_ie_len;
  276. u8 auth_wep_key[4][16];
  277. size_t auth_wep_key_len[4];
  278. int auth_wep_tx_keyidx;
  279. int auth_local_state_change;
  280. int auth_p2p;
  281. };
  282. static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
  283. static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
  284. void *timeout_ctx);
  285. static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
  286. enum nl80211_iftype nlmode);
  287. static int
  288. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
  289. const u8 *set_addr, int first);
  290. static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
  291. const u8 *addr, int cmd, u16 reason_code,
  292. int local_state_change);
  293. static void nl80211_remove_monitor_interface(
  294. struct wpa_driver_nl80211_data *drv);
  295. static int nl80211_send_frame_cmd(struct i802_bss *bss,
  296. unsigned int freq, unsigned int wait,
  297. const u8 *buf, size_t buf_len, u64 *cookie,
  298. int no_cck, int no_ack, int offchanok);
  299. static int nl80211_register_frame(struct i802_bss *bss,
  300. struct nl_handle *hl_handle,
  301. u16 type, const u8 *match, size_t match_len);
  302. static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
  303. int report);
  304. #ifdef ANDROID
  305. static int android_pno_start(struct i802_bss *bss,
  306. struct wpa_driver_scan_params *params);
  307. static int android_pno_stop(struct i802_bss *bss);
  308. extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
  309. size_t buf_len);
  310. #endif /* ANDROID */
  311. #ifdef ANDROID_P2P
  312. int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
  313. int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
  314. int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
  315. int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
  316. const struct wpabuf *proberesp,
  317. const struct wpabuf *assocresp);
  318. #endif /* ANDROID_P2P */
  319. static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  320. static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  321. static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  322. static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
  323. enum wpa_driver_if_type type,
  324. const char *ifname);
  325. static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
  326. struct hostapd_freq_params *freq);
  327. static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
  328. int ifindex, int disabled);
  329. static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
  330. static int wpa_driver_nl80211_authenticate_retry(
  331. struct wpa_driver_nl80211_data *drv);
  332. static int i802_set_iface_flags(struct i802_bss *bss, int up);
  333. static const char * nl80211_command_to_string(enum nl80211_commands cmd)
  334. {
  335. #define C2S(x) case x: return #x;
  336. switch (cmd) {
  337. C2S(NL80211_CMD_UNSPEC)
  338. C2S(NL80211_CMD_GET_WIPHY)
  339. C2S(NL80211_CMD_SET_WIPHY)
  340. C2S(NL80211_CMD_NEW_WIPHY)
  341. C2S(NL80211_CMD_DEL_WIPHY)
  342. C2S(NL80211_CMD_GET_INTERFACE)
  343. C2S(NL80211_CMD_SET_INTERFACE)
  344. C2S(NL80211_CMD_NEW_INTERFACE)
  345. C2S(NL80211_CMD_DEL_INTERFACE)
  346. C2S(NL80211_CMD_GET_KEY)
  347. C2S(NL80211_CMD_SET_KEY)
  348. C2S(NL80211_CMD_NEW_KEY)
  349. C2S(NL80211_CMD_DEL_KEY)
  350. C2S(NL80211_CMD_GET_BEACON)
  351. C2S(NL80211_CMD_SET_BEACON)
  352. C2S(NL80211_CMD_START_AP)
  353. C2S(NL80211_CMD_STOP_AP)
  354. C2S(NL80211_CMD_GET_STATION)
  355. C2S(NL80211_CMD_SET_STATION)
  356. C2S(NL80211_CMD_NEW_STATION)
  357. C2S(NL80211_CMD_DEL_STATION)
  358. C2S(NL80211_CMD_GET_MPATH)
  359. C2S(NL80211_CMD_SET_MPATH)
  360. C2S(NL80211_CMD_NEW_MPATH)
  361. C2S(NL80211_CMD_DEL_MPATH)
  362. C2S(NL80211_CMD_SET_BSS)
  363. C2S(NL80211_CMD_SET_REG)
  364. C2S(NL80211_CMD_REQ_SET_REG)
  365. C2S(NL80211_CMD_GET_MESH_CONFIG)
  366. C2S(NL80211_CMD_SET_MESH_CONFIG)
  367. C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
  368. C2S(NL80211_CMD_GET_REG)
  369. C2S(NL80211_CMD_GET_SCAN)
  370. C2S(NL80211_CMD_TRIGGER_SCAN)
  371. C2S(NL80211_CMD_NEW_SCAN_RESULTS)
  372. C2S(NL80211_CMD_SCAN_ABORTED)
  373. C2S(NL80211_CMD_REG_CHANGE)
  374. C2S(NL80211_CMD_AUTHENTICATE)
  375. C2S(NL80211_CMD_ASSOCIATE)
  376. C2S(NL80211_CMD_DEAUTHENTICATE)
  377. C2S(NL80211_CMD_DISASSOCIATE)
  378. C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
  379. C2S(NL80211_CMD_REG_BEACON_HINT)
  380. C2S(NL80211_CMD_JOIN_IBSS)
  381. C2S(NL80211_CMD_LEAVE_IBSS)
  382. C2S(NL80211_CMD_TESTMODE)
  383. C2S(NL80211_CMD_CONNECT)
  384. C2S(NL80211_CMD_ROAM)
  385. C2S(NL80211_CMD_DISCONNECT)
  386. C2S(NL80211_CMD_SET_WIPHY_NETNS)
  387. C2S(NL80211_CMD_GET_SURVEY)
  388. C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
  389. C2S(NL80211_CMD_SET_PMKSA)
  390. C2S(NL80211_CMD_DEL_PMKSA)
  391. C2S(NL80211_CMD_FLUSH_PMKSA)
  392. C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
  393. C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
  394. C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
  395. C2S(NL80211_CMD_REGISTER_FRAME)
  396. C2S(NL80211_CMD_FRAME)
  397. C2S(NL80211_CMD_FRAME_TX_STATUS)
  398. C2S(NL80211_CMD_SET_POWER_SAVE)
  399. C2S(NL80211_CMD_GET_POWER_SAVE)
  400. C2S(NL80211_CMD_SET_CQM)
  401. C2S(NL80211_CMD_NOTIFY_CQM)
  402. C2S(NL80211_CMD_SET_CHANNEL)
  403. C2S(NL80211_CMD_SET_WDS_PEER)
  404. C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
  405. C2S(NL80211_CMD_JOIN_MESH)
  406. C2S(NL80211_CMD_LEAVE_MESH)
  407. C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
  408. C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
  409. C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
  410. C2S(NL80211_CMD_GET_WOWLAN)
  411. C2S(NL80211_CMD_SET_WOWLAN)
  412. C2S(NL80211_CMD_START_SCHED_SCAN)
  413. C2S(NL80211_CMD_STOP_SCHED_SCAN)
  414. C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
  415. C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
  416. C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
  417. C2S(NL80211_CMD_PMKSA_CANDIDATE)
  418. C2S(NL80211_CMD_TDLS_OPER)
  419. C2S(NL80211_CMD_TDLS_MGMT)
  420. C2S(NL80211_CMD_UNEXPECTED_FRAME)
  421. C2S(NL80211_CMD_PROBE_CLIENT)
  422. C2S(NL80211_CMD_REGISTER_BEACONS)
  423. C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
  424. C2S(NL80211_CMD_SET_NOACK_MAP)
  425. C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
  426. C2S(NL80211_CMD_START_P2P_DEVICE)
  427. C2S(NL80211_CMD_STOP_P2P_DEVICE)
  428. C2S(NL80211_CMD_CONN_FAILED)
  429. C2S(NL80211_CMD_SET_MCAST_RATE)
  430. C2S(NL80211_CMD_SET_MAC_ACL)
  431. C2S(NL80211_CMD_RADAR_DETECT)
  432. C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
  433. C2S(NL80211_CMD_UPDATE_FT_IES)
  434. C2S(NL80211_CMD_FT_EVENT)
  435. C2S(NL80211_CMD_CRIT_PROTOCOL_START)
  436. C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
  437. C2S(NL80211_CMD_GET_COALESCE)
  438. C2S(NL80211_CMD_SET_COALESCE)
  439. C2S(NL80211_CMD_CHANNEL_SWITCH)
  440. C2S(NL80211_CMD_VENDOR)
  441. C2S(NL80211_CMD_SET_QOS_MAP)
  442. default:
  443. return "NL80211_CMD_UNKNOWN";
  444. }
  445. #undef C2S
  446. }
  447. /* Converts nl80211_chan_width to a common format */
  448. static enum chan_width convert2width(int width)
  449. {
  450. switch (width) {
  451. case NL80211_CHAN_WIDTH_20_NOHT:
  452. return CHAN_WIDTH_20_NOHT;
  453. case NL80211_CHAN_WIDTH_20:
  454. return CHAN_WIDTH_20;
  455. case NL80211_CHAN_WIDTH_40:
  456. return CHAN_WIDTH_40;
  457. case NL80211_CHAN_WIDTH_80:
  458. return CHAN_WIDTH_80;
  459. case NL80211_CHAN_WIDTH_80P80:
  460. return CHAN_WIDTH_80P80;
  461. case NL80211_CHAN_WIDTH_160:
  462. return CHAN_WIDTH_160;
  463. }
  464. return CHAN_WIDTH_UNKNOWN;
  465. }
  466. static int is_ap_interface(enum nl80211_iftype nlmode)
  467. {
  468. return (nlmode == NL80211_IFTYPE_AP ||
  469. nlmode == NL80211_IFTYPE_P2P_GO);
  470. }
  471. static int is_sta_interface(enum nl80211_iftype nlmode)
  472. {
  473. return (nlmode == NL80211_IFTYPE_STATION ||
  474. nlmode == NL80211_IFTYPE_P2P_CLIENT);
  475. }
  476. static int is_p2p_net_interface(enum nl80211_iftype nlmode)
  477. {
  478. return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
  479. nlmode == NL80211_IFTYPE_P2P_GO);
  480. }
  481. static void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
  482. {
  483. if (drv->associated)
  484. os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
  485. drv->associated = 0;
  486. os_memset(drv->bssid, 0, ETH_ALEN);
  487. }
  488. struct nl80211_bss_info_arg {
  489. struct wpa_driver_nl80211_data *drv;
  490. struct wpa_scan_results *res;
  491. unsigned int assoc_freq;
  492. u8 assoc_bssid[ETH_ALEN];
  493. };
  494. static int bss_info_handler(struct nl_msg *msg, void *arg);
  495. /* nl80211 code */
  496. static int ack_handler(struct nl_msg *msg, void *arg)
  497. {
  498. int *err = arg;
  499. *err = 0;
  500. return NL_STOP;
  501. }
  502. static int finish_handler(struct nl_msg *msg, void *arg)
  503. {
  504. int *ret = arg;
  505. *ret = 0;
  506. return NL_SKIP;
  507. }
  508. static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
  509. void *arg)
  510. {
  511. int *ret = arg;
  512. *ret = err->error;
  513. return NL_SKIP;
  514. }
  515. static int no_seq_check(struct nl_msg *msg, void *arg)
  516. {
  517. return NL_OK;
  518. }
  519. static int send_and_recv(struct nl80211_global *global,
  520. struct nl_handle *nl_handle, struct nl_msg *msg,
  521. int (*valid_handler)(struct nl_msg *, void *),
  522. void *valid_data)
  523. {
  524. struct nl_cb *cb;
  525. int err = -ENOMEM;
  526. cb = nl_cb_clone(global->nl_cb);
  527. if (!cb)
  528. goto out;
  529. err = nl_send_auto_complete(nl_handle, msg);
  530. if (err < 0)
  531. goto out;
  532. err = 1;
  533. nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
  534. nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
  535. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
  536. if (valid_handler)
  537. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
  538. valid_handler, valid_data);
  539. while (err > 0) {
  540. int res = nl_recvmsgs(nl_handle, cb);
  541. if (res) {
  542. wpa_printf(MSG_INFO,
  543. "nl80211: %s->nl_recvmsgs failed: %d",
  544. __func__, res);
  545. }
  546. }
  547. out:
  548. nl_cb_put(cb);
  549. nlmsg_free(msg);
  550. return err;
  551. }
  552. static int send_and_recv_msgs_global(struct nl80211_global *global,
  553. struct nl_msg *msg,
  554. int (*valid_handler)(struct nl_msg *, void *),
  555. void *valid_data)
  556. {
  557. return send_and_recv(global, global->nl, msg, valid_handler,
  558. valid_data);
  559. }
  560. static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
  561. struct nl_msg *msg,
  562. int (*valid_handler)(struct nl_msg *, void *),
  563. void *valid_data)
  564. {
  565. return send_and_recv(drv->global, drv->global->nl, msg,
  566. valid_handler, valid_data);
  567. }
  568. struct family_data {
  569. const char *group;
  570. int id;
  571. };
  572. static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
  573. {
  574. if (bss->wdev_id_set)
  575. NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
  576. else
  577. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  578. return 0;
  579. nla_put_failure:
  580. return -1;
  581. }
  582. static int family_handler(struct nl_msg *msg, void *arg)
  583. {
  584. struct family_data *res = arg;
  585. struct nlattr *tb[CTRL_ATTR_MAX + 1];
  586. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  587. struct nlattr *mcgrp;
  588. int i;
  589. nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  590. genlmsg_attrlen(gnlh, 0), NULL);
  591. if (!tb[CTRL_ATTR_MCAST_GROUPS])
  592. return NL_SKIP;
  593. nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
  594. struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
  595. nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
  596. nla_len(mcgrp), NULL);
  597. if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
  598. !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
  599. os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
  600. res->group,
  601. nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
  602. continue;
  603. res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
  604. break;
  605. };
  606. return NL_SKIP;
  607. }
  608. static int nl_get_multicast_id(struct nl80211_global *global,
  609. const char *family, const char *group)
  610. {
  611. struct nl_msg *msg;
  612. int ret = -1;
  613. struct family_data res = { group, -ENOENT };
  614. msg = nlmsg_alloc();
  615. if (!msg)
  616. return -ENOMEM;
  617. genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
  618. 0, 0, CTRL_CMD_GETFAMILY, 0);
  619. NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
  620. ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
  621. msg = NULL;
  622. if (ret == 0)
  623. ret = res.id;
  624. nla_put_failure:
  625. nlmsg_free(msg);
  626. return ret;
  627. }
  628. static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
  629. struct nl_msg *msg, int flags, uint8_t cmd)
  630. {
  631. return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
  632. 0, flags, cmd, 0);
  633. }
  634. struct wiphy_idx_data {
  635. int wiphy_idx;
  636. enum nl80211_iftype nlmode;
  637. u8 *macaddr;
  638. };
  639. static int netdev_info_handler(struct nl_msg *msg, void *arg)
  640. {
  641. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  642. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  643. struct wiphy_idx_data *info = arg;
  644. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  645. genlmsg_attrlen(gnlh, 0), NULL);
  646. if (tb[NL80211_ATTR_WIPHY])
  647. info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
  648. if (tb[NL80211_ATTR_IFTYPE])
  649. info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
  650. if (tb[NL80211_ATTR_MAC] && info->macaddr)
  651. os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
  652. ETH_ALEN);
  653. return NL_SKIP;
  654. }
  655. static int nl80211_get_wiphy_index(struct i802_bss *bss)
  656. {
  657. struct nl_msg *msg;
  658. struct wiphy_idx_data data = {
  659. .wiphy_idx = -1,
  660. .macaddr = NULL,
  661. };
  662. msg = nlmsg_alloc();
  663. if (!msg)
  664. return NL80211_IFTYPE_UNSPECIFIED;
  665. nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
  666. if (nl80211_set_iface_id(msg, bss) < 0)
  667. goto nla_put_failure;
  668. if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
  669. return data.wiphy_idx;
  670. msg = NULL;
  671. nla_put_failure:
  672. nlmsg_free(msg);
  673. return -1;
  674. }
  675. static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
  676. {
  677. struct nl_msg *msg;
  678. struct wiphy_idx_data data = {
  679. .nlmode = NL80211_IFTYPE_UNSPECIFIED,
  680. .macaddr = NULL,
  681. };
  682. msg = nlmsg_alloc();
  683. if (!msg)
  684. return -1;
  685. nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
  686. if (nl80211_set_iface_id(msg, bss) < 0)
  687. goto nla_put_failure;
  688. if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
  689. return data.nlmode;
  690. msg = NULL;
  691. nla_put_failure:
  692. nlmsg_free(msg);
  693. return NL80211_IFTYPE_UNSPECIFIED;
  694. }
  695. static int nl80211_get_macaddr(struct i802_bss *bss)
  696. {
  697. struct nl_msg *msg;
  698. struct wiphy_idx_data data = {
  699. .macaddr = bss->addr,
  700. };
  701. msg = nlmsg_alloc();
  702. if (!msg)
  703. return NL80211_IFTYPE_UNSPECIFIED;
  704. nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
  705. if (nl80211_set_iface_id(msg, bss) < 0)
  706. goto nla_put_failure;
  707. return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
  708. nla_put_failure:
  709. nlmsg_free(msg);
  710. return NL80211_IFTYPE_UNSPECIFIED;
  711. }
  712. static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
  713. struct nl80211_wiphy_data *w)
  714. {
  715. struct nl_msg *msg;
  716. int ret = -1;
  717. msg = nlmsg_alloc();
  718. if (!msg)
  719. return -1;
  720. nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
  721. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
  722. ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
  723. msg = NULL;
  724. if (ret) {
  725. wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
  726. "failed: ret=%d (%s)",
  727. ret, strerror(-ret));
  728. goto nla_put_failure;
  729. }
  730. ret = 0;
  731. nla_put_failure:
  732. nlmsg_free(msg);
  733. return ret;
  734. }
  735. static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
  736. {
  737. struct nl80211_wiphy_data *w = eloop_ctx;
  738. int res;
  739. wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
  740. res = nl_recvmsgs(handle, w->nl_cb);
  741. if (res) {
  742. wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
  743. __func__, res);
  744. }
  745. }
  746. static int process_beacon_event(struct nl_msg *msg, void *arg)
  747. {
  748. struct nl80211_wiphy_data *w = arg;
  749. struct wpa_driver_nl80211_data *drv;
  750. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  751. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  752. union wpa_event_data event;
  753. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  754. genlmsg_attrlen(gnlh, 0), NULL);
  755. if (gnlh->cmd != NL80211_CMD_FRAME) {
  756. wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
  757. gnlh->cmd);
  758. return NL_SKIP;
  759. }
  760. if (!tb[NL80211_ATTR_FRAME])
  761. return NL_SKIP;
  762. dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
  763. wiphy_list) {
  764. os_memset(&event, 0, sizeof(event));
  765. event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
  766. event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
  767. wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
  768. }
  769. return NL_SKIP;
  770. }
  771. static struct nl80211_wiphy_data *
  772. nl80211_get_wiphy_data_ap(struct i802_bss *bss)
  773. {
  774. static DEFINE_DL_LIST(nl80211_wiphys);
  775. struct nl80211_wiphy_data *w;
  776. int wiphy_idx, found = 0;
  777. struct i802_bss *tmp_bss;
  778. if (bss->wiphy_data != NULL)
  779. return bss->wiphy_data;
  780. wiphy_idx = nl80211_get_wiphy_index(bss);
  781. dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
  782. if (w->wiphy_idx == wiphy_idx)
  783. goto add;
  784. }
  785. /* alloc new one */
  786. w = os_zalloc(sizeof(*w));
  787. if (w == NULL)
  788. return NULL;
  789. w->wiphy_idx = wiphy_idx;
  790. dl_list_init(&w->bsss);
  791. dl_list_init(&w->drvs);
  792. w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  793. if (!w->nl_cb) {
  794. os_free(w);
  795. return NULL;
  796. }
  797. nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
  798. nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
  799. w);
  800. w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
  801. "wiphy beacons");
  802. if (w->nl_beacons == NULL) {
  803. os_free(w);
  804. return NULL;
  805. }
  806. if (nl80211_register_beacons(bss->drv, w)) {
  807. nl_destroy_handles(&w->nl_beacons);
  808. os_free(w);
  809. return NULL;
  810. }
  811. nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
  812. dl_list_add(&nl80211_wiphys, &w->list);
  813. add:
  814. /* drv entry for this bss already there? */
  815. dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
  816. if (tmp_bss->drv == bss->drv) {
  817. found = 1;
  818. break;
  819. }
  820. }
  821. /* if not add it */
  822. if (!found)
  823. dl_list_add(&w->drvs, &bss->drv->wiphy_list);
  824. dl_list_add(&w->bsss, &bss->wiphy_list);
  825. bss->wiphy_data = w;
  826. return w;
  827. }
  828. static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
  829. {
  830. struct nl80211_wiphy_data *w = bss->wiphy_data;
  831. struct i802_bss *tmp_bss;
  832. int found = 0;
  833. if (w == NULL)
  834. return;
  835. bss->wiphy_data = NULL;
  836. dl_list_del(&bss->wiphy_list);
  837. /* still any for this drv present? */
  838. dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
  839. if (tmp_bss->drv == bss->drv) {
  840. found = 1;
  841. break;
  842. }
  843. }
  844. /* if not remove it */
  845. if (!found)
  846. dl_list_del(&bss->drv->wiphy_list);
  847. if (!dl_list_empty(&w->bsss))
  848. return;
  849. nl80211_destroy_eloop_handle(&w->nl_beacons);
  850. nl_cb_put(w->nl_cb);
  851. dl_list_del(&w->list);
  852. os_free(w);
  853. }
  854. static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
  855. {
  856. struct i802_bss *bss = priv;
  857. struct wpa_driver_nl80211_data *drv = bss->drv;
  858. if (!drv->associated)
  859. return -1;
  860. os_memcpy(bssid, drv->bssid, ETH_ALEN);
  861. return 0;
  862. }
  863. static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
  864. {
  865. struct i802_bss *bss = priv;
  866. struct wpa_driver_nl80211_data *drv = bss->drv;
  867. if (!drv->associated)
  868. return -1;
  869. os_memcpy(ssid, drv->ssid, drv->ssid_len);
  870. return drv->ssid_len;
  871. }
  872. static void wpa_driver_nl80211_event_newlink(
  873. struct wpa_driver_nl80211_data *drv, char *ifname)
  874. {
  875. union wpa_event_data event;
  876. if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
  877. if (if_nametoindex(drv->first_bss->ifname) == 0) {
  878. wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
  879. drv->first_bss->ifname);
  880. return;
  881. }
  882. if (!drv->if_removed)
  883. return;
  884. wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
  885. drv->first_bss->ifname);
  886. drv->if_removed = 0;
  887. }
  888. os_memset(&event, 0, sizeof(event));
  889. os_strlcpy(event.interface_status.ifname, ifname,
  890. sizeof(event.interface_status.ifname));
  891. event.interface_status.ievent = EVENT_INTERFACE_ADDED;
  892. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
  893. }
  894. static void wpa_driver_nl80211_event_dellink(
  895. struct wpa_driver_nl80211_data *drv, char *ifname)
  896. {
  897. union wpa_event_data event;
  898. if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
  899. if (drv->if_removed) {
  900. wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
  901. ifname);
  902. return;
  903. }
  904. wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
  905. ifname);
  906. drv->if_removed = 1;
  907. } else {
  908. wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
  909. ifname);
  910. }
  911. os_memset(&event, 0, sizeof(event));
  912. os_strlcpy(event.interface_status.ifname, ifname,
  913. sizeof(event.interface_status.ifname));
  914. event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
  915. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
  916. }
  917. static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
  918. u8 *buf, size_t len)
  919. {
  920. int attrlen, rta_len;
  921. struct rtattr *attr;
  922. attrlen = len;
  923. attr = (struct rtattr *) buf;
  924. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  925. while (RTA_OK(attr, attrlen)) {
  926. if (attr->rta_type == IFLA_IFNAME) {
  927. if (os_strcmp(((char *) attr) + rta_len,
  928. drv->first_bss->ifname) == 0)
  929. return 1;
  930. else
  931. break;
  932. }
  933. attr = RTA_NEXT(attr, attrlen);
  934. }
  935. return 0;
  936. }
  937. static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
  938. int ifindex, u8 *buf, size_t len)
  939. {
  940. if (drv->ifindex == ifindex)
  941. return 1;
  942. if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
  943. wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
  944. "interface");
  945. wpa_driver_nl80211_finish_drv_init(drv, NULL, 0);
  946. return 1;
  947. }
  948. return 0;
  949. }
  950. static struct wpa_driver_nl80211_data *
  951. nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
  952. {
  953. struct wpa_driver_nl80211_data *drv;
  954. dl_list_for_each(drv, &global->interfaces,
  955. struct wpa_driver_nl80211_data, list) {
  956. if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
  957. have_ifidx(drv, idx))
  958. return drv;
  959. }
  960. return NULL;
  961. }
  962. static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
  963. struct ifinfomsg *ifi,
  964. u8 *buf, size_t len)
  965. {
  966. struct nl80211_global *global = ctx;
  967. struct wpa_driver_nl80211_data *drv;
  968. int attrlen;
  969. struct rtattr *attr;
  970. u32 brid = 0;
  971. char namebuf[IFNAMSIZ];
  972. char ifname[IFNAMSIZ + 1];
  973. char extra[100], *pos, *end;
  974. drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
  975. if (!drv) {
  976. wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
  977. ifi->ifi_index);
  978. return;
  979. }
  980. extra[0] = '\0';
  981. pos = extra;
  982. end = pos + sizeof(extra);
  983. ifname[0] = '\0';
  984. attrlen = len;
  985. attr = (struct rtattr *) buf;
  986. while (RTA_OK(attr, attrlen)) {
  987. switch (attr->rta_type) {
  988. case IFLA_IFNAME:
  989. if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
  990. break;
  991. os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
  992. ifname[RTA_PAYLOAD(attr)] = '\0';
  993. break;
  994. case IFLA_MASTER:
  995. brid = nla_get_u32((struct nlattr *) attr);
  996. pos += os_snprintf(pos, end - pos, " master=%u", brid);
  997. break;
  998. case IFLA_WIRELESS:
  999. pos += os_snprintf(pos, end - pos, " wext");
  1000. break;
  1001. case IFLA_OPERSTATE:
  1002. pos += os_snprintf(pos, end - pos, " operstate=%u",
  1003. nla_get_u32((struct nlattr *) attr));
  1004. break;
  1005. case IFLA_LINKMODE:
  1006. pos += os_snprintf(pos, end - pos, " linkmode=%u",
  1007. nla_get_u32((struct nlattr *) attr));
  1008. break;
  1009. }
  1010. attr = RTA_NEXT(attr, attrlen);
  1011. }
  1012. extra[sizeof(extra) - 1] = '\0';
  1013. wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_flags=0x%x (%s%s%s%s)",
  1014. ifi->ifi_index, ifname, extra, ifi->ifi_flags,
  1015. (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
  1016. (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
  1017. (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
  1018. (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
  1019. if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
  1020. if (if_indextoname(ifi->ifi_index, namebuf) &&
  1021. linux_iface_up(drv->global->ioctl_sock,
  1022. drv->first_bss->ifname) > 0) {
  1023. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
  1024. "event since interface %s is up", namebuf);
  1025. return;
  1026. }
  1027. wpa_printf(MSG_DEBUG, "nl80211: Interface down");
  1028. if (drv->ignore_if_down_event) {
  1029. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
  1030. "event generated by mode change");
  1031. drv->ignore_if_down_event = 0;
  1032. } else {
  1033. drv->if_disabled = 1;
  1034. wpa_supplicant_event(drv->ctx,
  1035. EVENT_INTERFACE_DISABLED, NULL);
  1036. /*
  1037. * Try to get drv again, since it may be removed as
  1038. * part of the EVENT_INTERFACE_DISABLED handling for
  1039. * dynamic interfaces
  1040. */
  1041. drv = nl80211_find_drv(global, ifi->ifi_index,
  1042. buf, len);
  1043. if (!drv)
  1044. return;
  1045. }
  1046. }
  1047. if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
  1048. if (if_indextoname(ifi->ifi_index, namebuf) &&
  1049. linux_iface_up(drv->global->ioctl_sock,
  1050. drv->first_bss->ifname) == 0) {
  1051. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
  1052. "event since interface %s is down",
  1053. namebuf);
  1054. } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
  1055. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
  1056. "event since interface %s does not exist",
  1057. drv->first_bss->ifname);
  1058. } else if (drv->if_removed) {
  1059. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
  1060. "event since interface %s is marked "
  1061. "removed", drv->first_bss->ifname);
  1062. } else {
  1063. wpa_printf(MSG_DEBUG, "nl80211: Interface up");
  1064. drv->if_disabled = 0;
  1065. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
  1066. NULL);
  1067. }
  1068. }
  1069. /*
  1070. * Some drivers send the association event before the operup event--in
  1071. * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
  1072. * fails. This will hit us when wpa_supplicant does not need to do
  1073. * IEEE 802.1X authentication
  1074. */
  1075. if (drv->operstate == 1 &&
  1076. (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
  1077. !(ifi->ifi_flags & IFF_RUNNING)) {
  1078. wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
  1079. netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
  1080. -1, IF_OPER_UP);
  1081. }
  1082. if (ifname[0])
  1083. wpa_driver_nl80211_event_newlink(drv, ifname);
  1084. if (ifi->ifi_family == AF_BRIDGE && brid) {
  1085. /* device has been added to bridge */
  1086. if_indextoname(brid, namebuf);
  1087. wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
  1088. brid, namebuf);
  1089. add_ifidx(drv, brid);
  1090. }
  1091. }
  1092. static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
  1093. struct ifinfomsg *ifi,
  1094. u8 *buf, size_t len)
  1095. {
  1096. struct nl80211_global *global = ctx;
  1097. struct wpa_driver_nl80211_data *drv;
  1098. int attrlen;
  1099. struct rtattr *attr;
  1100. u32 brid = 0;
  1101. char ifname[IFNAMSIZ + 1];
  1102. drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
  1103. if (!drv) {
  1104. wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
  1105. ifi->ifi_index);
  1106. return;
  1107. }
  1108. ifname[0] = '\0';
  1109. attrlen = len;
  1110. attr = (struct rtattr *) buf;
  1111. while (RTA_OK(attr, attrlen)) {
  1112. switch (attr->rta_type) {
  1113. case IFLA_IFNAME:
  1114. if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
  1115. break;
  1116. os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
  1117. ifname[RTA_PAYLOAD(attr)] = '\0';
  1118. break;
  1119. case IFLA_MASTER:
  1120. brid = nla_get_u32((struct nlattr *) attr);
  1121. break;
  1122. }
  1123. attr = RTA_NEXT(attr, attrlen);
  1124. }
  1125. if (ifname[0])
  1126. wpa_driver_nl80211_event_dellink(drv, ifname);
  1127. if (ifi->ifi_family == AF_BRIDGE && brid) {
  1128. /* device has been removed from bridge */
  1129. char namebuf[IFNAMSIZ];
  1130. if_indextoname(brid, namebuf);
  1131. wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
  1132. "%s", brid, namebuf);
  1133. del_ifidx(drv, brid);
  1134. }
  1135. }
  1136. static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
  1137. const u8 *frame, size_t len)
  1138. {
  1139. const struct ieee80211_mgmt *mgmt;
  1140. union wpa_event_data event;
  1141. wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
  1142. mgmt = (const struct ieee80211_mgmt *) frame;
  1143. if (len < 24 + sizeof(mgmt->u.auth)) {
  1144. wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
  1145. "frame");
  1146. return;
  1147. }
  1148. os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
  1149. os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
  1150. os_memset(&event, 0, sizeof(event));
  1151. os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
  1152. event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
  1153. event.auth.auth_transaction =
  1154. le_to_host16(mgmt->u.auth.auth_transaction);
  1155. event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
  1156. if (len > 24 + sizeof(mgmt->u.auth)) {
  1157. event.auth.ies = mgmt->u.auth.variable;
  1158. event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
  1159. }
  1160. wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
  1161. }
  1162. static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
  1163. {
  1164. struct nl_msg *msg;
  1165. int ret;
  1166. struct nl80211_bss_info_arg arg;
  1167. os_memset(&arg, 0, sizeof(arg));
  1168. msg = nlmsg_alloc();
  1169. if (!msg)
  1170. goto nla_put_failure;
  1171. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
  1172. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1173. arg.drv = drv;
  1174. ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
  1175. msg = NULL;
  1176. if (ret == 0) {
  1177. wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
  1178. "associated BSS from scan results: %u MHz",
  1179. arg.assoc_freq);
  1180. if (arg.assoc_freq)
  1181. drv->assoc_freq = arg.assoc_freq;
  1182. return drv->assoc_freq;
  1183. }
  1184. wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
  1185. "(%s)", ret, strerror(-ret));
  1186. nla_put_failure:
  1187. nlmsg_free(msg);
  1188. return drv->assoc_freq;
  1189. }
  1190. static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
  1191. const u8 *frame, size_t len)
  1192. {
  1193. const struct ieee80211_mgmt *mgmt;
  1194. union wpa_event_data event;
  1195. u16 status;
  1196. wpa_printf(MSG_DEBUG, "nl80211: Associate event");
  1197. mgmt = (const struct ieee80211_mgmt *) frame;
  1198. if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
  1199. wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
  1200. "frame");
  1201. return;
  1202. }
  1203. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  1204. if (status != WLAN_STATUS_SUCCESS) {
  1205. os_memset(&event, 0, sizeof(event));
  1206. event.assoc_reject.bssid = mgmt->bssid;
  1207. if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
  1208. event.assoc_reject.resp_ies =
  1209. (u8 *) mgmt->u.assoc_resp.variable;
  1210. event.assoc_reject.resp_ies_len =
  1211. len - 24 - sizeof(mgmt->u.assoc_resp);
  1212. }
  1213. event.assoc_reject.status_code = status;
  1214. wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
  1215. return;
  1216. }
  1217. drv->associated = 1;
  1218. os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
  1219. os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
  1220. os_memset(&event, 0, sizeof(event));
  1221. if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
  1222. event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
  1223. event.assoc_info.resp_ies_len =
  1224. len - 24 - sizeof(mgmt->u.assoc_resp);
  1225. }
  1226. event.assoc_info.freq = drv->assoc_freq;
  1227. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
  1228. }
  1229. static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
  1230. enum nl80211_commands cmd, struct nlattr *status,
  1231. struct nlattr *addr, struct nlattr *req_ie,
  1232. struct nlattr *resp_ie)
  1233. {
  1234. union wpa_event_data event;
  1235. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  1236. /*
  1237. * Avoid reporting two association events that would confuse
  1238. * the core code.
  1239. */
  1240. wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
  1241. "when using userspace SME", cmd);
  1242. return;
  1243. }
  1244. if (cmd == NL80211_CMD_CONNECT)
  1245. wpa_printf(MSG_DEBUG, "nl80211: Connect event");
  1246. else if (cmd == NL80211_CMD_ROAM)
  1247. wpa_printf(MSG_DEBUG, "nl80211: Roam event");
  1248. os_memset(&event, 0, sizeof(event));
  1249. if (cmd == NL80211_CMD_CONNECT &&
  1250. nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
  1251. if (addr)
  1252. event.assoc_reject.bssid = nla_data(addr);
  1253. if (resp_ie) {
  1254. event.assoc_reject.resp_ies = nla_data(resp_ie);
  1255. event.assoc_reject.resp_ies_len = nla_len(resp_ie);
  1256. }
  1257. event.assoc_reject.status_code = nla_get_u16(status);
  1258. wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
  1259. return;
  1260. }
  1261. drv->associated = 1;
  1262. if (addr) {
  1263. os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
  1264. os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
  1265. }
  1266. if (req_ie) {
  1267. event.assoc_info.req_ies = nla_data(req_ie);
  1268. event.assoc_info.req_ies_len = nla_len(req_ie);
  1269. }
  1270. if (resp_ie) {
  1271. event.assoc_info.resp_ies = nla_data(resp_ie);
  1272. event.assoc_info.resp_ies_len = nla_len(resp_ie);
  1273. }
  1274. event.assoc_info.freq = nl80211_get_assoc_freq(drv);
  1275. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
  1276. }
  1277. static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
  1278. struct nlattr *reason, struct nlattr *addr,
  1279. struct nlattr *by_ap)
  1280. {
  1281. union wpa_event_data data;
  1282. unsigned int locally_generated = by_ap == NULL;
  1283. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  1284. /*
  1285. * Avoid reporting two disassociation events that could
  1286. * confuse the core code.
  1287. */
  1288. wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
  1289. "event when using userspace SME");
  1290. return;
  1291. }
  1292. if (drv->ignore_next_local_disconnect) {
  1293. drv->ignore_next_local_disconnect = 0;
  1294. if (locally_generated) {
  1295. wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
  1296. "event triggered during reassociation");
  1297. return;
  1298. }
  1299. wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
  1300. "disconnect but got another disconnect "
  1301. "event first");
  1302. }
  1303. wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
  1304. nl80211_mark_disconnected(drv);
  1305. os_memset(&data, 0, sizeof(data));
  1306. if (reason)
  1307. data.deauth_info.reason_code = nla_get_u16(reason);
  1308. data.deauth_info.locally_generated = by_ap == NULL;
  1309. wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
  1310. }
  1311. static int calculate_chan_offset(int width, int freq, int cf1, int cf2)
  1312. {
  1313. int freq1 = 0;
  1314. switch (convert2width(width)) {
  1315. case CHAN_WIDTH_20_NOHT:
  1316. case CHAN_WIDTH_20:
  1317. return 0;
  1318. case CHAN_WIDTH_40:
  1319. freq1 = cf1 - 10;
  1320. break;
  1321. case CHAN_WIDTH_80:
  1322. freq1 = cf1 - 30;
  1323. break;
  1324. case CHAN_WIDTH_160:
  1325. freq1 = cf1 - 70;
  1326. break;
  1327. case CHAN_WIDTH_UNKNOWN:
  1328. case CHAN_WIDTH_80P80:
  1329. /* FIXME: implement this */
  1330. return 0;
  1331. }
  1332. return (abs(freq - freq1) / 20) % 2 == 0 ? 1 : -1;
  1333. }
  1334. static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
  1335. struct nlattr *ifindex, struct nlattr *freq,
  1336. struct nlattr *type, struct nlattr *bw,
  1337. struct nlattr *cf1, struct nlattr *cf2)
  1338. {
  1339. struct i802_bss *bss;
  1340. union wpa_event_data data;
  1341. int ht_enabled = 1;
  1342. int chan_offset = 0;
  1343. int ifidx;
  1344. wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
  1345. if (!freq)
  1346. return;
  1347. ifidx = nla_get_u32(ifindex);
  1348. for (bss = drv->first_bss; bss; bss = bss->next)
  1349. if (bss->ifindex == ifidx)
  1350. break;
  1351. if (bss == NULL) {
  1352. wpa_printf(MSG_WARNING, "nl80211: Unknown ifindex (%d) for channel switch, ignoring",
  1353. ifidx);
  1354. return;
  1355. }
  1356. if (type) {
  1357. switch (nla_get_u32(type)) {
  1358. case NL80211_CHAN_NO_HT:
  1359. ht_enabled = 0;
  1360. break;
  1361. case NL80211_CHAN_HT20:
  1362. break;
  1363. case NL80211_CHAN_HT40PLUS:
  1364. chan_offset = 1;
  1365. break;
  1366. case NL80211_CHAN_HT40MINUS:
  1367. chan_offset = -1;
  1368. break;
  1369. }
  1370. } else if (bw && cf1) {
  1371. /* This can happen for example with VHT80 ch switch */
  1372. chan_offset = calculate_chan_offset(nla_get_u32(bw),
  1373. nla_get_u32(freq),
  1374. nla_get_u32(cf1),
  1375. cf2 ? nla_get_u32(cf2) : 0);
  1376. } else {
  1377. wpa_printf(MSG_WARNING, "nl80211: Unknown secondary channel information - following channel definition calculations may fail");
  1378. }
  1379. os_memset(&data, 0, sizeof(data));
  1380. data.ch_switch.freq = nla_get_u32(freq);
  1381. data.ch_switch.ht_enabled = ht_enabled;
  1382. data.ch_switch.ch_offset = chan_offset;
  1383. if (bw)
  1384. data.ch_switch.ch_width = convert2width(nla_get_u32(bw));
  1385. if (cf1)
  1386. data.ch_switch.cf1 = nla_get_u32(cf1);
  1387. if (cf2)
  1388. data.ch_switch.cf2 = nla_get_u32(cf2);
  1389. bss->freq = data.ch_switch.freq;
  1390. wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
  1391. }
  1392. static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
  1393. enum nl80211_commands cmd, struct nlattr *addr)
  1394. {
  1395. union wpa_event_data event;
  1396. enum wpa_event_type ev;
  1397. if (nla_len(addr) != ETH_ALEN)
  1398. return;
  1399. wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
  1400. cmd, MAC2STR((u8 *) nla_data(addr)));
  1401. if (cmd == NL80211_CMD_AUTHENTICATE)
  1402. ev = EVENT_AUTH_TIMED_OUT;
  1403. else if (cmd == NL80211_CMD_ASSOCIATE)
  1404. ev = EVENT_ASSOC_TIMED_OUT;
  1405. else
  1406. return;
  1407. os_memset(&event, 0, sizeof(event));
  1408. os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
  1409. wpa_supplicant_event(drv->ctx, ev, &event);
  1410. }
  1411. static void mlme_event_mgmt(struct i802_bss *bss,
  1412. struct nlattr *freq, struct nlattr *sig,
  1413. const u8 *frame, size_t len)
  1414. {
  1415. struct wpa_driver_nl80211_data *drv = bss->drv;
  1416. const struct ieee80211_mgmt *mgmt;
  1417. union wpa_event_data event;
  1418. u16 fc, stype;
  1419. int ssi_signal = 0;
  1420. int rx_freq = 0;
  1421. wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
  1422. mgmt = (const struct ieee80211_mgmt *) frame;
  1423. if (len < 24) {
  1424. wpa_printf(MSG_DEBUG, "nl80211: Too short management frame");
  1425. return;
  1426. }
  1427. fc = le_to_host16(mgmt->frame_control);
  1428. stype = WLAN_FC_GET_STYPE(fc);
  1429. if (sig)
  1430. ssi_signal = (s32) nla_get_u32(sig);
  1431. os_memset(&event, 0, sizeof(event));
  1432. if (freq) {
  1433. event.rx_mgmt.freq = nla_get_u32(freq);
  1434. rx_freq = drv->last_mgmt_freq = event.rx_mgmt.freq;
  1435. }
  1436. wpa_printf(MSG_DEBUG,
  1437. "nl80211: RX frame freq=%d ssi_signal=%d stype=%u len=%u",
  1438. rx_freq, ssi_signal, stype, (unsigned int) len);
  1439. event.rx_mgmt.frame = frame;
  1440. event.rx_mgmt.frame_len = len;
  1441. event.rx_mgmt.ssi_signal = ssi_signal;
  1442. event.rx_mgmt.drv_priv = bss;
  1443. wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
  1444. }
  1445. static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
  1446. struct nlattr *cookie, const u8 *frame,
  1447. size_t len, struct nlattr *ack)
  1448. {
  1449. union wpa_event_data event;
  1450. const struct ieee80211_hdr *hdr;
  1451. u16 fc;
  1452. wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
  1453. if (!is_ap_interface(drv->nlmode)) {
  1454. u64 cookie_val;
  1455. if (!cookie)
  1456. return;
  1457. cookie_val = nla_get_u64(cookie);
  1458. wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
  1459. " cookie=0%llx%s (ack=%d)",
  1460. (long long unsigned int) cookie_val,
  1461. cookie_val == drv->send_action_cookie ?
  1462. " (match)" : " (unknown)", ack != NULL);
  1463. if (cookie_val != drv->send_action_cookie)
  1464. return;
  1465. }
  1466. hdr = (const struct ieee80211_hdr *) frame;
  1467. fc = le_to_host16(hdr->frame_control);
  1468. os_memset(&event, 0, sizeof(event));
  1469. event.tx_status.type = WLAN_FC_GET_TYPE(fc);
  1470. event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
  1471. event.tx_status.dst = hdr->addr1;
  1472. event.tx_status.data = frame;
  1473. event.tx_status.data_len = len;
  1474. event.tx_status.ack = ack != NULL;
  1475. wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
  1476. }
  1477. static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
  1478. enum wpa_event_type type,
  1479. const u8 *frame, size_t len)
  1480. {
  1481. const struct ieee80211_mgmt *mgmt;
  1482. union wpa_event_data event;
  1483. const u8 *bssid = NULL;
  1484. u16 reason_code = 0;
  1485. if (type == EVENT_DEAUTH)
  1486. wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
  1487. else
  1488. wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
  1489. mgmt = (const struct ieee80211_mgmt *) frame;
  1490. if (len >= 24) {
  1491. bssid = mgmt->bssid;
  1492. if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
  1493. !drv->associated &&
  1494. os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
  1495. os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
  1496. os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
  1497. /*
  1498. * Avoid issues with some roaming cases where
  1499. * disconnection event for the old AP may show up after
  1500. * we have started connection with the new AP.
  1501. */
  1502. wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
  1503. MAC2STR(bssid),
  1504. MAC2STR(drv->auth_attempt_bssid));
  1505. return;
  1506. }
  1507. if (drv->associated != 0 &&
  1508. os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
  1509. os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
  1510. /*
  1511. * We have presumably received this deauth as a
  1512. * response to a clear_state_mismatch() outgoing
  1513. * deauth. Don't let it take us offline!
  1514. */
  1515. wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
  1516. "from Unknown BSSID " MACSTR " -- ignoring",
  1517. MAC2STR(bssid));
  1518. return;
  1519. }
  1520. }
  1521. nl80211_mark_disconnected(drv);
  1522. os_memset(&event, 0, sizeof(event));
  1523. /* Note: Same offset for Reason Code in both frame subtypes */
  1524. if (len >= 24 + sizeof(mgmt->u.deauth))
  1525. reason_code = le_to_host16(mgmt->u.deauth.reason_code);
  1526. if (type == EVENT_DISASSOC) {
  1527. event.disassoc_info.locally_generated =
  1528. !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
  1529. event.disassoc_info.addr = bssid;
  1530. event.disassoc_info.reason_code = reason_code;
  1531. if (frame + len > mgmt->u.disassoc.variable) {
  1532. event.disassoc_info.ie = mgmt->u.disassoc.variable;
  1533. event.disassoc_info.ie_len = frame + len -
  1534. mgmt->u.disassoc.variable;
  1535. }
  1536. } else {
  1537. if (drv->ignore_deauth_event) {
  1538. wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event due to previous forced deauth-during-auth");
  1539. drv->ignore_deauth_event = 0;
  1540. return;
  1541. }
  1542. event.deauth_info.locally_generated =
  1543. !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
  1544. event.deauth_info.addr = bssid;
  1545. event.deauth_info.reason_code = reason_code;
  1546. if (frame + len > mgmt->u.deauth.variable) {
  1547. event.deauth_info.ie = mgmt->u.deauth.variable;
  1548. event.deauth_info.ie_len = frame + len -
  1549. mgmt->u.deauth.variable;
  1550. }
  1551. }
  1552. wpa_supplicant_event(drv->ctx, type, &event);
  1553. }
  1554. static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
  1555. enum wpa_event_type type,
  1556. const u8 *frame, size_t len)
  1557. {
  1558. const struct ieee80211_mgmt *mgmt;
  1559. union wpa_event_data event;
  1560. u16 reason_code = 0;
  1561. if (type == EVENT_UNPROT_DEAUTH)
  1562. wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
  1563. else
  1564. wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
  1565. if (len < 24)
  1566. return;
  1567. mgmt = (const struct ieee80211_mgmt *) frame;
  1568. os_memset(&event, 0, sizeof(event));
  1569. /* Note: Same offset for Reason Code in both frame subtypes */
  1570. if (len >= 24 + sizeof(mgmt->u.deauth))
  1571. reason_code = le_to_host16(mgmt->u.deauth.reason_code);
  1572. if (type == EVENT_UNPROT_DISASSOC) {
  1573. event.unprot_disassoc.sa = mgmt->sa;
  1574. event.unprot_disassoc.da = mgmt->da;
  1575. event.unprot_disassoc.reason_code = reason_code;
  1576. } else {
  1577. event.unprot_deauth.sa = mgmt->sa;
  1578. event.unprot_deauth.da = mgmt->da;
  1579. event.unprot_deauth.reason_code = reason_code;
  1580. }
  1581. wpa_supplicant_event(drv->ctx, type, &event);
  1582. }
  1583. static void mlme_event(struct i802_bss *bss,
  1584. enum nl80211_commands cmd, struct nlattr *frame,
  1585. struct nlattr *addr, struct nlattr *timed_out,
  1586. struct nlattr *freq, struct nlattr *ack,
  1587. struct nlattr *cookie, struct nlattr *sig)
  1588. {
  1589. struct wpa_driver_nl80211_data *drv = bss->drv;
  1590. const u8 *data;
  1591. size_t len;
  1592. if (timed_out && addr) {
  1593. mlme_timeout_event(drv, cmd, addr);
  1594. return;
  1595. }
  1596. if (frame == NULL) {
  1597. wpa_printf(MSG_DEBUG,
  1598. "nl80211: MLME event %d (%s) without frame data",
  1599. cmd, nl80211_command_to_string(cmd));
  1600. return;
  1601. }
  1602. data = nla_data(frame);
  1603. len = nla_len(frame);
  1604. if (len < 4 + 2 * ETH_ALEN) {
  1605. wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
  1606. MACSTR ") - too short",
  1607. cmd, nl80211_command_to_string(cmd), bss->ifname,
  1608. MAC2STR(bss->addr));
  1609. return;
  1610. }
  1611. wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
  1612. ") A1=" MACSTR " A2=" MACSTR, cmd,
  1613. nl80211_command_to_string(cmd), bss->ifname,
  1614. MAC2STR(bss->addr), MAC2STR(data + 4),
  1615. MAC2STR(data + 4 + ETH_ALEN));
  1616. if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
  1617. os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
  1618. os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
  1619. wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
  1620. "for foreign address", bss->ifname);
  1621. return;
  1622. }
  1623. wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
  1624. nla_data(frame), nla_len(frame));
  1625. switch (cmd) {
  1626. case NL80211_CMD_AUTHENTICATE:
  1627. mlme_event_auth(drv, nla_data(frame), nla_len(frame));
  1628. break;
  1629. case NL80211_CMD_ASSOCIATE:
  1630. mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
  1631. break;
  1632. case NL80211_CMD_DEAUTHENTICATE:
  1633. mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
  1634. nla_data(frame), nla_len(frame));
  1635. break;
  1636. case NL80211_CMD_DISASSOCIATE:
  1637. mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
  1638. nla_data(frame), nla_len(frame));
  1639. break;
  1640. case NL80211_CMD_FRAME:
  1641. mlme_event_mgmt(bss, freq, sig, nla_data(frame),
  1642. nla_len(frame));
  1643. break;
  1644. case NL80211_CMD_FRAME_TX_STATUS:
  1645. mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
  1646. nla_len(frame), ack);
  1647. break;
  1648. case NL80211_CMD_UNPROT_DEAUTHENTICATE:
  1649. mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
  1650. nla_data(frame), nla_len(frame));
  1651. break;
  1652. case NL80211_CMD_UNPROT_DISASSOCIATE:
  1653. mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
  1654. nla_data(frame), nla_len(frame));
  1655. break;
  1656. default:
  1657. break;
  1658. }
  1659. }
  1660. static void mlme_event_michael_mic_failure(struct i802_bss *bss,
  1661. struct nlattr *tb[])
  1662. {
  1663. union wpa_event_data data;
  1664. wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
  1665. os_memset(&data, 0, sizeof(data));
  1666. if (tb[NL80211_ATTR_MAC]) {
  1667. wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
  1668. nla_data(tb[NL80211_ATTR_MAC]),
  1669. nla_len(tb[NL80211_ATTR_MAC]));
  1670. data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
  1671. }
  1672. if (tb[NL80211_ATTR_KEY_SEQ]) {
  1673. wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
  1674. nla_data(tb[NL80211_ATTR_KEY_SEQ]),
  1675. nla_len(tb[NL80211_ATTR_KEY_SEQ]));
  1676. }
  1677. if (tb[NL80211_ATTR_KEY_TYPE]) {
  1678. enum nl80211_key_type key_type =
  1679. nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
  1680. wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
  1681. if (key_type == NL80211_KEYTYPE_PAIRWISE)
  1682. data.michael_mic_failure.unicast = 1;
  1683. } else
  1684. data.michael_mic_failure.unicast = 1;
  1685. if (tb[NL80211_ATTR_KEY_IDX]) {
  1686. u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
  1687. wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
  1688. }
  1689. wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
  1690. }
  1691. static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
  1692. struct nlattr *tb[])
  1693. {
  1694. if (tb[NL80211_ATTR_MAC] == NULL) {
  1695. wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
  1696. "event");
  1697. return;
  1698. }
  1699. os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
  1700. drv->associated = 1;
  1701. wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
  1702. MAC2STR(drv->bssid));
  1703. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
  1704. }
  1705. static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
  1706. int cancel_event, struct nlattr *tb[])
  1707. {
  1708. unsigned int freq, chan_type, duration;
  1709. union wpa_event_data data;
  1710. u64 cookie;
  1711. if (tb[NL80211_ATTR_WIPHY_FREQ])
  1712. freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
  1713. else
  1714. freq = 0;
  1715. if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
  1716. chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
  1717. else
  1718. chan_type = 0;
  1719. if (tb[NL80211_ATTR_DURATION])
  1720. duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
  1721. else
  1722. duration = 0;
  1723. if (tb[NL80211_ATTR_COOKIE])
  1724. cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
  1725. else
  1726. cookie = 0;
  1727. wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
  1728. "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
  1729. cancel_event, freq, chan_type, duration,
  1730. (long long unsigned int) cookie,
  1731. cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
  1732. if (cookie != drv->remain_on_chan_cookie)
  1733. return; /* not for us */
  1734. if (cancel_event)
  1735. drv->pending_remain_on_chan = 0;
  1736. os_memset(&data, 0, sizeof(data));
  1737. data.remain_on_channel.freq = freq;
  1738. data.remain_on_channel.duration = duration;
  1739. wpa_supplicant_event(drv->ctx, cancel_event ?
  1740. EVENT_CANCEL_REMAIN_ON_CHANNEL :
  1741. EVENT_REMAIN_ON_CHANNEL, &data);
  1742. }
  1743. static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
  1744. struct nlattr *tb[])
  1745. {
  1746. union wpa_event_data data;
  1747. os_memset(&data, 0, sizeof(data));
  1748. if (tb[NL80211_ATTR_IE]) {
  1749. data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
  1750. data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
  1751. }
  1752. if (tb[NL80211_ATTR_IE_RIC]) {
  1753. data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
  1754. data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
  1755. }
  1756. if (tb[NL80211_ATTR_MAC])
  1757. os_memcpy(data.ft_ies.target_ap,
  1758. nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
  1759. wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
  1760. MAC2STR(data.ft_ies.target_ap));
  1761. wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
  1762. }
  1763. static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
  1764. struct nlattr *tb[])
  1765. {
  1766. union wpa_event_data event;
  1767. struct nlattr *nl;
  1768. int rem;
  1769. struct scan_info *info;
  1770. #define MAX_REPORT_FREQS 50
  1771. int freqs[MAX_REPORT_FREQS];
  1772. int num_freqs = 0;
  1773. if (drv->scan_for_auth) {
  1774. drv->scan_for_auth = 0;
  1775. wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
  1776. "cfg80211 BSS entry");
  1777. wpa_driver_nl80211_authenticate_retry(drv);
  1778. return;
  1779. }
  1780. os_memset(&event, 0, sizeof(event));
  1781. info = &event.scan_info;
  1782. info->aborted = aborted;
  1783. if (tb[NL80211_ATTR_SCAN_SSIDS]) {
  1784. nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
  1785. struct wpa_driver_scan_ssid *s =
  1786. &info->ssids[info->num_ssids];
  1787. s->ssid = nla_data(nl);
  1788. s->ssid_len = nla_len(nl);
  1789. wpa_printf(MSG_DEBUG, "nl80211: Scan probed for SSID '%s'",
  1790. wpa_ssid_txt(s->ssid, s->ssid_len));
  1791. info->num_ssids++;
  1792. if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
  1793. break;
  1794. }
  1795. }
  1796. if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
  1797. char msg[200], *pos, *end;
  1798. int res;
  1799. pos = msg;
  1800. end = pos + sizeof(msg);
  1801. *pos = '\0';
  1802. nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
  1803. {
  1804. freqs[num_freqs] = nla_get_u32(nl);
  1805. res = os_snprintf(pos, end - pos, " %d",
  1806. freqs[num_freqs]);
  1807. if (res > 0 && end - pos > res)
  1808. pos += res;
  1809. num_freqs++;
  1810. if (num_freqs == MAX_REPORT_FREQS - 1)
  1811. break;
  1812. }
  1813. info->freqs = freqs;
  1814. info->num_freqs = num_freqs;
  1815. wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
  1816. msg);
  1817. }
  1818. wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
  1819. }
  1820. static int get_link_signal(struct nl_msg *msg, void *arg)
  1821. {
  1822. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1823. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1824. struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
  1825. static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
  1826. [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
  1827. [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
  1828. };
  1829. struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
  1830. static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
  1831. [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
  1832. [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
  1833. [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
  1834. [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
  1835. };
  1836. struct wpa_signal_info *sig_change = arg;
  1837. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1838. genlmsg_attrlen(gnlh, 0), NULL);
  1839. if (!tb[NL80211_ATTR_STA_INFO] ||
  1840. nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
  1841. tb[NL80211_ATTR_STA_INFO], policy))
  1842. return NL_SKIP;
  1843. if (!sinfo[NL80211_STA_INFO_SIGNAL])
  1844. return NL_SKIP;
  1845. sig_change->current_signal =
  1846. (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
  1847. if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
  1848. sig_change->avg_signal =
  1849. (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
  1850. else
  1851. sig_change->avg_signal = 0;
  1852. if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
  1853. if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
  1854. sinfo[NL80211_STA_INFO_TX_BITRATE],
  1855. rate_policy)) {
  1856. sig_change->current_txrate = 0;
  1857. } else {
  1858. if (rinfo[NL80211_RATE_INFO_BITRATE]) {
  1859. sig_change->current_txrate =
  1860. nla_get_u16(rinfo[
  1861. NL80211_RATE_INFO_BITRATE]) * 100;
  1862. }
  1863. }
  1864. }
  1865. return NL_SKIP;
  1866. }
  1867. static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
  1868. struct wpa_signal_info *sig)
  1869. {
  1870. struct nl_msg *msg;
  1871. sig->current_signal = -9999;
  1872. sig->current_txrate = 0;
  1873. msg = nlmsg_alloc();
  1874. if (!msg)
  1875. return -ENOMEM;
  1876. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
  1877. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1878. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
  1879. return send_and_recv_msgs(drv, msg, get_link_signal, sig);
  1880. nla_put_failure:
  1881. nlmsg_free(msg);
  1882. return -ENOBUFS;
  1883. }
  1884. static int get_link_noise(struct nl_msg *msg, void *arg)
  1885. {
  1886. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1887. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1888. struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
  1889. static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
  1890. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  1891. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  1892. };
  1893. struct wpa_signal_info *sig_change = arg;
  1894. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1895. genlmsg_attrlen(gnlh, 0), NULL);
  1896. if (!tb[NL80211_ATTR_SURVEY_INFO]) {
  1897. wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
  1898. return NL_SKIP;
  1899. }
  1900. if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
  1901. tb[NL80211_ATTR_SURVEY_INFO],
  1902. survey_policy)) {
  1903. wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
  1904. "attributes!");
  1905. return NL_SKIP;
  1906. }
  1907. if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
  1908. return NL_SKIP;
  1909. if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
  1910. sig_change->frequency)
  1911. return NL_SKIP;
  1912. if (!sinfo[NL80211_SURVEY_INFO_NOISE])
  1913. return NL_SKIP;
  1914. sig_change->current_noise =
  1915. (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
  1916. return NL_SKIP;
  1917. }
  1918. static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
  1919. struct wpa_signal_info *sig_change)
  1920. {
  1921. struct nl_msg *msg;
  1922. sig_change->current_noise = 9999;
  1923. sig_change->frequency = drv->assoc_freq;
  1924. msg = nlmsg_alloc();
  1925. if (!msg)
  1926. return -ENOMEM;
  1927. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
  1928. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1929. return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
  1930. nla_put_failure:
  1931. nlmsg_free(msg);
  1932. return -ENOBUFS;
  1933. }
  1934. static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
  1935. {
  1936. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1937. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1938. struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
  1939. static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
  1940. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  1941. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  1942. };
  1943. struct wpa_scan_results *scan_results = arg;
  1944. struct wpa_scan_res *scan_res;
  1945. size_t i;
  1946. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1947. genlmsg_attrlen(gnlh, 0), NULL);
  1948. if (!tb[NL80211_ATTR_SURVEY_INFO]) {
  1949. wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
  1950. return NL_SKIP;
  1951. }
  1952. if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
  1953. tb[NL80211_ATTR_SURVEY_INFO],
  1954. survey_policy)) {
  1955. wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
  1956. "attributes");
  1957. return NL_SKIP;
  1958. }
  1959. if (!sinfo[NL80211_SURVEY_INFO_NOISE])
  1960. return NL_SKIP;
  1961. if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
  1962. return NL_SKIP;
  1963. for (i = 0; i < scan_results->num; ++i) {
  1964. scan_res = scan_results->res[i];
  1965. if (!scan_res)
  1966. continue;
  1967. if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
  1968. scan_res->freq)
  1969. continue;
  1970. if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
  1971. continue;
  1972. scan_res->noise = (s8)
  1973. nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
  1974. scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
  1975. }
  1976. return NL_SKIP;
  1977. }
  1978. static int nl80211_get_noise_for_scan_results(
  1979. struct wpa_driver_nl80211_data *drv,
  1980. struct wpa_scan_results *scan_res)
  1981. {
  1982. struct nl_msg *msg;
  1983. msg = nlmsg_alloc();
  1984. if (!msg)
  1985. return -ENOMEM;
  1986. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
  1987. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1988. return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
  1989. scan_res);
  1990. nla_put_failure:
  1991. nlmsg_free(msg);
  1992. return -ENOBUFS;
  1993. }
  1994. static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
  1995. struct nlattr *tb[])
  1996. {
  1997. static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
  1998. [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
  1999. [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
  2000. [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
  2001. [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
  2002. };
  2003. struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
  2004. enum nl80211_cqm_rssi_threshold_event event;
  2005. union wpa_event_data ed;
  2006. struct wpa_signal_info sig;
  2007. int res;
  2008. if (tb[NL80211_ATTR_CQM] == NULL ||
  2009. nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
  2010. cqm_policy)) {
  2011. wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
  2012. return;
  2013. }
  2014. os_memset(&ed, 0, sizeof(ed));
  2015. if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
  2016. if (!tb[NL80211_ATTR_MAC])
  2017. return;
  2018. os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
  2019. ETH_ALEN);
  2020. wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
  2021. return;
  2022. }
  2023. if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
  2024. return;
  2025. event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
  2026. if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
  2027. wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
  2028. "event: RSSI high");
  2029. ed.signal_change.above_threshold = 1;
  2030. } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
  2031. wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
  2032. "event: RSSI low");
  2033. ed.signal_change.above_threshold = 0;
  2034. } else
  2035. return;
  2036. res = nl80211_get_link_signal(drv, &sig);
  2037. if (res == 0) {
  2038. ed.signal_change.current_signal = sig.current_signal;
  2039. ed.signal_change.current_txrate = sig.current_txrate;
  2040. wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
  2041. sig.current_signal, sig.current_txrate);
  2042. }
  2043. res = nl80211_get_link_noise(drv, &sig);
  2044. if (res == 0) {
  2045. ed.signal_change.current_noise = sig.current_noise;
  2046. wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
  2047. sig.current_noise);
  2048. }
  2049. wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
  2050. }
  2051. static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
  2052. struct nlattr **tb)
  2053. {
  2054. u8 *addr;
  2055. union wpa_event_data data;
  2056. if (tb[NL80211_ATTR_MAC] == NULL)
  2057. return;
  2058. addr = nla_data(tb[NL80211_ATTR_MAC]);
  2059. wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
  2060. if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
  2061. u8 *ies = NULL;
  2062. size_t ies_len = 0;
  2063. if (tb[NL80211_ATTR_IE]) {
  2064. ies = nla_data(tb[NL80211_ATTR_IE]);
  2065. ies_len = nla_len(tb[NL80211_ATTR_IE]);
  2066. }
  2067. wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
  2068. drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
  2069. return;
  2070. }
  2071. if (drv->nlmode != NL80211_IFTYPE_ADHOC)
  2072. return;
  2073. os_memset(&data, 0, sizeof(data));
  2074. os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
  2075. wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
  2076. }
  2077. static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
  2078. struct nlattr **tb)
  2079. {
  2080. u8 *addr;
  2081. union wpa_event_data data;
  2082. if (tb[NL80211_ATTR_MAC] == NULL)
  2083. return;
  2084. addr = nla_data(tb[NL80211_ATTR_MAC]);
  2085. wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
  2086. MAC2STR(addr));
  2087. if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
  2088. drv_event_disassoc(drv->ctx, addr);
  2089. return;
  2090. }
  2091. if (drv->nlmode != NL80211_IFTYPE_ADHOC)
  2092. return;
  2093. os_memset(&data, 0, sizeof(data));
  2094. os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
  2095. wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
  2096. }
  2097. static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
  2098. struct nlattr **tb)
  2099. {
  2100. struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
  2101. static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
  2102. [NL80211_REKEY_DATA_KEK] = {
  2103. .minlen = NL80211_KEK_LEN,
  2104. .maxlen = NL80211_KEK_LEN,
  2105. },
  2106. [NL80211_REKEY_DATA_KCK] = {
  2107. .minlen = NL80211_KCK_LEN,
  2108. .maxlen = NL80211_KCK_LEN,
  2109. },
  2110. [NL80211_REKEY_DATA_REPLAY_CTR] = {
  2111. .minlen = NL80211_REPLAY_CTR_LEN,
  2112. .maxlen = NL80211_REPLAY_CTR_LEN,
  2113. },
  2114. };
  2115. union wpa_event_data data;
  2116. if (!tb[NL80211_ATTR_MAC])
  2117. return;
  2118. if (!tb[NL80211_ATTR_REKEY_DATA])
  2119. return;
  2120. if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
  2121. tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
  2122. return;
  2123. if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
  2124. return;
  2125. os_memset(&data, 0, sizeof(data));
  2126. data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
  2127. wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
  2128. MAC2STR(data.driver_gtk_rekey.bssid));
  2129. data.driver_gtk_rekey.replay_ctr =
  2130. nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
  2131. wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
  2132. data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
  2133. wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
  2134. }
  2135. static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
  2136. struct nlattr **tb)
  2137. {
  2138. struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
  2139. static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
  2140. [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
  2141. [NL80211_PMKSA_CANDIDATE_BSSID] = {
  2142. .minlen = ETH_ALEN,
  2143. .maxlen = ETH_ALEN,
  2144. },
  2145. [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
  2146. };
  2147. union wpa_event_data data;
  2148. wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
  2149. if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
  2150. return;
  2151. if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
  2152. tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
  2153. return;
  2154. if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
  2155. !cand[NL80211_PMKSA_CANDIDATE_BSSID])
  2156. return;
  2157. os_memset(&data, 0, sizeof(data));
  2158. os_memcpy(data.pmkid_candidate.bssid,
  2159. nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
  2160. data.pmkid_candidate.index =
  2161. nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
  2162. data.pmkid_candidate.preauth =
  2163. cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
  2164. wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
  2165. }
  2166. static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
  2167. struct nlattr **tb)
  2168. {
  2169. union wpa_event_data data;
  2170. wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
  2171. if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
  2172. return;
  2173. os_memset(&data, 0, sizeof(data));
  2174. os_memcpy(data.client_poll.addr,
  2175. nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
  2176. wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
  2177. }
  2178. static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
  2179. struct nlattr **tb)
  2180. {
  2181. union wpa_event_data data;
  2182. wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
  2183. if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
  2184. return;
  2185. os_memset(&data, 0, sizeof(data));
  2186. os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
  2187. switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
  2188. case NL80211_TDLS_SETUP:
  2189. wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
  2190. MACSTR, MAC2STR(data.tdls.peer));
  2191. data.tdls.oper = TDLS_REQUEST_SETUP;
  2192. break;
  2193. case NL80211_TDLS_TEARDOWN:
  2194. wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
  2195. MACSTR, MAC2STR(data.tdls.peer));
  2196. data.tdls.oper = TDLS_REQUEST_TEARDOWN;
  2197. break;
  2198. default:
  2199. wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
  2200. "event");
  2201. return;
  2202. }
  2203. if (tb[NL80211_ATTR_REASON_CODE]) {
  2204. data.tdls.reason_code =
  2205. nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
  2206. }
  2207. wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
  2208. }
  2209. static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
  2210. struct nlattr **tb)
  2211. {
  2212. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
  2213. }
  2214. static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
  2215. struct nlattr **tb)
  2216. {
  2217. union wpa_event_data data;
  2218. u32 reason;
  2219. wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
  2220. if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
  2221. return;
  2222. os_memset(&data, 0, sizeof(data));
  2223. os_memcpy(data.connect_failed_reason.addr,
  2224. nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
  2225. reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
  2226. switch (reason) {
  2227. case NL80211_CONN_FAIL_MAX_CLIENTS:
  2228. wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
  2229. data.connect_failed_reason.code = MAX_CLIENT_REACHED;
  2230. break;
  2231. case NL80211_CONN_FAIL_BLOCKED_CLIENT:
  2232. wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
  2233. " tried to connect",
  2234. MAC2STR(data.connect_failed_reason.addr));
  2235. data.connect_failed_reason.code = BLOCKED_CLIENT;
  2236. break;
  2237. default:
  2238. wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
  2239. "%u", reason);
  2240. return;
  2241. }
  2242. wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
  2243. }
  2244. static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
  2245. struct nlattr **tb)
  2246. {
  2247. union wpa_event_data data;
  2248. enum nl80211_radar_event event_type;
  2249. if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
  2250. return;
  2251. os_memset(&data, 0, sizeof(data));
  2252. data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
  2253. event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
  2254. /* Check HT params */
  2255. if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
  2256. data.dfs_event.ht_enabled = 1;
  2257. data.dfs_event.chan_offset = 0;
  2258. switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
  2259. case NL80211_CHAN_NO_HT:
  2260. data.dfs_event.ht_enabled = 0;
  2261. break;
  2262. case NL80211_CHAN_HT20:
  2263. break;
  2264. case NL80211_CHAN_HT40PLUS:
  2265. data.dfs_event.chan_offset = 1;
  2266. break;
  2267. case NL80211_CHAN_HT40MINUS:
  2268. data.dfs_event.chan_offset = -1;
  2269. break;
  2270. }
  2271. }
  2272. /* Get VHT params */
  2273. if (tb[NL80211_ATTR_CHANNEL_WIDTH])
  2274. data.dfs_event.chan_width =
  2275. convert2width(nla_get_u32(
  2276. tb[NL80211_ATTR_CHANNEL_WIDTH]));
  2277. if (tb[NL80211_ATTR_CENTER_FREQ1])
  2278. data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
  2279. if (tb[NL80211_ATTR_CENTER_FREQ2])
  2280. data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
  2281. wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
  2282. data.dfs_event.freq, data.dfs_event.ht_enabled,
  2283. data.dfs_event.chan_offset, data.dfs_event.chan_width,
  2284. data.dfs_event.cf1, data.dfs_event.cf2);
  2285. switch (event_type) {
  2286. case NL80211_RADAR_DETECTED:
  2287. wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
  2288. break;
  2289. case NL80211_RADAR_CAC_FINISHED:
  2290. wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
  2291. break;
  2292. case NL80211_RADAR_CAC_ABORTED:
  2293. wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
  2294. break;
  2295. case NL80211_RADAR_NOP_FINISHED:
  2296. wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
  2297. break;
  2298. default:
  2299. wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
  2300. "received", event_type);
  2301. break;
  2302. }
  2303. }
  2304. static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
  2305. int wds)
  2306. {
  2307. struct wpa_driver_nl80211_data *drv = bss->drv;
  2308. union wpa_event_data event;
  2309. if (!tb[NL80211_ATTR_MAC])
  2310. return;
  2311. os_memset(&event, 0, sizeof(event));
  2312. event.rx_from_unknown.bssid = bss->addr;
  2313. event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
  2314. event.rx_from_unknown.wds = wds;
  2315. wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
  2316. }
  2317. static void qca_nl80211_avoid_freq(struct wpa_driver_nl80211_data *drv,
  2318. const u8 *data, size_t len)
  2319. {
  2320. u32 i, count;
  2321. union wpa_event_data event;
  2322. struct wpa_freq_range *range = NULL;
  2323. const struct qca_avoid_freq_list *freq_range;
  2324. freq_range = (const struct qca_avoid_freq_list *) data;
  2325. if (len < sizeof(freq_range->count))
  2326. return;
  2327. count = freq_range->count;
  2328. if (len < sizeof(freq_range->count) +
  2329. count * sizeof(struct qca_avoid_freq_range)) {
  2330. wpa_printf(MSG_DEBUG, "nl80211: Ignored too short avoid frequency list (len=%u)",
  2331. (unsigned int) len);
  2332. return;
  2333. }
  2334. if (count > 0) {
  2335. range = os_calloc(count, sizeof(struct wpa_freq_range));
  2336. if (range == NULL)
  2337. return;
  2338. }
  2339. os_memset(&event, 0, sizeof(event));
  2340. for (i = 0; i < count; i++) {
  2341. unsigned int idx = event.freq_range.num;
  2342. range[idx].min = freq_range->range[i].start_freq;
  2343. range[idx].max = freq_range->range[i].end_freq;
  2344. wpa_printf(MSG_DEBUG, "nl80211: Avoid frequency range: %u-%u",
  2345. range[idx].min, range[idx].max);
  2346. if (range[idx].min > range[idx].max) {
  2347. wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid frequency range");
  2348. continue;
  2349. }
  2350. event.freq_range.num++;
  2351. }
  2352. event.freq_range.range = range;
  2353. wpa_supplicant_event(drv->ctx, EVENT_AVOID_FREQUENCIES, &event);
  2354. os_free(range);
  2355. }
  2356. static void nl80211_vendor_event_qca(struct wpa_driver_nl80211_data *drv,
  2357. u32 subcmd, u8 *data, size_t len)
  2358. {
  2359. switch (subcmd) {
  2360. case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
  2361. qca_nl80211_avoid_freq(drv, data, len);
  2362. break;
  2363. default:
  2364. wpa_printf(MSG_DEBUG,
  2365. "nl80211: Ignore unsupported QCA vendor event %u",
  2366. subcmd);
  2367. break;
  2368. }
  2369. }
  2370. static void nl80211_vendor_event(struct wpa_driver_nl80211_data *drv,
  2371. struct nlattr **tb)
  2372. {
  2373. u32 vendor_id, subcmd, wiphy = 0;
  2374. int wiphy_idx;
  2375. u8 *data = NULL;
  2376. size_t len = 0;
  2377. if (!tb[NL80211_ATTR_VENDOR_ID] ||
  2378. !tb[NL80211_ATTR_VENDOR_SUBCMD])
  2379. return;
  2380. vendor_id = nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]);
  2381. subcmd = nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]);
  2382. if (tb[NL80211_ATTR_WIPHY])
  2383. wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
  2384. wpa_printf(MSG_DEBUG, "nl80211: Vendor event: wiphy=%u vendor_id=0x%x subcmd=%u",
  2385. wiphy, vendor_id, subcmd);
  2386. if (tb[NL80211_ATTR_VENDOR_DATA]) {
  2387. data = nla_data(tb[NL80211_ATTR_VENDOR_DATA]);
  2388. len = nla_len(tb[NL80211_ATTR_VENDOR_DATA]);
  2389. wpa_hexdump(MSG_MSGDUMP, "nl80211: Vendor data", data, len);
  2390. }
  2391. wiphy_idx = nl80211_get_wiphy_index(drv->first_bss);
  2392. if (wiphy_idx >= 0 && wiphy_idx != (int) wiphy) {
  2393. wpa_printf(MSG_DEBUG, "nl80211: Ignore vendor event for foreign wiphy %u (own: %d)",
  2394. wiphy, wiphy_idx);
  2395. return;
  2396. }
  2397. switch (vendor_id) {
  2398. case OUI_QCA:
  2399. nl80211_vendor_event_qca(drv, subcmd, data, len);
  2400. break;
  2401. default:
  2402. wpa_printf(MSG_DEBUG, "nl80211: Ignore unsupported vendor event");
  2403. break;
  2404. }
  2405. }
  2406. static void do_process_drv_event(struct i802_bss *bss, int cmd,
  2407. struct nlattr **tb)
  2408. {
  2409. struct wpa_driver_nl80211_data *drv = bss->drv;
  2410. union wpa_event_data data;
  2411. wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
  2412. cmd, nl80211_command_to_string(cmd), bss->ifname);
  2413. if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
  2414. (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
  2415. cmd == NL80211_CMD_SCAN_ABORTED)) {
  2416. wpa_driver_nl80211_set_mode(drv->first_bss,
  2417. drv->ap_scan_as_station);
  2418. drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
  2419. }
  2420. switch (cmd) {
  2421. case NL80211_CMD_TRIGGER_SCAN:
  2422. wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
  2423. drv->scan_state = SCAN_STARTED;
  2424. if (drv->scan_for_auth) {
  2425. /*
  2426. * Cannot indicate EVENT_SCAN_STARTED here since we skip
  2427. * EVENT_SCAN_RESULTS in scan_for_auth case and the
  2428. * upper layer implementation could get confused about
  2429. * scanning state.
  2430. */
  2431. wpa_printf(MSG_DEBUG, "nl80211: Do not indicate scan-start event due to internal scan_for_auth");
  2432. break;
  2433. }
  2434. wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
  2435. break;
  2436. case NL80211_CMD_START_SCHED_SCAN:
  2437. wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
  2438. drv->scan_state = SCHED_SCAN_STARTED;
  2439. break;
  2440. case NL80211_CMD_SCHED_SCAN_STOPPED:
  2441. wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
  2442. drv->scan_state = SCHED_SCAN_STOPPED;
  2443. wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
  2444. break;
  2445. case NL80211_CMD_NEW_SCAN_RESULTS:
  2446. wpa_dbg(drv->ctx, MSG_DEBUG,
  2447. "nl80211: New scan results available");
  2448. drv->scan_state = SCAN_COMPLETED;
  2449. drv->scan_complete_events = 1;
  2450. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
  2451. drv->ctx);
  2452. send_scan_event(drv, 0, tb);
  2453. break;
  2454. case NL80211_CMD_SCHED_SCAN_RESULTS:
  2455. wpa_dbg(drv->ctx, MSG_DEBUG,
  2456. "nl80211: New sched scan results available");
  2457. drv->scan_state = SCHED_SCAN_RESULTS;
  2458. send_scan_event(drv, 0, tb);
  2459. break;
  2460. case NL80211_CMD_SCAN_ABORTED:
  2461. wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
  2462. drv->scan_state = SCAN_ABORTED;
  2463. /*
  2464. * Need to indicate that scan results are available in order
  2465. * not to make wpa_supplicant stop its scanning.
  2466. */
  2467. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
  2468. drv->ctx);
  2469. send_scan_event(drv, 1, tb);
  2470. break;
  2471. case NL80211_CMD_AUTHENTICATE:
  2472. case NL80211_CMD_ASSOCIATE:
  2473. case NL80211_CMD_DEAUTHENTICATE:
  2474. case NL80211_CMD_DISASSOCIATE:
  2475. case NL80211_CMD_FRAME_TX_STATUS:
  2476. case NL80211_CMD_UNPROT_DEAUTHENTICATE:
  2477. case NL80211_CMD_UNPROT_DISASSOCIATE:
  2478. mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
  2479. tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
  2480. tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
  2481. tb[NL80211_ATTR_COOKIE],
  2482. tb[NL80211_ATTR_RX_SIGNAL_DBM]);
  2483. break;
  2484. case NL80211_CMD_CONNECT:
  2485. case NL80211_CMD_ROAM:
  2486. mlme_event_connect(drv, cmd,
  2487. tb[NL80211_ATTR_STATUS_CODE],
  2488. tb[NL80211_ATTR_MAC],
  2489. tb[NL80211_ATTR_REQ_IE],
  2490. tb[NL80211_ATTR_RESP_IE]);
  2491. break;
  2492. case NL80211_CMD_CH_SWITCH_NOTIFY:
  2493. mlme_event_ch_switch(drv,
  2494. tb[NL80211_ATTR_IFINDEX],
  2495. tb[NL80211_ATTR_WIPHY_FREQ],
  2496. tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
  2497. tb[NL80211_ATTR_CHANNEL_WIDTH],
  2498. tb[NL80211_ATTR_CENTER_FREQ1],
  2499. tb[NL80211_ATTR_CENTER_FREQ2]);
  2500. break;
  2501. case NL80211_CMD_DISCONNECT:
  2502. mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
  2503. tb[NL80211_ATTR_MAC],
  2504. tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
  2505. break;
  2506. case NL80211_CMD_MICHAEL_MIC_FAILURE:
  2507. mlme_event_michael_mic_failure(bss, tb);
  2508. break;
  2509. case NL80211_CMD_JOIN_IBSS:
  2510. mlme_event_join_ibss(drv, tb);
  2511. break;
  2512. case NL80211_CMD_REMAIN_ON_CHANNEL:
  2513. mlme_event_remain_on_channel(drv, 0, tb);
  2514. break;
  2515. case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
  2516. mlme_event_remain_on_channel(drv, 1, tb);
  2517. break;
  2518. case NL80211_CMD_NOTIFY_CQM:
  2519. nl80211_cqm_event(drv, tb);
  2520. break;
  2521. case NL80211_CMD_REG_CHANGE:
  2522. wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
  2523. if (tb[NL80211_ATTR_REG_INITIATOR] == NULL)
  2524. break;
  2525. os_memset(&data, 0, sizeof(data));
  2526. switch (nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])) {
  2527. case NL80211_REGDOM_SET_BY_CORE:
  2528. data.channel_list_changed.initiator =
  2529. REGDOM_SET_BY_CORE;
  2530. break;
  2531. case NL80211_REGDOM_SET_BY_USER:
  2532. data.channel_list_changed.initiator =
  2533. REGDOM_SET_BY_USER;
  2534. break;
  2535. case NL80211_REGDOM_SET_BY_DRIVER:
  2536. data.channel_list_changed.initiator =
  2537. REGDOM_SET_BY_DRIVER;
  2538. break;
  2539. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  2540. data.channel_list_changed.initiator =
  2541. REGDOM_SET_BY_COUNTRY_IE;
  2542. break;
  2543. default:
  2544. wpa_printf(MSG_DEBUG, "nl80211: Unknown reg change initiator %d received",
  2545. nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]));
  2546. break;
  2547. }
  2548. wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
  2549. &data);
  2550. break;
  2551. case NL80211_CMD_REG_BEACON_HINT:
  2552. wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
  2553. os_memset(&data, 0, sizeof(data));
  2554. data.channel_list_changed.initiator = REGDOM_BEACON_HINT;
  2555. wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
  2556. &data);
  2557. break;
  2558. case NL80211_CMD_NEW_STATION:
  2559. nl80211_new_station_event(drv, tb);
  2560. break;
  2561. case NL80211_CMD_DEL_STATION:
  2562. nl80211_del_station_event(drv, tb);
  2563. break;
  2564. case NL80211_CMD_SET_REKEY_OFFLOAD:
  2565. nl80211_rekey_offload_event(drv, tb);
  2566. break;
  2567. case NL80211_CMD_PMKSA_CANDIDATE:
  2568. nl80211_pmksa_candidate_event(drv, tb);
  2569. break;
  2570. case NL80211_CMD_PROBE_CLIENT:
  2571. nl80211_client_probe_event(drv, tb);
  2572. break;
  2573. case NL80211_CMD_TDLS_OPER:
  2574. nl80211_tdls_oper_event(drv, tb);
  2575. break;
  2576. case NL80211_CMD_CONN_FAILED:
  2577. nl80211_connect_failed_event(drv, tb);
  2578. break;
  2579. case NL80211_CMD_FT_EVENT:
  2580. mlme_event_ft_event(drv, tb);
  2581. break;
  2582. case NL80211_CMD_RADAR_DETECT:
  2583. nl80211_radar_event(drv, tb);
  2584. break;
  2585. case NL80211_CMD_STOP_AP:
  2586. nl80211_stop_ap(drv, tb);
  2587. break;
  2588. case NL80211_CMD_VENDOR:
  2589. nl80211_vendor_event(drv, tb);
  2590. break;
  2591. default:
  2592. wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
  2593. "(cmd=%d)", cmd);
  2594. break;
  2595. }
  2596. }
  2597. static int process_drv_event(struct nl_msg *msg, void *arg)
  2598. {
  2599. struct wpa_driver_nl80211_data *drv = arg;
  2600. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  2601. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  2602. struct i802_bss *bss;
  2603. int ifidx = -1;
  2604. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  2605. genlmsg_attrlen(gnlh, 0), NULL);
  2606. if (tb[NL80211_ATTR_IFINDEX]) {
  2607. ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
  2608. for (bss = drv->first_bss; bss; bss = bss->next)
  2609. if (ifidx == -1 || ifidx == bss->ifindex) {
  2610. do_process_drv_event(bss, gnlh->cmd, tb);
  2611. return NL_SKIP;
  2612. }
  2613. wpa_printf(MSG_DEBUG,
  2614. "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
  2615. gnlh->cmd, ifidx);
  2616. } else if (tb[NL80211_ATTR_WDEV]) {
  2617. u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
  2618. wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
  2619. for (bss = drv->first_bss; bss; bss = bss->next) {
  2620. if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
  2621. do_process_drv_event(bss, gnlh->cmd, tb);
  2622. return NL_SKIP;
  2623. }
  2624. }
  2625. wpa_printf(MSG_DEBUG,
  2626. "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
  2627. gnlh->cmd, (long long unsigned int) wdev_id);
  2628. }
  2629. return NL_SKIP;
  2630. }
  2631. static int process_global_event(struct nl_msg *msg, void *arg)
  2632. {
  2633. struct nl80211_global *global = arg;
  2634. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  2635. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  2636. struct wpa_driver_nl80211_data *drv, *tmp;
  2637. int ifidx = -1;
  2638. struct i802_bss *bss;
  2639. u64 wdev_id = 0;
  2640. int wdev_id_set = 0;
  2641. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  2642. genlmsg_attrlen(gnlh, 0), NULL);
  2643. if (tb[NL80211_ATTR_IFINDEX])
  2644. ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
  2645. else if (tb[NL80211_ATTR_WDEV]) {
  2646. wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
  2647. wdev_id_set = 1;
  2648. }
  2649. dl_list_for_each_safe(drv, tmp, &global->interfaces,
  2650. struct wpa_driver_nl80211_data, list) {
  2651. for (bss = drv->first_bss; bss; bss = bss->next) {
  2652. if ((ifidx == -1 && !wdev_id_set) ||
  2653. ifidx == bss->ifindex ||
  2654. (wdev_id_set && bss->wdev_id_set &&
  2655. wdev_id == bss->wdev_id)) {
  2656. do_process_drv_event(bss, gnlh->cmd, tb);
  2657. return NL_SKIP;
  2658. }
  2659. }
  2660. }
  2661. return NL_SKIP;
  2662. }
  2663. static int process_bss_event(struct nl_msg *msg, void *arg)
  2664. {
  2665. struct i802_bss *bss = arg;
  2666. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  2667. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  2668. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  2669. genlmsg_attrlen(gnlh, 0), NULL);
  2670. wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
  2671. gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
  2672. bss->ifname);
  2673. switch (gnlh->cmd) {
  2674. case NL80211_CMD_FRAME:
  2675. case NL80211_CMD_FRAME_TX_STATUS:
  2676. mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
  2677. tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
  2678. tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
  2679. tb[NL80211_ATTR_COOKIE],
  2680. tb[NL80211_ATTR_RX_SIGNAL_DBM]);
  2681. break;
  2682. case NL80211_CMD_UNEXPECTED_FRAME:
  2683. nl80211_spurious_frame(bss, tb, 0);
  2684. break;
  2685. case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
  2686. nl80211_spurious_frame(bss, tb, 1);
  2687. break;
  2688. default:
  2689. wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
  2690. "(cmd=%d)", gnlh->cmd);
  2691. break;
  2692. }
  2693. return NL_SKIP;
  2694. }
  2695. static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
  2696. void *handle)
  2697. {
  2698. struct nl_cb *cb = eloop_ctx;
  2699. int res;
  2700. wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
  2701. res = nl_recvmsgs(handle, cb);
  2702. if (res) {
  2703. wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
  2704. __func__, res);
  2705. }
  2706. }
  2707. /**
  2708. * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
  2709. * @priv: driver_nl80211 private data
  2710. * @alpha2_arg: country to which to switch to
  2711. * Returns: 0 on success, -1 on failure
  2712. *
  2713. * This asks nl80211 to set the regulatory domain for given
  2714. * country ISO / IEC alpha2.
  2715. */
  2716. static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
  2717. {
  2718. struct i802_bss *bss = priv;
  2719. struct wpa_driver_nl80211_data *drv = bss->drv;
  2720. char alpha2[3];
  2721. struct nl_msg *msg;
  2722. msg = nlmsg_alloc();
  2723. if (!msg)
  2724. return -ENOMEM;
  2725. alpha2[0] = alpha2_arg[0];
  2726. alpha2[1] = alpha2_arg[1];
  2727. alpha2[2] = '\0';
  2728. nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
  2729. NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
  2730. if (send_and_recv_msgs(drv, msg, NULL, NULL))
  2731. return -EINVAL;
  2732. return 0;
  2733. nla_put_failure:
  2734. nlmsg_free(msg);
  2735. return -EINVAL;
  2736. }
  2737. static int nl80211_get_country(struct nl_msg *msg, void *arg)
  2738. {
  2739. char *alpha2 = arg;
  2740. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  2741. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  2742. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  2743. genlmsg_attrlen(gnlh, 0), NULL);
  2744. if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
  2745. wpa_printf(MSG_DEBUG, "nl80211: No country information available");
  2746. return NL_SKIP;
  2747. }
  2748. os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
  2749. return NL_SKIP;
  2750. }
  2751. static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
  2752. {
  2753. struct i802_bss *bss = priv;
  2754. struct wpa_driver_nl80211_data *drv = bss->drv;
  2755. struct nl_msg *msg;
  2756. int ret;
  2757. msg = nlmsg_alloc();
  2758. if (!msg)
  2759. return -ENOMEM;
  2760. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
  2761. alpha2[0] = '\0';
  2762. ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
  2763. if (!alpha2[0])
  2764. ret = -1;
  2765. return ret;
  2766. }
  2767. static int protocol_feature_handler(struct nl_msg *msg, void *arg)
  2768. {
  2769. u32 *feat = arg;
  2770. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  2771. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  2772. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  2773. genlmsg_attrlen(gnlh, 0), NULL);
  2774. if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
  2775. *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
  2776. return NL_SKIP;
  2777. }
  2778. static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
  2779. {
  2780. u32 feat = 0;
  2781. struct nl_msg *msg;
  2782. msg = nlmsg_alloc();
  2783. if (!msg)
  2784. goto nla_put_failure;
  2785. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
  2786. if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
  2787. return feat;
  2788. msg = NULL;
  2789. nla_put_failure:
  2790. nlmsg_free(msg);
  2791. return 0;
  2792. }
  2793. struct wiphy_info_data {
  2794. struct wpa_driver_nl80211_data *drv;
  2795. struct wpa_driver_capa *capa;
  2796. unsigned int num_multichan_concurrent;
  2797. unsigned int error:1;
  2798. unsigned int device_ap_sme:1;
  2799. unsigned int poll_command_supported:1;
  2800. unsigned int data_tx_status:1;
  2801. unsigned int monitor_supported:1;
  2802. unsigned int auth_supported:1;
  2803. unsigned int connect_supported:1;
  2804. unsigned int p2p_go_supported:1;
  2805. unsigned int p2p_client_supported:1;
  2806. unsigned int p2p_concurrent:1;
  2807. unsigned int channel_switch_supported:1;
  2808. unsigned int set_qos_map_supported:1;
  2809. };
  2810. static unsigned int probe_resp_offload_support(int supp_protocols)
  2811. {
  2812. unsigned int prot = 0;
  2813. if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
  2814. prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
  2815. if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
  2816. prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
  2817. if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
  2818. prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
  2819. if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
  2820. prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
  2821. return prot;
  2822. }
  2823. static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
  2824. struct nlattr *tb)
  2825. {
  2826. struct nlattr *nl_mode;
  2827. int i;
  2828. if (tb == NULL)
  2829. return;
  2830. nla_for_each_nested(nl_mode, tb, i) {
  2831. switch (nla_type(nl_mode)) {
  2832. case NL80211_IFTYPE_AP:
  2833. info->capa->flags |= WPA_DRIVER_FLAGS_AP;
  2834. break;
  2835. case NL80211_IFTYPE_ADHOC:
  2836. info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
  2837. break;
  2838. case NL80211_IFTYPE_P2P_DEVICE:
  2839. info->capa->flags |=
  2840. WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
  2841. break;
  2842. case NL80211_IFTYPE_P2P_GO:
  2843. info->p2p_go_supported = 1;
  2844. break;
  2845. case NL80211_IFTYPE_P2P_CLIENT:
  2846. info->p2p_client_supported = 1;
  2847. break;
  2848. case NL80211_IFTYPE_MONITOR:
  2849. info->monitor_supported = 1;
  2850. break;
  2851. }
  2852. }
  2853. }
  2854. static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
  2855. struct nlattr *nl_combi)
  2856. {
  2857. struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
  2858. struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
  2859. struct nlattr *nl_limit, *nl_mode;
  2860. int err, rem_limit, rem_mode;
  2861. int combination_has_p2p = 0, combination_has_mgd = 0;
  2862. static struct nla_policy
  2863. iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
  2864. [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
  2865. [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
  2866. [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
  2867. [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
  2868. [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
  2869. },
  2870. iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
  2871. [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
  2872. [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
  2873. };
  2874. err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
  2875. nl_combi, iface_combination_policy);
  2876. if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
  2877. !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
  2878. !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
  2879. return 0; /* broken combination */
  2880. if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
  2881. info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
  2882. nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
  2883. rem_limit) {
  2884. err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
  2885. nl_limit, iface_limit_policy);
  2886. if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
  2887. return 0; /* broken combination */
  2888. nla_for_each_nested(nl_mode,
  2889. tb_limit[NL80211_IFACE_LIMIT_TYPES],
  2890. rem_mode) {
  2891. int ift = nla_type(nl_mode);
  2892. if (ift == NL80211_IFTYPE_P2P_GO ||
  2893. ift == NL80211_IFTYPE_P2P_CLIENT)
  2894. combination_has_p2p = 1;
  2895. if (ift == NL80211_IFTYPE_STATION)
  2896. combination_has_mgd = 1;
  2897. }
  2898. if (combination_has_p2p && combination_has_mgd)
  2899. break;
  2900. }
  2901. if (combination_has_p2p && combination_has_mgd) {
  2902. info->p2p_concurrent = 1;
  2903. info->num_multichan_concurrent =
  2904. nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
  2905. return 1;
  2906. }
  2907. return 0;
  2908. }
  2909. static void wiphy_info_iface_comb(struct wiphy_info_data *info,
  2910. struct nlattr *tb)
  2911. {
  2912. struct nlattr *nl_combi;
  2913. int rem_combi;
  2914. if (tb == NULL)
  2915. return;
  2916. nla_for_each_nested(nl_combi, tb, rem_combi) {
  2917. if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
  2918. break;
  2919. }
  2920. }
  2921. static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
  2922. struct nlattr *tb)
  2923. {
  2924. struct nlattr *nl_cmd;
  2925. int i;
  2926. if (tb == NULL)
  2927. return;
  2928. nla_for_each_nested(nl_cmd, tb, i) {
  2929. switch (nla_get_u32(nl_cmd)) {
  2930. case NL80211_CMD_AUTHENTICATE:
  2931. info->auth_supported = 1;
  2932. break;
  2933. case NL80211_CMD_CONNECT:
  2934. info->connect_supported = 1;
  2935. break;
  2936. case NL80211_CMD_START_SCHED_SCAN:
  2937. info->capa->sched_scan_supported = 1;
  2938. break;
  2939. case NL80211_CMD_PROBE_CLIENT:
  2940. info->poll_command_supported = 1;
  2941. break;
  2942. case NL80211_CMD_CHANNEL_SWITCH:
  2943. info->channel_switch_supported = 1;
  2944. break;
  2945. case NL80211_CMD_SET_QOS_MAP:
  2946. info->set_qos_map_supported = 1;
  2947. break;
  2948. }
  2949. }
  2950. }
  2951. static void wiphy_info_cipher_suites(struct wiphy_info_data *info,
  2952. struct nlattr *tb)
  2953. {
  2954. int i, num;
  2955. u32 *ciphers;
  2956. if (tb == NULL)
  2957. return;
  2958. num = nla_len(tb) / sizeof(u32);
  2959. ciphers = nla_data(tb);
  2960. for (i = 0; i < num; i++) {
  2961. u32 c = ciphers[i];
  2962. wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
  2963. c >> 24, (c >> 16) & 0xff,
  2964. (c >> 8) & 0xff, c & 0xff);
  2965. switch (c) {
  2966. case WLAN_CIPHER_SUITE_CCMP_256:
  2967. info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
  2968. break;
  2969. case WLAN_CIPHER_SUITE_GCMP_256:
  2970. info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
  2971. break;
  2972. case WLAN_CIPHER_SUITE_CCMP:
  2973. info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
  2974. break;
  2975. case WLAN_CIPHER_SUITE_GCMP:
  2976. info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
  2977. break;
  2978. case WLAN_CIPHER_SUITE_TKIP:
  2979. info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
  2980. break;
  2981. case WLAN_CIPHER_SUITE_WEP104:
  2982. info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
  2983. break;
  2984. case WLAN_CIPHER_SUITE_WEP40:
  2985. info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
  2986. break;
  2987. case WLAN_CIPHER_SUITE_AES_CMAC:
  2988. info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
  2989. break;
  2990. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  2991. info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
  2992. break;
  2993. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  2994. info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
  2995. break;
  2996. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  2997. info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
  2998. break;
  2999. case WLAN_CIPHER_SUITE_NO_GROUP_ADDR:
  3000. info->capa->enc |= WPA_DRIVER_CAPA_ENC_GTK_NOT_USED;
  3001. break;
  3002. }
  3003. }
  3004. }
  3005. static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
  3006. struct nlattr *tb)
  3007. {
  3008. if (tb)
  3009. capa->max_remain_on_chan = nla_get_u32(tb);
  3010. }
  3011. static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
  3012. struct nlattr *ext_setup)
  3013. {
  3014. if (tdls == NULL)
  3015. return;
  3016. wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
  3017. capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
  3018. if (ext_setup) {
  3019. wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
  3020. capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
  3021. }
  3022. }
  3023. static void wiphy_info_feature_flags(struct wiphy_info_data *info,
  3024. struct nlattr *tb)
  3025. {
  3026. u32 flags;
  3027. struct wpa_driver_capa *capa = info->capa;
  3028. if (tb == NULL)
  3029. return;
  3030. flags = nla_get_u32(tb);
  3031. if (flags & NL80211_FEATURE_SK_TX_STATUS)
  3032. info->data_tx_status = 1;
  3033. if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
  3034. capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
  3035. if (flags & NL80211_FEATURE_SAE)
  3036. capa->flags |= WPA_DRIVER_FLAGS_SAE;
  3037. if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
  3038. capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
  3039. }
  3040. static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
  3041. struct nlattr *tb)
  3042. {
  3043. u32 protocols;
  3044. if (tb == NULL)
  3045. return;
  3046. protocols = nla_get_u32(tb);
  3047. wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
  3048. "mode");
  3049. capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
  3050. capa->probe_resp_offloads = probe_resp_offload_support(protocols);
  3051. }
  3052. static int wiphy_info_handler(struct nl_msg *msg, void *arg)
  3053. {
  3054. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  3055. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  3056. struct wiphy_info_data *info = arg;
  3057. struct wpa_driver_capa *capa = info->capa;
  3058. struct wpa_driver_nl80211_data *drv = info->drv;
  3059. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  3060. genlmsg_attrlen(gnlh, 0), NULL);
  3061. if (tb[NL80211_ATTR_WIPHY_NAME])
  3062. os_strlcpy(drv->phyname,
  3063. nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
  3064. sizeof(drv->phyname));
  3065. if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
  3066. capa->max_scan_ssids =
  3067. nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
  3068. if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
  3069. capa->max_sched_scan_ssids =
  3070. nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
  3071. if (tb[NL80211_ATTR_MAX_MATCH_SETS])
  3072. capa->max_match_sets =
  3073. nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
  3074. if (tb[NL80211_ATTR_MAC_ACL_MAX])
  3075. capa->max_acl_mac_addrs =
  3076. nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
  3077. wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
  3078. wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
  3079. wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
  3080. wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
  3081. if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
  3082. wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
  3083. "off-channel TX");
  3084. capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
  3085. }
  3086. if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
  3087. wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
  3088. capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
  3089. }
  3090. wiphy_info_max_roc(capa,
  3091. tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
  3092. if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
  3093. capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
  3094. wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
  3095. tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
  3096. if (tb[NL80211_ATTR_DEVICE_AP_SME])
  3097. info->device_ap_sme = 1;
  3098. wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
  3099. wiphy_info_probe_resp_offload(capa,
  3100. tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
  3101. if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
  3102. drv->extended_capa == NULL) {
  3103. drv->extended_capa =
  3104. os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
  3105. if (drv->extended_capa) {
  3106. os_memcpy(drv->extended_capa,
  3107. nla_data(tb[NL80211_ATTR_EXT_CAPA]),
  3108. nla_len(tb[NL80211_ATTR_EXT_CAPA]));
  3109. drv->extended_capa_len =
  3110. nla_len(tb[NL80211_ATTR_EXT_CAPA]);
  3111. }
  3112. drv->extended_capa_mask =
  3113. os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
  3114. if (drv->extended_capa_mask) {
  3115. os_memcpy(drv->extended_capa_mask,
  3116. nla_data(tb[NL80211_ATTR_EXT_CAPA]),
  3117. nla_len(tb[NL80211_ATTR_EXT_CAPA]));
  3118. } else {
  3119. os_free(drv->extended_capa);
  3120. drv->extended_capa = NULL;
  3121. drv->extended_capa_len = 0;
  3122. }
  3123. }
  3124. if (tb[NL80211_ATTR_VENDOR_DATA]) {
  3125. struct nlattr *nl;
  3126. int rem;
  3127. nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_DATA], rem) {
  3128. struct nl80211_vendor_cmd_info *vinfo;
  3129. if (nla_len(nl) != sizeof(*vinfo)) {
  3130. wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
  3131. continue;
  3132. }
  3133. vinfo = nla_data(nl);
  3134. if (vinfo->subcmd ==
  3135. QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY)
  3136. drv->dfs_vendor_cmd_avail = 1;
  3137. wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
  3138. vinfo->vendor_id, vinfo->subcmd);
  3139. }
  3140. }
  3141. if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
  3142. struct nlattr *nl;
  3143. int rem;
  3144. nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
  3145. struct nl80211_vendor_cmd_info *vinfo;
  3146. if (nla_len(nl) != sizeof(*vinfo)) {
  3147. wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
  3148. continue;
  3149. }
  3150. vinfo = nla_data(nl);
  3151. wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
  3152. vinfo->vendor_id, vinfo->subcmd);
  3153. }
  3154. }
  3155. return NL_SKIP;
  3156. }
  3157. static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
  3158. struct wiphy_info_data *info)
  3159. {
  3160. u32 feat;
  3161. struct nl_msg *msg;
  3162. os_memset(info, 0, sizeof(*info));
  3163. info->capa = &drv->capa;
  3164. info->drv = drv;
  3165. msg = nlmsg_alloc();
  3166. if (!msg)
  3167. return -1;
  3168. feat = get_nl80211_protocol_features(drv);
  3169. if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
  3170. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
  3171. else
  3172. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
  3173. NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
  3174. if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
  3175. goto nla_put_failure;
  3176. if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
  3177. return -1;
  3178. if (info->auth_supported)
  3179. drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
  3180. else if (!info->connect_supported) {
  3181. wpa_printf(MSG_INFO, "nl80211: Driver does not support "
  3182. "authentication/association or connect commands");
  3183. info->error = 1;
  3184. }
  3185. if (info->p2p_go_supported && info->p2p_client_supported)
  3186. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
  3187. if (info->p2p_concurrent) {
  3188. wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
  3189. "interface (driver advertised support)");
  3190. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
  3191. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
  3192. }
  3193. if (info->num_multichan_concurrent > 1) {
  3194. wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
  3195. "concurrent (driver advertised support)");
  3196. drv->capa.num_multichan_concurrent =
  3197. info->num_multichan_concurrent;
  3198. }
  3199. /* default to 5000 since early versions of mac80211 don't set it */
  3200. if (!drv->capa.max_remain_on_chan)
  3201. drv->capa.max_remain_on_chan = 5000;
  3202. if (info->channel_switch_supported)
  3203. drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
  3204. return 0;
  3205. nla_put_failure:
  3206. nlmsg_free(msg);
  3207. return -1;
  3208. }
  3209. static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
  3210. {
  3211. struct wiphy_info_data info;
  3212. if (wpa_driver_nl80211_get_info(drv, &info))
  3213. return -1;
  3214. if (info.error)
  3215. return -1;
  3216. drv->has_capability = 1;
  3217. drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  3218. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  3219. WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  3220. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
  3221. drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
  3222. WPA_DRIVER_AUTH_SHARED |
  3223. WPA_DRIVER_AUTH_LEAP;
  3224. drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
  3225. drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
  3226. drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
  3227. /*
  3228. * As all cfg80211 drivers must support cases where the AP interface is
  3229. * removed without the knowledge of wpa_supplicant/hostapd, e.g., in
  3230. * case that the user space daemon has crashed, they must be able to
  3231. * cleanup all stations and key entries in the AP tear down flow. Thus,
  3232. * this flag can/should always be set for cfg80211 drivers.
  3233. */
  3234. drv->capa.flags |= WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT;
  3235. if (!info.device_ap_sme) {
  3236. drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
  3237. /*
  3238. * No AP SME is currently assumed to also indicate no AP MLME
  3239. * in the driver/firmware.
  3240. */
  3241. drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
  3242. }
  3243. drv->device_ap_sme = info.device_ap_sme;
  3244. drv->poll_command_supported = info.poll_command_supported;
  3245. drv->data_tx_status = info.data_tx_status;
  3246. if (info.set_qos_map_supported)
  3247. drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
  3248. /*
  3249. * If poll command and tx status are supported, mac80211 is new enough
  3250. * to have everything we need to not need monitor interfaces.
  3251. */
  3252. drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
  3253. if (drv->device_ap_sme && drv->use_monitor) {
  3254. /*
  3255. * Non-mac80211 drivers may not support monitor interface.
  3256. * Make sure we do not get stuck with incorrect capability here
  3257. * by explicitly testing this.
  3258. */
  3259. if (!info.monitor_supported) {
  3260. wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
  3261. "with device_ap_sme since no monitor mode "
  3262. "support detected");
  3263. drv->use_monitor = 0;
  3264. }
  3265. }
  3266. /*
  3267. * If we aren't going to use monitor interfaces, but the
  3268. * driver doesn't support data TX status, we won't get TX
  3269. * status for EAPOL frames.
  3270. */
  3271. if (!drv->use_monitor && !info.data_tx_status)
  3272. drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
  3273. return 0;
  3274. }
  3275. #ifdef ANDROID
  3276. static int android_genl_ctrl_resolve(struct nl_handle *handle,
  3277. const char *name)
  3278. {
  3279. /*
  3280. * Android ICS has very minimal genl_ctrl_resolve() implementation, so
  3281. * need to work around that.
  3282. */
  3283. struct nl_cache *cache = NULL;
  3284. struct genl_family *nl80211 = NULL;
  3285. int id = -1;
  3286. if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
  3287. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  3288. "netlink cache");
  3289. goto fail;
  3290. }
  3291. nl80211 = genl_ctrl_search_by_name(cache, name);
  3292. if (nl80211 == NULL)
  3293. goto fail;
  3294. id = genl_family_get_id(nl80211);
  3295. fail:
  3296. if (nl80211)
  3297. genl_family_put(nl80211);
  3298. if (cache)
  3299. nl_cache_free(cache);
  3300. return id;
  3301. }
  3302. #define genl_ctrl_resolve android_genl_ctrl_resolve
  3303. #endif /* ANDROID */
  3304. static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
  3305. {
  3306. int ret;
  3307. global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  3308. if (global->nl_cb == NULL) {
  3309. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  3310. "callbacks");
  3311. return -1;
  3312. }
  3313. global->nl = nl_create_handle(global->nl_cb, "nl");
  3314. if (global->nl == NULL)
  3315. goto err;
  3316. global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
  3317. if (global->nl80211_id < 0) {
  3318. wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
  3319. "found");
  3320. goto err;
  3321. }
  3322. global->nl_event = nl_create_handle(global->nl_cb, "event");
  3323. if (global->nl_event == NULL)
  3324. goto err;
  3325. ret = nl_get_multicast_id(global, "nl80211", "scan");
  3326. if (ret >= 0)
  3327. ret = nl_socket_add_membership(global->nl_event, ret);
  3328. if (ret < 0) {
  3329. wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
  3330. "membership for scan events: %d (%s)",
  3331. ret, strerror(-ret));
  3332. goto err;
  3333. }
  3334. ret = nl_get_multicast_id(global, "nl80211", "mlme");
  3335. if (ret >= 0)
  3336. ret = nl_socket_add_membership(global->nl_event, ret);
  3337. if (ret < 0) {
  3338. wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
  3339. "membership for mlme events: %d (%s)",
  3340. ret, strerror(-ret));
  3341. goto err;
  3342. }
  3343. ret = nl_get_multicast_id(global, "nl80211", "regulatory");
  3344. if (ret >= 0)
  3345. ret = nl_socket_add_membership(global->nl_event, ret);
  3346. if (ret < 0) {
  3347. wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
  3348. "membership for regulatory events: %d (%s)",
  3349. ret, strerror(-ret));
  3350. /* Continue without regulatory events */
  3351. }
  3352. ret = nl_get_multicast_id(global, "nl80211", "vendor");
  3353. if (ret >= 0)
  3354. ret = nl_socket_add_membership(global->nl_event, ret);
  3355. if (ret < 0) {
  3356. wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
  3357. "membership for vendor events: %d (%s)",
  3358. ret, strerror(-ret));
  3359. /* Continue without vendor events */
  3360. }
  3361. nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
  3362. no_seq_check, NULL);
  3363. nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
  3364. process_global_event, global);
  3365. nl80211_register_eloop_read(&global->nl_event,
  3366. wpa_driver_nl80211_event_receive,
  3367. global->nl_cb);
  3368. return 0;
  3369. err:
  3370. nl_destroy_handles(&global->nl_event);
  3371. nl_destroy_handles(&global->nl);
  3372. nl_cb_put(global->nl_cb);
  3373. global->nl_cb = NULL;
  3374. return -1;
  3375. }
  3376. static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
  3377. {
  3378. drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  3379. if (!drv->nl_cb) {
  3380. wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
  3381. return -1;
  3382. }
  3383. nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
  3384. no_seq_check, NULL);
  3385. nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
  3386. process_drv_event, drv);
  3387. return 0;
  3388. }
  3389. static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
  3390. {
  3391. wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
  3392. /*
  3393. * This may be for any interface; use ifdown event to disable
  3394. * interface.
  3395. */
  3396. }
  3397. static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
  3398. {
  3399. struct wpa_driver_nl80211_data *drv = ctx;
  3400. wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
  3401. if (i802_set_iface_flags(drv->first_bss, 1)) {
  3402. wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
  3403. "after rfkill unblock");
  3404. return;
  3405. }
  3406. /* rtnetlink ifup handler will report interface as enabled */
  3407. }
  3408. static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
  3409. void *eloop_ctx,
  3410. void *handle)
  3411. {
  3412. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  3413. u8 data[2048];
  3414. struct msghdr msg;
  3415. struct iovec entry;
  3416. u8 control[512];
  3417. struct cmsghdr *cmsg;
  3418. int res, found_ee = 0, found_wifi = 0, acked = 0;
  3419. union wpa_event_data event;
  3420. memset(&msg, 0, sizeof(msg));
  3421. msg.msg_iov = &entry;
  3422. msg.msg_iovlen = 1;
  3423. entry.iov_base = data;
  3424. entry.iov_len = sizeof(data);
  3425. msg.msg_control = &control;
  3426. msg.msg_controllen = sizeof(control);
  3427. res = recvmsg(sock, &msg, MSG_ERRQUEUE);
  3428. /* if error or not fitting 802.3 header, return */
  3429. if (res < 14)
  3430. return;
  3431. for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
  3432. {
  3433. if (cmsg->cmsg_level == SOL_SOCKET &&
  3434. cmsg->cmsg_type == SCM_WIFI_STATUS) {
  3435. int *ack;
  3436. found_wifi = 1;
  3437. ack = (void *)CMSG_DATA(cmsg);
  3438. acked = *ack;
  3439. }
  3440. if (cmsg->cmsg_level == SOL_PACKET &&
  3441. cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
  3442. struct sock_extended_err *err =
  3443. (struct sock_extended_err *)CMSG_DATA(cmsg);
  3444. if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
  3445. found_ee = 1;
  3446. }
  3447. }
  3448. if (!found_ee || !found_wifi)
  3449. return;
  3450. memset(&event, 0, sizeof(event));
  3451. event.eapol_tx_status.dst = data;
  3452. event.eapol_tx_status.data = data + 14;
  3453. event.eapol_tx_status.data_len = res - 14;
  3454. event.eapol_tx_status.ack = acked;
  3455. wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
  3456. }
  3457. static int nl80211_init_bss(struct i802_bss *bss)
  3458. {
  3459. bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  3460. if (!bss->nl_cb)
  3461. return -1;
  3462. nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
  3463. no_seq_check, NULL);
  3464. nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
  3465. process_bss_event, bss);
  3466. return 0;
  3467. }
  3468. static void nl80211_destroy_bss(struct i802_bss *bss)
  3469. {
  3470. nl_cb_put(bss->nl_cb);
  3471. bss->nl_cb = NULL;
  3472. }
  3473. static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
  3474. void *global_priv, int hostapd,
  3475. const u8 *set_addr)
  3476. {
  3477. struct wpa_driver_nl80211_data *drv;
  3478. struct rfkill_config *rcfg;
  3479. struct i802_bss *bss;
  3480. if (global_priv == NULL)
  3481. return NULL;
  3482. drv = os_zalloc(sizeof(*drv));
  3483. if (drv == NULL)
  3484. return NULL;
  3485. drv->global = global_priv;
  3486. drv->ctx = ctx;
  3487. drv->hostapd = !!hostapd;
  3488. drv->eapol_sock = -1;
  3489. drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
  3490. drv->if_indices = drv->default_if_indices;
  3491. drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
  3492. if (!drv->first_bss) {
  3493. os_free(drv);
  3494. return NULL;
  3495. }
  3496. bss = drv->first_bss;
  3497. bss->drv = drv;
  3498. bss->ctx = ctx;
  3499. os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
  3500. drv->monitor_ifidx = -1;
  3501. drv->monitor_sock = -1;
  3502. drv->eapol_tx_sock = -1;
  3503. drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
  3504. if (wpa_driver_nl80211_init_nl(drv)) {
  3505. os_free(drv);
  3506. return NULL;
  3507. }
  3508. if (nl80211_init_bss(bss))
  3509. goto failed;
  3510. rcfg = os_zalloc(sizeof(*rcfg));
  3511. if (rcfg == NULL)
  3512. goto failed;
  3513. rcfg->ctx = drv;
  3514. os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
  3515. rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
  3516. rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
  3517. drv->rfkill = rfkill_init(rcfg);
  3518. if (drv->rfkill == NULL) {
  3519. wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
  3520. os_free(rcfg);
  3521. }
  3522. if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
  3523. drv->start_iface_up = 1;
  3524. if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1))
  3525. goto failed;
  3526. drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
  3527. if (drv->eapol_tx_sock < 0)
  3528. goto failed;
  3529. if (drv->data_tx_status) {
  3530. int enabled = 1;
  3531. if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
  3532. &enabled, sizeof(enabled)) < 0) {
  3533. wpa_printf(MSG_DEBUG,
  3534. "nl80211: wifi status sockopt failed\n");
  3535. drv->data_tx_status = 0;
  3536. if (!drv->use_monitor)
  3537. drv->capa.flags &=
  3538. ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
  3539. } else {
  3540. eloop_register_read_sock(drv->eapol_tx_sock,
  3541. wpa_driver_nl80211_handle_eapol_tx_status,
  3542. drv, NULL);
  3543. }
  3544. }
  3545. if (drv->global) {
  3546. dl_list_add(&drv->global->interfaces, &drv->list);
  3547. drv->in_interface_list = 1;
  3548. }
  3549. return bss;
  3550. failed:
  3551. wpa_driver_nl80211_deinit(bss);
  3552. return NULL;
  3553. }
  3554. /**
  3555. * wpa_driver_nl80211_init - Initialize nl80211 driver interface
  3556. * @ctx: context to be used when calling wpa_supplicant functions,
  3557. * e.g., wpa_supplicant_event()
  3558. * @ifname: interface name, e.g., wlan0
  3559. * @global_priv: private driver global data from global_init()
  3560. * Returns: Pointer to private data, %NULL on failure
  3561. */
  3562. static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
  3563. void *global_priv)
  3564. {
  3565. return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL);
  3566. }
  3567. static int nl80211_register_frame(struct i802_bss *bss,
  3568. struct nl_handle *nl_handle,
  3569. u16 type, const u8 *match, size_t match_len)
  3570. {
  3571. struct wpa_driver_nl80211_data *drv = bss->drv;
  3572. struct nl_msg *msg;
  3573. int ret = -1;
  3574. char buf[30];
  3575. msg = nlmsg_alloc();
  3576. if (!msg)
  3577. return -1;
  3578. buf[0] = '\0';
  3579. wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
  3580. wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p match=%s",
  3581. type, nl_handle, buf);
  3582. nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
  3583. if (nl80211_set_iface_id(msg, bss) < 0)
  3584. goto nla_put_failure;
  3585. NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
  3586. NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
  3587. ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
  3588. msg = NULL;
  3589. if (ret) {
  3590. wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
  3591. "failed (type=%u): ret=%d (%s)",
  3592. type, ret, strerror(-ret));
  3593. wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
  3594. match, match_len);
  3595. goto nla_put_failure;
  3596. }
  3597. ret = 0;
  3598. nla_put_failure:
  3599. nlmsg_free(msg);
  3600. return ret;
  3601. }
  3602. static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
  3603. {
  3604. struct wpa_driver_nl80211_data *drv = bss->drv;
  3605. if (bss->nl_mgmt) {
  3606. wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
  3607. "already on! (nl_mgmt=%p)", bss->nl_mgmt);
  3608. return -1;
  3609. }
  3610. bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
  3611. if (bss->nl_mgmt == NULL)
  3612. return -1;
  3613. return 0;
  3614. }
  3615. static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
  3616. {
  3617. nl80211_register_eloop_read(&bss->nl_mgmt,
  3618. wpa_driver_nl80211_event_receive,
  3619. bss->nl_cb);
  3620. }
  3621. static int nl80211_register_action_frame(struct i802_bss *bss,
  3622. const u8 *match, size_t match_len)
  3623. {
  3624. u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
  3625. return nl80211_register_frame(bss, bss->nl_mgmt,
  3626. type, match, match_len);
  3627. }
  3628. static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
  3629. {
  3630. struct wpa_driver_nl80211_data *drv = bss->drv;
  3631. int ret = 0;
  3632. if (nl80211_alloc_mgmt_handle(bss))
  3633. return -1;
  3634. wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
  3635. "handle %p", bss->nl_mgmt);
  3636. if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
  3637. u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
  3638. /* register for any AUTH message */
  3639. nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
  3640. }
  3641. #ifdef CONFIG_INTERWORKING
  3642. /* QoS Map Configure */
  3643. if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
  3644. ret = -1;
  3645. #endif /* CONFIG_INTERWORKING */
  3646. #if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
  3647. /* GAS Initial Request */
  3648. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
  3649. ret = -1;
  3650. /* GAS Initial Response */
  3651. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
  3652. ret = -1;
  3653. /* GAS Comeback Request */
  3654. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
  3655. ret = -1;
  3656. /* GAS Comeback Response */
  3657. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
  3658. ret = -1;
  3659. /* Protected GAS Initial Request */
  3660. if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
  3661. ret = -1;
  3662. /* Protected GAS Initial Response */
  3663. if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
  3664. ret = -1;
  3665. /* Protected GAS Comeback Request */
  3666. if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
  3667. ret = -1;
  3668. /* Protected GAS Comeback Response */
  3669. if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
  3670. ret = -1;
  3671. #endif /* CONFIG_P2P || CONFIG_INTERWORKING */
  3672. #ifdef CONFIG_P2P
  3673. /* P2P Public Action */
  3674. if (nl80211_register_action_frame(bss,
  3675. (u8 *) "\x04\x09\x50\x6f\x9a\x09",
  3676. 6) < 0)
  3677. ret = -1;
  3678. /* P2P Action */
  3679. if (nl80211_register_action_frame(bss,
  3680. (u8 *) "\x7f\x50\x6f\x9a\x09",
  3681. 5) < 0)
  3682. ret = -1;
  3683. #endif /* CONFIG_P2P */
  3684. #ifdef CONFIG_IEEE80211W
  3685. /* SA Query Response */
  3686. if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
  3687. ret = -1;
  3688. #endif /* CONFIG_IEEE80211W */
  3689. #ifdef CONFIG_TDLS
  3690. if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
  3691. /* TDLS Discovery Response */
  3692. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
  3693. 0)
  3694. ret = -1;
  3695. }
  3696. #endif /* CONFIG_TDLS */
  3697. /* FT Action frames */
  3698. if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
  3699. ret = -1;
  3700. else
  3701. drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
  3702. WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
  3703. /* WNM - BSS Transition Management Request */
  3704. if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
  3705. ret = -1;
  3706. /* WNM-Sleep Mode Response */
  3707. if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
  3708. ret = -1;
  3709. #ifdef CONFIG_HS20
  3710. /* WNM-Notification */
  3711. if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
  3712. return -1;
  3713. #endif /* CONFIG_HS20 */
  3714. nl80211_mgmt_handle_register_eloop(bss);
  3715. return ret;
  3716. }
  3717. static int nl80211_register_spurious_class3(struct i802_bss *bss)
  3718. {
  3719. struct wpa_driver_nl80211_data *drv = bss->drv;
  3720. struct nl_msg *msg;
  3721. int ret = -1;
  3722. msg = nlmsg_alloc();
  3723. if (!msg)
  3724. return -1;
  3725. nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
  3726. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  3727. ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
  3728. msg = NULL;
  3729. if (ret) {
  3730. wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
  3731. "failed: ret=%d (%s)",
  3732. ret, strerror(-ret));
  3733. goto nla_put_failure;
  3734. }
  3735. ret = 0;
  3736. nla_put_failure:
  3737. nlmsg_free(msg);
  3738. return ret;
  3739. }
  3740. static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
  3741. {
  3742. static const int stypes[] = {
  3743. WLAN_FC_STYPE_AUTH,
  3744. WLAN_FC_STYPE_ASSOC_REQ,
  3745. WLAN_FC_STYPE_REASSOC_REQ,
  3746. WLAN_FC_STYPE_DISASSOC,
  3747. WLAN_FC_STYPE_DEAUTH,
  3748. WLAN_FC_STYPE_ACTION,
  3749. WLAN_FC_STYPE_PROBE_REQ,
  3750. /* Beacon doesn't work as mac80211 doesn't currently allow
  3751. * it, but it wouldn't really be the right thing anyway as
  3752. * it isn't per interface ... maybe just dump the scan
  3753. * results periodically for OLBC?
  3754. */
  3755. // WLAN_FC_STYPE_BEACON,
  3756. };
  3757. unsigned int i;
  3758. if (nl80211_alloc_mgmt_handle(bss))
  3759. return -1;
  3760. wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
  3761. "handle %p", bss->nl_mgmt);
  3762. for (i = 0; i < ARRAY_SIZE(stypes); i++) {
  3763. if (nl80211_register_frame(bss, bss->nl_mgmt,
  3764. (WLAN_FC_TYPE_MGMT << 2) |
  3765. (stypes[i] << 4),
  3766. NULL, 0) < 0) {
  3767. goto out_err;
  3768. }
  3769. }
  3770. if (nl80211_register_spurious_class3(bss))
  3771. goto out_err;
  3772. if (nl80211_get_wiphy_data_ap(bss) == NULL)
  3773. goto out_err;
  3774. nl80211_mgmt_handle_register_eloop(bss);
  3775. return 0;
  3776. out_err:
  3777. nl_destroy_handles(&bss->nl_mgmt);
  3778. return -1;
  3779. }
  3780. static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
  3781. {
  3782. if (nl80211_alloc_mgmt_handle(bss))
  3783. return -1;
  3784. wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
  3785. "handle %p (device SME)", bss->nl_mgmt);
  3786. if (nl80211_register_frame(bss, bss->nl_mgmt,
  3787. (WLAN_FC_TYPE_MGMT << 2) |
  3788. (WLAN_FC_STYPE_ACTION << 4),
  3789. NULL, 0) < 0)
  3790. goto out_err;
  3791. nl80211_mgmt_handle_register_eloop(bss);
  3792. return 0;
  3793. out_err:
  3794. nl_destroy_handles(&bss->nl_mgmt);
  3795. return -1;
  3796. }
  3797. static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
  3798. {
  3799. if (bss->nl_mgmt == NULL)
  3800. return;
  3801. wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
  3802. "(%s)", bss->nl_mgmt, reason);
  3803. nl80211_destroy_eloop_handle(&bss->nl_mgmt);
  3804. nl80211_put_wiphy_data_ap(bss);
  3805. }
  3806. static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
  3807. {
  3808. wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
  3809. }
  3810. static void nl80211_del_p2pdev(struct i802_bss *bss)
  3811. {
  3812. struct wpa_driver_nl80211_data *drv = bss->drv;
  3813. struct nl_msg *msg;
  3814. int ret;
  3815. msg = nlmsg_alloc();
  3816. if (!msg)
  3817. return;
  3818. nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
  3819. NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
  3820. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3821. msg = NULL;
  3822. wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
  3823. bss->ifname, (long long unsigned int) bss->wdev_id,
  3824. strerror(-ret));
  3825. nla_put_failure:
  3826. nlmsg_free(msg);
  3827. }
  3828. static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
  3829. {
  3830. struct wpa_driver_nl80211_data *drv = bss->drv;
  3831. struct nl_msg *msg;
  3832. int ret = -1;
  3833. msg = nlmsg_alloc();
  3834. if (!msg)
  3835. return -1;
  3836. if (start)
  3837. nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
  3838. else
  3839. nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
  3840. NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
  3841. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3842. msg = NULL;
  3843. wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
  3844. start ? "Start" : "Stop",
  3845. bss->ifname, (long long unsigned int) bss->wdev_id,
  3846. strerror(-ret));
  3847. nla_put_failure:
  3848. nlmsg_free(msg);
  3849. return ret;
  3850. }
  3851. static int i802_set_iface_flags(struct i802_bss *bss, int up)
  3852. {
  3853. enum nl80211_iftype nlmode;
  3854. nlmode = nl80211_get_ifmode(bss);
  3855. if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
  3856. return linux_set_iface_flags(bss->drv->global->ioctl_sock,
  3857. bss->ifname, up);
  3858. }
  3859. /* P2P Device has start/stop which is equivalent */
  3860. return nl80211_set_p2pdev(bss, up);
  3861. }
  3862. static int
  3863. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
  3864. const u8 *set_addr, int first)
  3865. {
  3866. struct i802_bss *bss = drv->first_bss;
  3867. int send_rfkill_event = 0;
  3868. enum nl80211_iftype nlmode;
  3869. drv->ifindex = if_nametoindex(bss->ifname);
  3870. bss->ifindex = drv->ifindex;
  3871. bss->wdev_id = drv->global->if_add_wdevid;
  3872. bss->wdev_id_set = drv->global->if_add_wdevid_set;
  3873. bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
  3874. bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
  3875. drv->global->if_add_wdevid_set = 0;
  3876. if (wpa_driver_nl80211_capa(drv))
  3877. return -1;
  3878. wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
  3879. bss->ifname, drv->phyname);
  3880. if (set_addr &&
  3881. (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
  3882. linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  3883. set_addr)))
  3884. return -1;
  3885. if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
  3886. drv->start_mode_ap = 1;
  3887. if (drv->hostapd)
  3888. nlmode = NL80211_IFTYPE_AP;
  3889. else if (bss->if_dynamic)
  3890. nlmode = nl80211_get_ifmode(bss);
  3891. else
  3892. nlmode = NL80211_IFTYPE_STATION;
  3893. if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
  3894. wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
  3895. return -1;
  3896. }
  3897. if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
  3898. nl80211_get_macaddr(bss);
  3899. if (!rfkill_is_blocked(drv->rfkill)) {
  3900. int ret = i802_set_iface_flags(bss, 1);
  3901. if (ret) {
  3902. wpa_printf(MSG_ERROR, "nl80211: Could not set "
  3903. "interface '%s' UP", bss->ifname);
  3904. return ret;
  3905. }
  3906. if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
  3907. return ret;
  3908. } else {
  3909. wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
  3910. "interface '%s' due to rfkill", bss->ifname);
  3911. if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
  3912. return 0;
  3913. drv->if_disabled = 1;
  3914. send_rfkill_event = 1;
  3915. }
  3916. if (!drv->hostapd)
  3917. netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
  3918. 1, IF_OPER_DORMANT);
  3919. if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  3920. bss->addr))
  3921. return -1;
  3922. if (send_rfkill_event) {
  3923. eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
  3924. drv, drv->ctx);
  3925. }
  3926. return 0;
  3927. }
  3928. static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
  3929. {
  3930. struct nl_msg *msg;
  3931. msg = nlmsg_alloc();
  3932. if (!msg)
  3933. return -ENOMEM;
  3934. wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
  3935. drv->ifindex);
  3936. nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
  3937. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3938. return send_and_recv_msgs(drv, msg, NULL, NULL);
  3939. nla_put_failure:
  3940. nlmsg_free(msg);
  3941. return -ENOBUFS;
  3942. }
  3943. /**
  3944. * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
  3945. * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
  3946. *
  3947. * Shut down driver interface and processing of driver events. Free
  3948. * private data buffer if one was allocated in wpa_driver_nl80211_init().
  3949. */
  3950. static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
  3951. {
  3952. struct wpa_driver_nl80211_data *drv = bss->drv;
  3953. bss->in_deinit = 1;
  3954. if (drv->data_tx_status)
  3955. eloop_unregister_read_sock(drv->eapol_tx_sock);
  3956. if (drv->eapol_tx_sock >= 0)
  3957. close(drv->eapol_tx_sock);
  3958. if (bss->nl_preq)
  3959. wpa_driver_nl80211_probe_req_report(bss, 0);
  3960. if (bss->added_if_into_bridge) {
  3961. if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
  3962. bss->ifname) < 0)
  3963. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  3964. "interface %s from bridge %s: %s",
  3965. bss->ifname, bss->brname, strerror(errno));
  3966. }
  3967. if (bss->added_bridge) {
  3968. if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
  3969. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  3970. "bridge %s: %s",
  3971. bss->brname, strerror(errno));
  3972. }
  3973. nl80211_remove_monitor_interface(drv);
  3974. if (is_ap_interface(drv->nlmode))
  3975. wpa_driver_nl80211_del_beacon(drv);
  3976. if (drv->eapol_sock >= 0) {
  3977. eloop_unregister_read_sock(drv->eapol_sock);
  3978. close(drv->eapol_sock);
  3979. }
  3980. if (drv->if_indices != drv->default_if_indices)
  3981. os_free(drv->if_indices);
  3982. if (drv->disabled_11b_rates)
  3983. nl80211_disable_11b_rates(drv, drv->ifindex, 0);
  3984. netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
  3985. IF_OPER_UP);
  3986. rfkill_deinit(drv->rfkill);
  3987. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
  3988. if (!drv->start_iface_up)
  3989. (void) i802_set_iface_flags(bss, 0);
  3990. if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
  3991. if (!drv->hostapd || !drv->start_mode_ap)
  3992. wpa_driver_nl80211_set_mode(bss,
  3993. NL80211_IFTYPE_STATION);
  3994. nl80211_mgmt_unsubscribe(bss, "deinit");
  3995. } else {
  3996. nl80211_mgmt_unsubscribe(bss, "deinit");
  3997. nl80211_del_p2pdev(bss);
  3998. }
  3999. nl_cb_put(drv->nl_cb);
  4000. nl80211_destroy_bss(drv->first_bss);
  4001. os_free(drv->filter_ssids);
  4002. os_free(drv->auth_ie);
  4003. if (drv->in_interface_list)
  4004. dl_list_del(&drv->list);
  4005. os_free(drv->extended_capa);
  4006. os_free(drv->extended_capa_mask);
  4007. os_free(drv->first_bss);
  4008. os_free(drv);
  4009. }
  4010. /**
  4011. * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
  4012. * @eloop_ctx: Driver private data
  4013. * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
  4014. *
  4015. * This function can be used as registered timeout when starting a scan to
  4016. * generate a scan completed event if the driver does not report this.
  4017. */
  4018. static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  4019. {
  4020. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  4021. if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
  4022. wpa_driver_nl80211_set_mode(drv->first_bss,
  4023. drv->ap_scan_as_station);
  4024. drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
  4025. }
  4026. wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
  4027. wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
  4028. }
  4029. static struct nl_msg *
  4030. nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
  4031. struct wpa_driver_scan_params *params, u64 *wdev_id)
  4032. {
  4033. struct nl_msg *msg;
  4034. size_t i;
  4035. msg = nlmsg_alloc();
  4036. if (!msg)
  4037. return NULL;
  4038. nl80211_cmd(drv, msg, 0, cmd);
  4039. if (!wdev_id)
  4040. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  4041. else
  4042. NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
  4043. if (params->num_ssids) {
  4044. struct nlattr *ssids;
  4045. ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
  4046. if (ssids == NULL)
  4047. goto fail;
  4048. for (i = 0; i < params->num_ssids; i++) {
  4049. wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
  4050. params->ssids[i].ssid,
  4051. params->ssids[i].ssid_len);
  4052. if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
  4053. params->ssids[i].ssid) < 0)
  4054. goto fail;
  4055. }
  4056. nla_nest_end(msg, ssids);
  4057. }
  4058. if (params->extra_ies) {
  4059. wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
  4060. params->extra_ies, params->extra_ies_len);
  4061. if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
  4062. params->extra_ies) < 0)
  4063. goto fail;
  4064. }
  4065. if (params->freqs) {
  4066. struct nlattr *freqs;
  4067. freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
  4068. if (freqs == NULL)
  4069. goto fail;
  4070. for (i = 0; params->freqs[i]; i++) {
  4071. wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
  4072. "MHz", params->freqs[i]);
  4073. if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
  4074. goto fail;
  4075. }
  4076. nla_nest_end(msg, freqs);
  4077. }
  4078. os_free(drv->filter_ssids);
  4079. drv->filter_ssids = params->filter_ssids;
  4080. params->filter_ssids = NULL;
  4081. drv->num_filter_ssids = params->num_filter_ssids;
  4082. if (params->only_new_results) {
  4083. wpa_printf(MSG_DEBUG, "nl80211: Add NL80211_SCAN_FLAG_FLUSH");
  4084. NLA_PUT_U32(msg, NL80211_ATTR_SCAN_FLAGS,
  4085. NL80211_SCAN_FLAG_FLUSH);
  4086. }
  4087. return msg;
  4088. fail:
  4089. nla_put_failure:
  4090. nlmsg_free(msg);
  4091. return NULL;
  4092. }
  4093. /**
  4094. * wpa_driver_nl80211_scan - Request the driver to initiate scan
  4095. * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
  4096. * @params: Scan parameters
  4097. * Returns: 0 on success, -1 on failure
  4098. */
  4099. static int wpa_driver_nl80211_scan(struct i802_bss *bss,
  4100. struct wpa_driver_scan_params *params)
  4101. {
  4102. struct wpa_driver_nl80211_data *drv = bss->drv;
  4103. int ret = -1, timeout;
  4104. struct nl_msg *msg = NULL;
  4105. wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
  4106. drv->scan_for_auth = 0;
  4107. msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
  4108. bss->wdev_id_set ? &bss->wdev_id : NULL);
  4109. if (!msg)
  4110. return -1;
  4111. if (params->p2p_probe) {
  4112. struct nlattr *rates;
  4113. wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
  4114. rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
  4115. if (rates == NULL)
  4116. goto nla_put_failure;
  4117. /*
  4118. * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
  4119. * by masking out everything else apart from the OFDM rates 6,
  4120. * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
  4121. * rates are left enabled.
  4122. */
  4123. NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
  4124. "\x0c\x12\x18\x24\x30\x48\x60\x6c");
  4125. nla_nest_end(msg, rates);
  4126. NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
  4127. }
  4128. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4129. msg = NULL;
  4130. if (ret) {
  4131. wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
  4132. "(%s)", ret, strerror(-ret));
  4133. if (drv->hostapd && is_ap_interface(drv->nlmode)) {
  4134. enum nl80211_iftype old_mode = drv->nlmode;
  4135. /*
  4136. * mac80211 does not allow scan requests in AP mode, so
  4137. * try to do this in station mode.
  4138. */
  4139. if (wpa_driver_nl80211_set_mode(
  4140. bss, NL80211_IFTYPE_STATION))
  4141. goto nla_put_failure;
  4142. if (wpa_driver_nl80211_scan(bss, params)) {
  4143. wpa_driver_nl80211_set_mode(bss, drv->nlmode);
  4144. goto nla_put_failure;
  4145. }
  4146. /* Restore AP mode when processing scan results */
  4147. drv->ap_scan_as_station = old_mode;
  4148. ret = 0;
  4149. } else
  4150. goto nla_put_failure;
  4151. }
  4152. drv->scan_state = SCAN_REQUESTED;
  4153. /* Not all drivers generate "scan completed" wireless event, so try to
  4154. * read results after a timeout. */
  4155. timeout = 10;
  4156. if (drv->scan_complete_events) {
  4157. /*
  4158. * The driver seems to deliver events to notify when scan is
  4159. * complete, so use longer timeout to avoid race conditions
  4160. * with scanning and following association request.
  4161. */
  4162. timeout = 30;
  4163. }
  4164. wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
  4165. "seconds", ret, timeout);
  4166. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
  4167. eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
  4168. drv, drv->ctx);
  4169. nla_put_failure:
  4170. nlmsg_free(msg);
  4171. return ret;
  4172. }
  4173. /**
  4174. * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
  4175. * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
  4176. * @params: Scan parameters
  4177. * @interval: Interval between scan cycles in milliseconds
  4178. * Returns: 0 on success, -1 on failure or if not supported
  4179. */
  4180. static int wpa_driver_nl80211_sched_scan(void *priv,
  4181. struct wpa_driver_scan_params *params,
  4182. u32 interval)
  4183. {
  4184. struct i802_bss *bss = priv;
  4185. struct wpa_driver_nl80211_data *drv = bss->drv;
  4186. int ret = -1;
  4187. struct nl_msg *msg;
  4188. size_t i;
  4189. wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
  4190. #ifdef ANDROID
  4191. if (!drv->capa.sched_scan_supported)
  4192. return android_pno_start(bss, params);
  4193. #endif /* ANDROID */
  4194. msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
  4195. bss->wdev_id_set ? &bss->wdev_id : NULL);
  4196. if (!msg)
  4197. goto nla_put_failure;
  4198. NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
  4199. if ((drv->num_filter_ssids &&
  4200. (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
  4201. params->filter_rssi) {
  4202. struct nlattr *match_sets;
  4203. match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
  4204. if (match_sets == NULL)
  4205. goto nla_put_failure;
  4206. for (i = 0; i < drv->num_filter_ssids; i++) {
  4207. struct nlattr *match_set_ssid;
  4208. wpa_hexdump_ascii(MSG_MSGDUMP,
  4209. "nl80211: Sched scan filter SSID",
  4210. drv->filter_ssids[i].ssid,
  4211. drv->filter_ssids[i].ssid_len);
  4212. match_set_ssid = nla_nest_start(msg, i + 1);
  4213. if (match_set_ssid == NULL)
  4214. goto nla_put_failure;
  4215. NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
  4216. drv->filter_ssids[i].ssid_len,
  4217. drv->filter_ssids[i].ssid);
  4218. if (params->filter_rssi)
  4219. NLA_PUT_U32(msg,
  4220. NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
  4221. params->filter_rssi);
  4222. nla_nest_end(msg, match_set_ssid);
  4223. }
  4224. /*
  4225. * Due to backward compatibility code, newer kernels treat this
  4226. * matchset (with only an RSSI filter) as the default for all
  4227. * other matchsets, unless it's the only one, in which case the
  4228. * matchset will actually allow all SSIDs above the RSSI.
  4229. */
  4230. if (params->filter_rssi) {
  4231. struct nlattr *match_set_rssi;
  4232. match_set_rssi = nla_nest_start(msg, 0);
  4233. if (match_set_rssi == NULL)
  4234. goto nla_put_failure;
  4235. NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
  4236. params->filter_rssi);
  4237. wpa_printf(MSG_MSGDUMP,
  4238. "nl80211: Sched scan RSSI filter %d dBm",
  4239. params->filter_rssi);
  4240. nla_nest_end(msg, match_set_rssi);
  4241. }
  4242. nla_nest_end(msg, match_sets);
  4243. }
  4244. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4245. /* TODO: if we get an error here, we should fall back to normal scan */
  4246. msg = NULL;
  4247. if (ret) {
  4248. wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
  4249. "ret=%d (%s)", ret, strerror(-ret));
  4250. goto nla_put_failure;
  4251. }
  4252. wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
  4253. "scan interval %d msec", ret, interval);
  4254. nla_put_failure:
  4255. nlmsg_free(msg);
  4256. return ret;
  4257. }
  4258. /**
  4259. * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
  4260. * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
  4261. * Returns: 0 on success, -1 on failure or if not supported
  4262. */
  4263. static int wpa_driver_nl80211_stop_sched_scan(void *priv)
  4264. {
  4265. struct i802_bss *bss = priv;
  4266. struct wpa_driver_nl80211_data *drv = bss->drv;
  4267. int ret = 0;
  4268. struct nl_msg *msg;
  4269. #ifdef ANDROID
  4270. if (!drv->capa.sched_scan_supported)
  4271. return android_pno_stop(bss);
  4272. #endif /* ANDROID */
  4273. msg = nlmsg_alloc();
  4274. if (!msg)
  4275. return -1;
  4276. nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
  4277. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  4278. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4279. msg = NULL;
  4280. if (ret) {
  4281. wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
  4282. "ret=%d (%s)", ret, strerror(-ret));
  4283. goto nla_put_failure;
  4284. }
  4285. wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
  4286. nla_put_failure:
  4287. nlmsg_free(msg);
  4288. return ret;
  4289. }
  4290. static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
  4291. {
  4292. const u8 *end, *pos;
  4293. if (ies == NULL)
  4294. return NULL;
  4295. pos = ies;
  4296. end = ies + ies_len;
  4297. while (pos + 1 < end) {
  4298. if (pos + 2 + pos[1] > end)
  4299. break;
  4300. if (pos[0] == ie)
  4301. return pos;
  4302. pos += 2 + pos[1];
  4303. }
  4304. return NULL;
  4305. }
  4306. static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
  4307. const u8 *ie, size_t ie_len)
  4308. {
  4309. const u8 *ssid;
  4310. size_t i;
  4311. if (drv->filter_ssids == NULL)
  4312. return 0;
  4313. ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
  4314. if (ssid == NULL)
  4315. return 1;
  4316. for (i = 0; i < drv->num_filter_ssids; i++) {
  4317. if (ssid[1] == drv->filter_ssids[i].ssid_len &&
  4318. os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
  4319. 0)
  4320. return 0;
  4321. }
  4322. return 1;
  4323. }
  4324. static int bss_info_handler(struct nl_msg *msg, void *arg)
  4325. {
  4326. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  4327. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  4328. struct nlattr *bss[NL80211_BSS_MAX + 1];
  4329. static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
  4330. [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
  4331. [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
  4332. [NL80211_BSS_TSF] = { .type = NLA_U64 },
  4333. [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
  4334. [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
  4335. [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
  4336. [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
  4337. [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
  4338. [NL80211_BSS_STATUS] = { .type = NLA_U32 },
  4339. [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
  4340. [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
  4341. };
  4342. struct nl80211_bss_info_arg *_arg = arg;
  4343. struct wpa_scan_results *res = _arg->res;
  4344. struct wpa_scan_res **tmp;
  4345. struct wpa_scan_res *r;
  4346. const u8 *ie, *beacon_ie;
  4347. size_t ie_len, beacon_ie_len;
  4348. u8 *pos;
  4349. size_t i;
  4350. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  4351. genlmsg_attrlen(gnlh, 0), NULL);
  4352. if (!tb[NL80211_ATTR_BSS])
  4353. return NL_SKIP;
  4354. if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
  4355. bss_policy))
  4356. return NL_SKIP;
  4357. if (bss[NL80211_BSS_STATUS]) {
  4358. enum nl80211_bss_status status;
  4359. status = nla_get_u32(bss[NL80211_BSS_STATUS]);
  4360. if (status == NL80211_BSS_STATUS_ASSOCIATED &&
  4361. bss[NL80211_BSS_FREQUENCY]) {
  4362. _arg->assoc_freq =
  4363. nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
  4364. wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
  4365. _arg->assoc_freq);
  4366. }
  4367. if (status == NL80211_BSS_STATUS_ASSOCIATED &&
  4368. bss[NL80211_BSS_BSSID]) {
  4369. os_memcpy(_arg->assoc_bssid,
  4370. nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
  4371. wpa_printf(MSG_DEBUG, "nl80211: Associated with "
  4372. MACSTR, MAC2STR(_arg->assoc_bssid));
  4373. }
  4374. }
  4375. if (!res)
  4376. return NL_SKIP;
  4377. if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
  4378. ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  4379. ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  4380. } else {
  4381. ie = NULL;
  4382. ie_len = 0;
  4383. }
  4384. if (bss[NL80211_BSS_BEACON_IES]) {
  4385. beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
  4386. beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
  4387. } else {
  4388. beacon_ie = NULL;
  4389. beacon_ie_len = 0;
  4390. }
  4391. if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
  4392. ie ? ie_len : beacon_ie_len))
  4393. return NL_SKIP;
  4394. r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
  4395. if (r == NULL)
  4396. return NL_SKIP;
  4397. if (bss[NL80211_BSS_BSSID])
  4398. os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
  4399. ETH_ALEN);
  4400. if (bss[NL80211_BSS_FREQUENCY])
  4401. r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
  4402. if (bss[NL80211_BSS_BEACON_INTERVAL])
  4403. r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
  4404. if (bss[NL80211_BSS_CAPABILITY])
  4405. r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
  4406. r->flags |= WPA_SCAN_NOISE_INVALID;
  4407. if (bss[NL80211_BSS_SIGNAL_MBM]) {
  4408. r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
  4409. r->level /= 100; /* mBm to dBm */
  4410. r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
  4411. } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
  4412. r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
  4413. r->flags |= WPA_SCAN_QUAL_INVALID;
  4414. } else
  4415. r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
  4416. if (bss[NL80211_BSS_TSF])
  4417. r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
  4418. if (bss[NL80211_BSS_SEEN_MS_AGO])
  4419. r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
  4420. r->ie_len = ie_len;
  4421. pos = (u8 *) (r + 1);
  4422. if (ie) {
  4423. os_memcpy(pos, ie, ie_len);
  4424. pos += ie_len;
  4425. }
  4426. r->beacon_ie_len = beacon_ie_len;
  4427. if (beacon_ie)
  4428. os_memcpy(pos, beacon_ie, beacon_ie_len);
  4429. if (bss[NL80211_BSS_STATUS]) {
  4430. enum nl80211_bss_status status;
  4431. status = nla_get_u32(bss[NL80211_BSS_STATUS]);
  4432. switch (status) {
  4433. case NL80211_BSS_STATUS_AUTHENTICATED:
  4434. r->flags |= WPA_SCAN_AUTHENTICATED;
  4435. break;
  4436. case NL80211_BSS_STATUS_ASSOCIATED:
  4437. r->flags |= WPA_SCAN_ASSOCIATED;
  4438. break;
  4439. default:
  4440. break;
  4441. }
  4442. }
  4443. /*
  4444. * cfg80211 maintains separate BSS table entries for APs if the same
  4445. * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
  4446. * not use frequency as a separate key in the BSS table, so filter out
  4447. * duplicated entries. Prefer associated BSS entry in such a case in
  4448. * order to get the correct frequency into the BSS table. Similarly,
  4449. * prefer newer entries over older.
  4450. */
  4451. for (i = 0; i < res->num; i++) {
  4452. const u8 *s1, *s2;
  4453. if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
  4454. continue;
  4455. s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
  4456. res->res[i]->ie_len, WLAN_EID_SSID);
  4457. s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
  4458. if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
  4459. os_memcmp(s1, s2, 2 + s1[1]) != 0)
  4460. continue;
  4461. /* Same BSSID,SSID was already included in scan results */
  4462. wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
  4463. "for " MACSTR, MAC2STR(r->bssid));
  4464. if (((r->flags & WPA_SCAN_ASSOCIATED) &&
  4465. !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) ||
  4466. r->age < res->res[i]->age) {
  4467. os_free(res->res[i]);
  4468. res->res[i] = r;
  4469. } else
  4470. os_free(r);
  4471. return NL_SKIP;
  4472. }
  4473. tmp = os_realloc_array(res->res, res->num + 1,
  4474. sizeof(struct wpa_scan_res *));
  4475. if (tmp == NULL) {
  4476. os_free(r);
  4477. return NL_SKIP;
  4478. }
  4479. tmp[res->num++] = r;
  4480. res->res = tmp;
  4481. return NL_SKIP;
  4482. }
  4483. static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
  4484. const u8 *addr)
  4485. {
  4486. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  4487. wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
  4488. "mismatch (" MACSTR ")", MAC2STR(addr));
  4489. wpa_driver_nl80211_mlme(drv, addr,
  4490. NL80211_CMD_DEAUTHENTICATE,
  4491. WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
  4492. }
  4493. }
  4494. static void wpa_driver_nl80211_check_bss_status(
  4495. struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
  4496. {
  4497. size_t i;
  4498. for (i = 0; i < res->num; i++) {
  4499. struct wpa_scan_res *r = res->res[i];
  4500. if (r->flags & WPA_SCAN_AUTHENTICATED) {
  4501. wpa_printf(MSG_DEBUG, "nl80211: Scan results "
  4502. "indicates BSS status with " MACSTR
  4503. " as authenticated",
  4504. MAC2STR(r->bssid));
  4505. if (is_sta_interface(drv->nlmode) &&
  4506. os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
  4507. os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
  4508. 0) {
  4509. wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
  4510. " in local state (auth=" MACSTR
  4511. " assoc=" MACSTR ")",
  4512. MAC2STR(drv->auth_bssid),
  4513. MAC2STR(drv->bssid));
  4514. clear_state_mismatch(drv, r->bssid);
  4515. }
  4516. }
  4517. if (r->flags & WPA_SCAN_ASSOCIATED) {
  4518. wpa_printf(MSG_DEBUG, "nl80211: Scan results "
  4519. "indicate BSS status with " MACSTR
  4520. " as associated",
  4521. MAC2STR(r->bssid));
  4522. if (is_sta_interface(drv->nlmode) &&
  4523. !drv->associated) {
  4524. wpa_printf(MSG_DEBUG, "nl80211: Local state "
  4525. "(not associated) does not match "
  4526. "with BSS state");
  4527. clear_state_mismatch(drv, r->bssid);
  4528. } else if (is_sta_interface(drv->nlmode) &&
  4529. os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
  4530. 0) {
  4531. wpa_printf(MSG_DEBUG, "nl80211: Local state "
  4532. "(associated with " MACSTR ") does "
  4533. "not match with BSS state",
  4534. MAC2STR(drv->bssid));
  4535. clear_state_mismatch(drv, r->bssid);
  4536. clear_state_mismatch(drv, drv->bssid);
  4537. }
  4538. }
  4539. }
  4540. }
  4541. static struct wpa_scan_results *
  4542. nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
  4543. {
  4544. struct nl_msg *msg;
  4545. struct wpa_scan_results *res;
  4546. int ret;
  4547. struct nl80211_bss_info_arg arg;
  4548. res = os_zalloc(sizeof(*res));
  4549. if (res == NULL)
  4550. return NULL;
  4551. msg = nlmsg_alloc();
  4552. if (!msg)
  4553. goto nla_put_failure;
  4554. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
  4555. if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
  4556. goto nla_put_failure;
  4557. arg.drv = drv;
  4558. arg.res = res;
  4559. ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
  4560. msg = NULL;
  4561. if (ret == 0) {
  4562. wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
  4563. "BSSes)", (unsigned long) res->num);
  4564. nl80211_get_noise_for_scan_results(drv, res);
  4565. return res;
  4566. }
  4567. wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
  4568. "(%s)", ret, strerror(-ret));
  4569. nla_put_failure:
  4570. nlmsg_free(msg);
  4571. wpa_scan_results_free(res);
  4572. return NULL;
  4573. }
  4574. /**
  4575. * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
  4576. * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
  4577. * Returns: Scan results on success, -1 on failure
  4578. */
  4579. static struct wpa_scan_results *
  4580. wpa_driver_nl80211_get_scan_results(void *priv)
  4581. {
  4582. struct i802_bss *bss = priv;
  4583. struct wpa_driver_nl80211_data *drv = bss->drv;
  4584. struct wpa_scan_results *res;
  4585. res = nl80211_get_scan_results(drv);
  4586. if (res)
  4587. wpa_driver_nl80211_check_bss_status(drv, res);
  4588. return res;
  4589. }
  4590. static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
  4591. {
  4592. struct wpa_scan_results *res;
  4593. size_t i;
  4594. res = nl80211_get_scan_results(drv);
  4595. if (res == NULL) {
  4596. wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
  4597. return;
  4598. }
  4599. wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
  4600. for (i = 0; i < res->num; i++) {
  4601. struct wpa_scan_res *r = res->res[i];
  4602. wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
  4603. (int) i, (int) res->num, MAC2STR(r->bssid),
  4604. r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
  4605. r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
  4606. }
  4607. wpa_scan_results_free(res);
  4608. }
  4609. static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
  4610. {
  4611. switch (alg) {
  4612. case WPA_ALG_WEP:
  4613. if (key_len == 5)
  4614. return WLAN_CIPHER_SUITE_WEP40;
  4615. return WLAN_CIPHER_SUITE_WEP104;
  4616. case WPA_ALG_TKIP:
  4617. return WLAN_CIPHER_SUITE_TKIP;
  4618. case WPA_ALG_CCMP:
  4619. return WLAN_CIPHER_SUITE_CCMP;
  4620. case WPA_ALG_GCMP:
  4621. return WLAN_CIPHER_SUITE_GCMP;
  4622. case WPA_ALG_CCMP_256:
  4623. return WLAN_CIPHER_SUITE_CCMP_256;
  4624. case WPA_ALG_GCMP_256:
  4625. return WLAN_CIPHER_SUITE_GCMP_256;
  4626. case WPA_ALG_IGTK:
  4627. return WLAN_CIPHER_SUITE_AES_CMAC;
  4628. case WPA_ALG_BIP_GMAC_128:
  4629. return WLAN_CIPHER_SUITE_BIP_GMAC_128;
  4630. case WPA_ALG_BIP_GMAC_256:
  4631. return WLAN_CIPHER_SUITE_BIP_GMAC_256;
  4632. case WPA_ALG_BIP_CMAC_256:
  4633. return WLAN_CIPHER_SUITE_BIP_CMAC_256;
  4634. case WPA_ALG_SMS4:
  4635. return WLAN_CIPHER_SUITE_SMS4;
  4636. case WPA_ALG_KRK:
  4637. return WLAN_CIPHER_SUITE_KRK;
  4638. case WPA_ALG_NONE:
  4639. case WPA_ALG_PMK:
  4640. wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
  4641. alg);
  4642. return 0;
  4643. }
  4644. wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
  4645. alg);
  4646. return 0;
  4647. }
  4648. static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
  4649. {
  4650. switch (cipher) {
  4651. case WPA_CIPHER_CCMP_256:
  4652. return WLAN_CIPHER_SUITE_CCMP_256;
  4653. case WPA_CIPHER_GCMP_256:
  4654. return WLAN_CIPHER_SUITE_GCMP_256;
  4655. case WPA_CIPHER_CCMP:
  4656. return WLAN_CIPHER_SUITE_CCMP;
  4657. case WPA_CIPHER_GCMP:
  4658. return WLAN_CIPHER_SUITE_GCMP;
  4659. case WPA_CIPHER_TKIP:
  4660. return WLAN_CIPHER_SUITE_TKIP;
  4661. case WPA_CIPHER_WEP104:
  4662. return WLAN_CIPHER_SUITE_WEP104;
  4663. case WPA_CIPHER_WEP40:
  4664. return WLAN_CIPHER_SUITE_WEP40;
  4665. case WPA_CIPHER_GTK_NOT_USED:
  4666. return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
  4667. }
  4668. return 0;
  4669. }
  4670. static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
  4671. int max_suites)
  4672. {
  4673. int num_suites = 0;
  4674. if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
  4675. suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
  4676. if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
  4677. suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
  4678. if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
  4679. suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
  4680. if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
  4681. suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
  4682. if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
  4683. suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
  4684. if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
  4685. suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
  4686. if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
  4687. suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
  4688. return num_suites;
  4689. }
  4690. static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
  4691. enum wpa_alg alg, const u8 *addr,
  4692. int key_idx, int set_tx,
  4693. const u8 *seq, size_t seq_len,
  4694. const u8 *key, size_t key_len)
  4695. {
  4696. struct wpa_driver_nl80211_data *drv = bss->drv;
  4697. int ifindex;
  4698. struct nl_msg *msg;
  4699. int ret;
  4700. int tdls = 0;
  4701. /* Ignore for P2P Device */
  4702. if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
  4703. return 0;
  4704. ifindex = if_nametoindex(ifname);
  4705. wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
  4706. "set_tx=%d seq_len=%lu key_len=%lu",
  4707. __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
  4708. (unsigned long) seq_len, (unsigned long) key_len);
  4709. #ifdef CONFIG_TDLS
  4710. if (key_idx == -1) {
  4711. key_idx = 0;
  4712. tdls = 1;
  4713. }
  4714. #endif /* CONFIG_TDLS */
  4715. msg = nlmsg_alloc();
  4716. if (!msg)
  4717. return -ENOMEM;
  4718. if (alg == WPA_ALG_NONE) {
  4719. nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
  4720. } else {
  4721. nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
  4722. NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
  4723. wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
  4724. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  4725. wpa_alg_to_cipher_suite(alg, key_len));
  4726. }
  4727. if (seq && seq_len) {
  4728. NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
  4729. wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
  4730. }
  4731. if (addr && !is_broadcast_ether_addr(addr)) {
  4732. wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
  4733. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  4734. if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
  4735. wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
  4736. NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
  4737. NL80211_KEYTYPE_GROUP);
  4738. }
  4739. } else if (addr && is_broadcast_ether_addr(addr)) {
  4740. struct nlattr *types;
  4741. wpa_printf(MSG_DEBUG, " broadcast key");
  4742. types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
  4743. if (!types)
  4744. goto nla_put_failure;
  4745. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
  4746. nla_nest_end(msg, types);
  4747. }
  4748. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
  4749. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  4750. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4751. if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
  4752. ret = 0;
  4753. if (ret)
  4754. wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
  4755. ret, strerror(-ret));
  4756. /*
  4757. * If we failed or don't need to set the default TX key (below),
  4758. * we're done here.
  4759. */
  4760. if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
  4761. return ret;
  4762. if (is_ap_interface(drv->nlmode) && addr &&
  4763. !is_broadcast_ether_addr(addr))
  4764. return ret;
  4765. msg = nlmsg_alloc();
  4766. if (!msg)
  4767. return -ENOMEM;
  4768. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
  4769. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
  4770. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  4771. if (alg == WPA_ALG_IGTK)
  4772. NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
  4773. else
  4774. NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
  4775. if (addr && is_broadcast_ether_addr(addr)) {
  4776. struct nlattr *types;
  4777. types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
  4778. if (!types)
  4779. goto nla_put_failure;
  4780. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
  4781. nla_nest_end(msg, types);
  4782. } else if (addr) {
  4783. struct nlattr *types;
  4784. types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
  4785. if (!types)
  4786. goto nla_put_failure;
  4787. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
  4788. nla_nest_end(msg, types);
  4789. }
  4790. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4791. if (ret == -ENOENT)
  4792. ret = 0;
  4793. if (ret)
  4794. wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
  4795. "err=%d %s)", ret, strerror(-ret));
  4796. return ret;
  4797. nla_put_failure:
  4798. nlmsg_free(msg);
  4799. return -ENOBUFS;
  4800. }
  4801. static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
  4802. int key_idx, int defkey,
  4803. const u8 *seq, size_t seq_len,
  4804. const u8 *key, size_t key_len)
  4805. {
  4806. struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
  4807. if (!key_attr)
  4808. return -1;
  4809. if (defkey && alg == WPA_ALG_IGTK)
  4810. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
  4811. else if (defkey)
  4812. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
  4813. NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
  4814. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  4815. wpa_alg_to_cipher_suite(alg, key_len));
  4816. if (seq && seq_len)
  4817. NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
  4818. NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
  4819. nla_nest_end(msg, key_attr);
  4820. return 0;
  4821. nla_put_failure:
  4822. return -1;
  4823. }
  4824. static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
  4825. struct nl_msg *msg)
  4826. {
  4827. int i, privacy = 0;
  4828. struct nlattr *nl_keys, *nl_key;
  4829. for (i = 0; i < 4; i++) {
  4830. if (!params->wep_key[i])
  4831. continue;
  4832. privacy = 1;
  4833. break;
  4834. }
  4835. if (params->wps == WPS_MODE_PRIVACY)
  4836. privacy = 1;
  4837. if (params->pairwise_suite &&
  4838. params->pairwise_suite != WPA_CIPHER_NONE)
  4839. privacy = 1;
  4840. if (!privacy)
  4841. return 0;
  4842. NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
  4843. nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
  4844. if (!nl_keys)
  4845. goto nla_put_failure;
  4846. for (i = 0; i < 4; i++) {
  4847. if (!params->wep_key[i])
  4848. continue;
  4849. nl_key = nla_nest_start(msg, i);
  4850. if (!nl_key)
  4851. goto nla_put_failure;
  4852. NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
  4853. params->wep_key[i]);
  4854. if (params->wep_key_len[i] == 5)
  4855. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  4856. WLAN_CIPHER_SUITE_WEP40);
  4857. else
  4858. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  4859. WLAN_CIPHER_SUITE_WEP104);
  4860. NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
  4861. if (i == params->wep_tx_keyidx)
  4862. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
  4863. nla_nest_end(msg, nl_key);
  4864. }
  4865. nla_nest_end(msg, nl_keys);
  4866. return 0;
  4867. nla_put_failure:
  4868. return -ENOBUFS;
  4869. }
  4870. static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
  4871. const u8 *addr, int cmd, u16 reason_code,
  4872. int local_state_change)
  4873. {
  4874. int ret = -1;
  4875. struct nl_msg *msg;
  4876. msg = nlmsg_alloc();
  4877. if (!msg)
  4878. return -1;
  4879. nl80211_cmd(drv, msg, 0, cmd);
  4880. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  4881. NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
  4882. if (addr)
  4883. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  4884. if (local_state_change)
  4885. NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
  4886. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4887. msg = NULL;
  4888. if (ret) {
  4889. wpa_dbg(drv->ctx, MSG_DEBUG,
  4890. "nl80211: MLME command failed: reason=%u ret=%d (%s)",
  4891. reason_code, ret, strerror(-ret));
  4892. goto nla_put_failure;
  4893. }
  4894. ret = 0;
  4895. nla_put_failure:
  4896. nlmsg_free(msg);
  4897. return ret;
  4898. }
  4899. static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
  4900. int reason_code)
  4901. {
  4902. int ret;
  4903. wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
  4904. nl80211_mark_disconnected(drv);
  4905. /* Disconnect command doesn't need BSSID - it uses cached value */
  4906. ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
  4907. reason_code, 0);
  4908. /*
  4909. * For locally generated disconnect, supplicant already generates a
  4910. * DEAUTH event, so ignore the event from NL80211.
  4911. */
  4912. drv->ignore_next_local_disconnect = ret == 0;
  4913. return ret;
  4914. }
  4915. static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
  4916. const u8 *addr, int reason_code)
  4917. {
  4918. struct wpa_driver_nl80211_data *drv = bss->drv;
  4919. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
  4920. return wpa_driver_nl80211_disconnect(drv, reason_code);
  4921. wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
  4922. __func__, MAC2STR(addr), reason_code);
  4923. nl80211_mark_disconnected(drv);
  4924. if (drv->nlmode == NL80211_IFTYPE_ADHOC)
  4925. return nl80211_leave_ibss(drv);
  4926. return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
  4927. reason_code, 0);
  4928. }
  4929. static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
  4930. struct wpa_driver_auth_params *params)
  4931. {
  4932. int i;
  4933. drv->auth_freq = params->freq;
  4934. drv->auth_alg = params->auth_alg;
  4935. drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
  4936. drv->auth_local_state_change = params->local_state_change;
  4937. drv->auth_p2p = params->p2p;
  4938. if (params->bssid)
  4939. os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
  4940. else
  4941. os_memset(drv->auth_bssid_, 0, ETH_ALEN);
  4942. if (params->ssid) {
  4943. os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
  4944. drv->auth_ssid_len = params->ssid_len;
  4945. } else
  4946. drv->auth_ssid_len = 0;
  4947. os_free(drv->auth_ie);
  4948. drv->auth_ie = NULL;
  4949. drv->auth_ie_len = 0;
  4950. if (params->ie) {
  4951. drv->auth_ie = os_malloc(params->ie_len);
  4952. if (drv->auth_ie) {
  4953. os_memcpy(drv->auth_ie, params->ie, params->ie_len);
  4954. drv->auth_ie_len = params->ie_len;
  4955. }
  4956. }
  4957. for (i = 0; i < 4; i++) {
  4958. if (params->wep_key[i] && params->wep_key_len[i] &&
  4959. params->wep_key_len[i] <= 16) {
  4960. os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
  4961. params->wep_key_len[i]);
  4962. drv->auth_wep_key_len[i] = params->wep_key_len[i];
  4963. } else
  4964. drv->auth_wep_key_len[i] = 0;
  4965. }
  4966. }
  4967. static int wpa_driver_nl80211_authenticate(
  4968. struct i802_bss *bss, struct wpa_driver_auth_params *params)
  4969. {
  4970. struct wpa_driver_nl80211_data *drv = bss->drv;
  4971. int ret = -1, i;
  4972. struct nl_msg *msg;
  4973. enum nl80211_auth_type type;
  4974. enum nl80211_iftype nlmode;
  4975. int count = 0;
  4976. int is_retry;
  4977. is_retry = drv->retry_auth;
  4978. drv->retry_auth = 0;
  4979. drv->ignore_deauth_event = 0;
  4980. nl80211_mark_disconnected(drv);
  4981. os_memset(drv->auth_bssid, 0, ETH_ALEN);
  4982. if (params->bssid)
  4983. os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
  4984. else
  4985. os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
  4986. /* FIX: IBSS mode */
  4987. nlmode = params->p2p ?
  4988. NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
  4989. if (drv->nlmode != nlmode &&
  4990. wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
  4991. return -1;
  4992. retry:
  4993. msg = nlmsg_alloc();
  4994. if (!msg)
  4995. return -1;
  4996. wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
  4997. drv->ifindex);
  4998. nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
  4999. for (i = 0; i < 4; i++) {
  5000. if (!params->wep_key[i])
  5001. continue;
  5002. wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
  5003. NULL, i,
  5004. i == params->wep_tx_keyidx, NULL, 0,
  5005. params->wep_key[i],
  5006. params->wep_key_len[i]);
  5007. if (params->wep_tx_keyidx != i)
  5008. continue;
  5009. if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
  5010. params->wep_key[i], params->wep_key_len[i])) {
  5011. nlmsg_free(msg);
  5012. return -1;
  5013. }
  5014. }
  5015. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  5016. if (params->bssid) {
  5017. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  5018. MAC2STR(params->bssid));
  5019. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  5020. }
  5021. if (params->freq) {
  5022. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  5023. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  5024. }
  5025. if (params->ssid) {
  5026. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  5027. params->ssid, params->ssid_len);
  5028. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  5029. params->ssid);
  5030. }
  5031. wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
  5032. if (params->ie)
  5033. NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
  5034. if (params->sae_data) {
  5035. wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
  5036. params->sae_data_len);
  5037. NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
  5038. params->sae_data);
  5039. }
  5040. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  5041. type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  5042. else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  5043. type = NL80211_AUTHTYPE_SHARED_KEY;
  5044. else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  5045. type = NL80211_AUTHTYPE_NETWORK_EAP;
  5046. else if (params->auth_alg & WPA_AUTH_ALG_FT)
  5047. type = NL80211_AUTHTYPE_FT;
  5048. else if (params->auth_alg & WPA_AUTH_ALG_SAE)
  5049. type = NL80211_AUTHTYPE_SAE;
  5050. else
  5051. goto nla_put_failure;
  5052. wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
  5053. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
  5054. if (params->local_state_change) {
  5055. wpa_printf(MSG_DEBUG, " * Local state change only");
  5056. NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
  5057. }
  5058. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5059. msg = NULL;
  5060. if (ret) {
  5061. wpa_dbg(drv->ctx, MSG_DEBUG,
  5062. "nl80211: MLME command failed (auth): ret=%d (%s)",
  5063. ret, strerror(-ret));
  5064. count++;
  5065. if (ret == -EALREADY && count == 1 && params->bssid &&
  5066. !params->local_state_change) {
  5067. /*
  5068. * mac80211 does not currently accept new
  5069. * authentication if we are already authenticated. As a
  5070. * workaround, force deauthentication and try again.
  5071. */
  5072. wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
  5073. "after forced deauthentication");
  5074. drv->ignore_deauth_event = 1;
  5075. wpa_driver_nl80211_deauthenticate(
  5076. bss, params->bssid,
  5077. WLAN_REASON_PREV_AUTH_NOT_VALID);
  5078. nlmsg_free(msg);
  5079. goto retry;
  5080. }
  5081. if (ret == -ENOENT && params->freq && !is_retry) {
  5082. /*
  5083. * cfg80211 has likely expired the BSS entry even
  5084. * though it was previously available in our internal
  5085. * BSS table. To recover quickly, start a single
  5086. * channel scan on the specified channel.
  5087. */
  5088. struct wpa_driver_scan_params scan;
  5089. int freqs[2];
  5090. os_memset(&scan, 0, sizeof(scan));
  5091. scan.num_ssids = 1;
  5092. if (params->ssid) {
  5093. scan.ssids[0].ssid = params->ssid;
  5094. scan.ssids[0].ssid_len = params->ssid_len;
  5095. }
  5096. freqs[0] = params->freq;
  5097. freqs[1] = 0;
  5098. scan.freqs = freqs;
  5099. wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
  5100. "channel scan to refresh cfg80211 BSS "
  5101. "entry");
  5102. ret = wpa_driver_nl80211_scan(bss, &scan);
  5103. if (ret == 0) {
  5104. nl80211_copy_auth_params(drv, params);
  5105. drv->scan_for_auth = 1;
  5106. }
  5107. } else if (is_retry) {
  5108. /*
  5109. * Need to indicate this with an event since the return
  5110. * value from the retry is not delivered to core code.
  5111. */
  5112. union wpa_event_data event;
  5113. wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
  5114. "failed");
  5115. os_memset(&event, 0, sizeof(event));
  5116. os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
  5117. ETH_ALEN);
  5118. wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
  5119. &event);
  5120. }
  5121. goto nla_put_failure;
  5122. }
  5123. ret = 0;
  5124. wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
  5125. "successfully");
  5126. nla_put_failure:
  5127. nlmsg_free(msg);
  5128. return ret;
  5129. }
  5130. static int wpa_driver_nl80211_authenticate_retry(
  5131. struct wpa_driver_nl80211_data *drv)
  5132. {
  5133. struct wpa_driver_auth_params params;
  5134. struct i802_bss *bss = drv->first_bss;
  5135. int i;
  5136. wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
  5137. os_memset(&params, 0, sizeof(params));
  5138. params.freq = drv->auth_freq;
  5139. params.auth_alg = drv->auth_alg;
  5140. params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
  5141. params.local_state_change = drv->auth_local_state_change;
  5142. params.p2p = drv->auth_p2p;
  5143. if (!is_zero_ether_addr(drv->auth_bssid_))
  5144. params.bssid = drv->auth_bssid_;
  5145. if (drv->auth_ssid_len) {
  5146. params.ssid = drv->auth_ssid;
  5147. params.ssid_len = drv->auth_ssid_len;
  5148. }
  5149. params.ie = drv->auth_ie;
  5150. params.ie_len = drv->auth_ie_len;
  5151. for (i = 0; i < 4; i++) {
  5152. if (drv->auth_wep_key_len[i]) {
  5153. params.wep_key[i] = drv->auth_wep_key[i];
  5154. params.wep_key_len[i] = drv->auth_wep_key_len[i];
  5155. }
  5156. }
  5157. drv->retry_auth = 1;
  5158. return wpa_driver_nl80211_authenticate(bss, &params);
  5159. }
  5160. struct phy_info_arg {
  5161. u16 *num_modes;
  5162. struct hostapd_hw_modes *modes;
  5163. int last_mode, last_chan_idx;
  5164. };
  5165. static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
  5166. struct nlattr *ampdu_factor,
  5167. struct nlattr *ampdu_density,
  5168. struct nlattr *mcs_set)
  5169. {
  5170. if (capa)
  5171. mode->ht_capab = nla_get_u16(capa);
  5172. if (ampdu_factor)
  5173. mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
  5174. if (ampdu_density)
  5175. mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
  5176. if (mcs_set && nla_len(mcs_set) >= 16) {
  5177. u8 *mcs;
  5178. mcs = nla_data(mcs_set);
  5179. os_memcpy(mode->mcs_set, mcs, 16);
  5180. }
  5181. }
  5182. static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
  5183. struct nlattr *capa,
  5184. struct nlattr *mcs_set)
  5185. {
  5186. if (capa)
  5187. mode->vht_capab = nla_get_u32(capa);
  5188. if (mcs_set && nla_len(mcs_set) >= 8) {
  5189. u8 *mcs;
  5190. mcs = nla_data(mcs_set);
  5191. os_memcpy(mode->vht_mcs_set, mcs, 8);
  5192. }
  5193. }
  5194. static void phy_info_freq(struct hostapd_hw_modes *mode,
  5195. struct hostapd_channel_data *chan,
  5196. struct nlattr *tb_freq[])
  5197. {
  5198. u8 channel;
  5199. chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
  5200. chan->flag = 0;
  5201. if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
  5202. chan->chan = channel;
  5203. if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
  5204. chan->flag |= HOSTAPD_CHAN_DISABLED;
  5205. if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
  5206. chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN | HOSTAPD_CHAN_NO_IBSS;
  5207. if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
  5208. chan->flag |= HOSTAPD_CHAN_RADAR;
  5209. if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
  5210. enum nl80211_dfs_state state =
  5211. nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
  5212. switch (state) {
  5213. case NL80211_DFS_USABLE:
  5214. chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
  5215. break;
  5216. case NL80211_DFS_AVAILABLE:
  5217. chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
  5218. break;
  5219. case NL80211_DFS_UNAVAILABLE:
  5220. chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
  5221. break;
  5222. }
  5223. }
  5224. }
  5225. static int phy_info_freqs(struct phy_info_arg *phy_info,
  5226. struct hostapd_hw_modes *mode, struct nlattr *tb)
  5227. {
  5228. static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
  5229. [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
  5230. [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
  5231. [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
  5232. [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
  5233. [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
  5234. [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
  5235. };
  5236. int new_channels = 0;
  5237. struct hostapd_channel_data *channel;
  5238. struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
  5239. struct nlattr *nl_freq;
  5240. int rem_freq, idx;
  5241. if (tb == NULL)
  5242. return NL_OK;
  5243. nla_for_each_nested(nl_freq, tb, rem_freq) {
  5244. nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
  5245. nla_data(nl_freq), nla_len(nl_freq), freq_policy);
  5246. if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
  5247. continue;
  5248. new_channels++;
  5249. }
  5250. channel = os_realloc_array(mode->channels,
  5251. mode->num_channels + new_channels,
  5252. sizeof(struct hostapd_channel_data));
  5253. if (!channel)
  5254. return NL_SKIP;
  5255. mode->channels = channel;
  5256. mode->num_channels += new_channels;
  5257. idx = phy_info->last_chan_idx;
  5258. nla_for_each_nested(nl_freq, tb, rem_freq) {
  5259. nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
  5260. nla_data(nl_freq), nla_len(nl_freq), freq_policy);
  5261. if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
  5262. continue;
  5263. phy_info_freq(mode, &mode->channels[idx], tb_freq);
  5264. idx++;
  5265. }
  5266. phy_info->last_chan_idx = idx;
  5267. return NL_OK;
  5268. }
  5269. static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
  5270. {
  5271. static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
  5272. [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
  5273. [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
  5274. { .type = NLA_FLAG },
  5275. };
  5276. struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
  5277. struct nlattr *nl_rate;
  5278. int rem_rate, idx;
  5279. if (tb == NULL)
  5280. return NL_OK;
  5281. nla_for_each_nested(nl_rate, tb, rem_rate) {
  5282. nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
  5283. nla_data(nl_rate), nla_len(nl_rate),
  5284. rate_policy);
  5285. if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
  5286. continue;
  5287. mode->num_rates++;
  5288. }
  5289. mode->rates = os_calloc(mode->num_rates, sizeof(int));
  5290. if (!mode->rates)
  5291. return NL_SKIP;
  5292. idx = 0;
  5293. nla_for_each_nested(nl_rate, tb, rem_rate) {
  5294. nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
  5295. nla_data(nl_rate), nla_len(nl_rate),
  5296. rate_policy);
  5297. if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
  5298. continue;
  5299. mode->rates[idx] = nla_get_u32(
  5300. tb_rate[NL80211_BITRATE_ATTR_RATE]);
  5301. idx++;
  5302. }
  5303. return NL_OK;
  5304. }
  5305. static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
  5306. {
  5307. struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
  5308. struct hostapd_hw_modes *mode;
  5309. int ret;
  5310. if (phy_info->last_mode != nl_band->nla_type) {
  5311. mode = os_realloc_array(phy_info->modes,
  5312. *phy_info->num_modes + 1,
  5313. sizeof(*mode));
  5314. if (!mode)
  5315. return NL_SKIP;
  5316. phy_info->modes = mode;
  5317. mode = &phy_info->modes[*(phy_info->num_modes)];
  5318. os_memset(mode, 0, sizeof(*mode));
  5319. mode->mode = NUM_HOSTAPD_MODES;
  5320. mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
  5321. HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
  5322. /*
  5323. * Unsupported VHT MCS stream is defined as value 3, so the VHT
  5324. * MCS RX/TX map must be initialized with 0xffff to mark all 8
  5325. * possible streams as unsupported. This will be overridden if
  5326. * driver advertises VHT support.
  5327. */
  5328. mode->vht_mcs_set[0] = 0xff;
  5329. mode->vht_mcs_set[1] = 0xff;
  5330. mode->vht_mcs_set[4] = 0xff;
  5331. mode->vht_mcs_set[5] = 0xff;
  5332. *(phy_info->num_modes) += 1;
  5333. phy_info->last_mode = nl_band->nla_type;
  5334. phy_info->last_chan_idx = 0;
  5335. } else
  5336. mode = &phy_info->modes[*(phy_info->num_modes) - 1];
  5337. nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
  5338. nla_len(nl_band), NULL);
  5339. phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
  5340. tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
  5341. tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
  5342. tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
  5343. phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
  5344. tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
  5345. ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
  5346. if (ret != NL_OK)
  5347. return ret;
  5348. ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
  5349. if (ret != NL_OK)
  5350. return ret;
  5351. return NL_OK;
  5352. }
  5353. static int phy_info_handler(struct nl_msg *msg, void *arg)
  5354. {
  5355. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  5356. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  5357. struct phy_info_arg *phy_info = arg;
  5358. struct nlattr *nl_band;
  5359. int rem_band;
  5360. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  5361. genlmsg_attrlen(gnlh, 0), NULL);
  5362. if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
  5363. return NL_SKIP;
  5364. nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
  5365. {
  5366. int res = phy_info_band(phy_info, nl_band);
  5367. if (res != NL_OK)
  5368. return res;
  5369. }
  5370. return NL_SKIP;
  5371. }
  5372. static struct hostapd_hw_modes *
  5373. wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
  5374. u16 *num_modes)
  5375. {
  5376. u16 m;
  5377. struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
  5378. int i, mode11g_idx = -1;
  5379. /* heuristic to set up modes */
  5380. for (m = 0; m < *num_modes; m++) {
  5381. if (!modes[m].num_channels)
  5382. continue;
  5383. if (modes[m].channels[0].freq < 4000) {
  5384. modes[m].mode = HOSTAPD_MODE_IEEE80211B;
  5385. for (i = 0; i < modes[m].num_rates; i++) {
  5386. if (modes[m].rates[i] > 200) {
  5387. modes[m].mode = HOSTAPD_MODE_IEEE80211G;
  5388. break;
  5389. }
  5390. }
  5391. } else if (modes[m].channels[0].freq > 50000)
  5392. modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
  5393. else
  5394. modes[m].mode = HOSTAPD_MODE_IEEE80211A;
  5395. }
  5396. /* If only 802.11g mode is included, use it to construct matching
  5397. * 802.11b mode data. */
  5398. for (m = 0; m < *num_modes; m++) {
  5399. if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
  5400. return modes; /* 802.11b already included */
  5401. if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
  5402. mode11g_idx = m;
  5403. }
  5404. if (mode11g_idx < 0)
  5405. return modes; /* 2.4 GHz band not supported at all */
  5406. nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
  5407. if (nmodes == NULL)
  5408. return modes; /* Could not add 802.11b mode */
  5409. mode = &nmodes[*num_modes];
  5410. os_memset(mode, 0, sizeof(*mode));
  5411. (*num_modes)++;
  5412. modes = nmodes;
  5413. mode->mode = HOSTAPD_MODE_IEEE80211B;
  5414. mode11g = &modes[mode11g_idx];
  5415. mode->num_channels = mode11g->num_channels;
  5416. mode->channels = os_malloc(mode11g->num_channels *
  5417. sizeof(struct hostapd_channel_data));
  5418. if (mode->channels == NULL) {
  5419. (*num_modes)--;
  5420. return modes; /* Could not add 802.11b mode */
  5421. }
  5422. os_memcpy(mode->channels, mode11g->channels,
  5423. mode11g->num_channels * sizeof(struct hostapd_channel_data));
  5424. mode->num_rates = 0;
  5425. mode->rates = os_malloc(4 * sizeof(int));
  5426. if (mode->rates == NULL) {
  5427. os_free(mode->channels);
  5428. (*num_modes)--;
  5429. return modes; /* Could not add 802.11b mode */
  5430. }
  5431. for (i = 0; i < mode11g->num_rates; i++) {
  5432. if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
  5433. mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
  5434. continue;
  5435. mode->rates[mode->num_rates] = mode11g->rates[i];
  5436. mode->num_rates++;
  5437. if (mode->num_rates == 4)
  5438. break;
  5439. }
  5440. if (mode->num_rates == 0) {
  5441. os_free(mode->channels);
  5442. os_free(mode->rates);
  5443. (*num_modes)--;
  5444. return modes; /* No 802.11b rates */
  5445. }
  5446. wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
  5447. "information");
  5448. return modes;
  5449. }
  5450. static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
  5451. int end)
  5452. {
  5453. int c;
  5454. for (c = 0; c < mode->num_channels; c++) {
  5455. struct hostapd_channel_data *chan = &mode->channels[c];
  5456. if (chan->freq - 10 >= start && chan->freq + 10 <= end)
  5457. chan->flag |= HOSTAPD_CHAN_HT40;
  5458. }
  5459. }
  5460. static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
  5461. int end)
  5462. {
  5463. int c;
  5464. for (c = 0; c < mode->num_channels; c++) {
  5465. struct hostapd_channel_data *chan = &mode->channels[c];
  5466. if (!(chan->flag & HOSTAPD_CHAN_HT40))
  5467. continue;
  5468. if (chan->freq - 30 >= start && chan->freq - 10 <= end)
  5469. chan->flag |= HOSTAPD_CHAN_HT40MINUS;
  5470. if (chan->freq + 10 >= start && chan->freq + 30 <= end)
  5471. chan->flag |= HOSTAPD_CHAN_HT40PLUS;
  5472. }
  5473. }
  5474. static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
  5475. struct phy_info_arg *results)
  5476. {
  5477. u16 m;
  5478. for (m = 0; m < *results->num_modes; m++) {
  5479. int c;
  5480. struct hostapd_hw_modes *mode = &results->modes[m];
  5481. for (c = 0; c < mode->num_channels; c++) {
  5482. struct hostapd_channel_data *chan = &mode->channels[c];
  5483. if ((u32) chan->freq - 10 >= start &&
  5484. (u32) chan->freq + 10 <= end)
  5485. chan->max_tx_power = max_eirp;
  5486. }
  5487. }
  5488. }
  5489. static void nl80211_reg_rule_ht40(u32 start, u32 end,
  5490. struct phy_info_arg *results)
  5491. {
  5492. u16 m;
  5493. for (m = 0; m < *results->num_modes; m++) {
  5494. if (!(results->modes[m].ht_capab &
  5495. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  5496. continue;
  5497. nl80211_set_ht40_mode(&results->modes[m], start, end);
  5498. }
  5499. }
  5500. static void nl80211_reg_rule_sec(struct nlattr *tb[],
  5501. struct phy_info_arg *results)
  5502. {
  5503. u32 start, end, max_bw;
  5504. u16 m;
  5505. if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
  5506. tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
  5507. tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
  5508. return;
  5509. start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
  5510. end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
  5511. max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
  5512. if (max_bw < 20)
  5513. return;
  5514. for (m = 0; m < *results->num_modes; m++) {
  5515. if (!(results->modes[m].ht_capab &
  5516. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  5517. continue;
  5518. nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
  5519. }
  5520. }
  5521. static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
  5522. int end)
  5523. {
  5524. int c;
  5525. for (c = 0; c < mode->num_channels; c++) {
  5526. struct hostapd_channel_data *chan = &mode->channels[c];
  5527. if (chan->freq - 10 >= start && chan->freq + 70 <= end)
  5528. chan->flag |= HOSTAPD_CHAN_VHT_10_70;
  5529. if (chan->freq - 30 >= start && chan->freq + 50 <= end)
  5530. chan->flag |= HOSTAPD_CHAN_VHT_30_50;
  5531. if (chan->freq - 50 >= start && chan->freq + 30 <= end)
  5532. chan->flag |= HOSTAPD_CHAN_VHT_50_30;
  5533. if (chan->freq - 70 >= start && chan->freq + 10 <= end)
  5534. chan->flag |= HOSTAPD_CHAN_VHT_70_10;
  5535. }
  5536. }
  5537. static void nl80211_reg_rule_vht(struct nlattr *tb[],
  5538. struct phy_info_arg *results)
  5539. {
  5540. u32 start, end, max_bw;
  5541. u16 m;
  5542. if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
  5543. tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
  5544. tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
  5545. return;
  5546. start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
  5547. end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
  5548. max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
  5549. if (max_bw < 80)
  5550. return;
  5551. for (m = 0; m < *results->num_modes; m++) {
  5552. if (!(results->modes[m].ht_capab &
  5553. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  5554. continue;
  5555. /* TODO: use a real VHT support indication */
  5556. if (!results->modes[m].vht_capab)
  5557. continue;
  5558. nl80211_set_vht_mode(&results->modes[m], start, end);
  5559. }
  5560. }
  5561. static const char * dfs_domain_name(enum nl80211_dfs_regions region)
  5562. {
  5563. switch (region) {
  5564. case NL80211_DFS_UNSET:
  5565. return "DFS-UNSET";
  5566. case NL80211_DFS_FCC:
  5567. return "DFS-FCC";
  5568. case NL80211_DFS_ETSI:
  5569. return "DFS-ETSI";
  5570. case NL80211_DFS_JP:
  5571. return "DFS-JP";
  5572. default:
  5573. return "DFS-invalid";
  5574. }
  5575. }
  5576. static int nl80211_get_reg(struct nl_msg *msg, void *arg)
  5577. {
  5578. struct phy_info_arg *results = arg;
  5579. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  5580. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  5581. struct nlattr *nl_rule;
  5582. struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
  5583. int rem_rule;
  5584. static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
  5585. [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
  5586. [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
  5587. [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
  5588. [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
  5589. [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
  5590. [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
  5591. };
  5592. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  5593. genlmsg_attrlen(gnlh, 0), NULL);
  5594. if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
  5595. !tb_msg[NL80211_ATTR_REG_RULES]) {
  5596. wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
  5597. "available");
  5598. return NL_SKIP;
  5599. }
  5600. if (tb_msg[NL80211_ATTR_DFS_REGION]) {
  5601. enum nl80211_dfs_regions dfs_domain;
  5602. dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
  5603. wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
  5604. (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
  5605. dfs_domain_name(dfs_domain));
  5606. } else {
  5607. wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
  5608. (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
  5609. }
  5610. nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
  5611. {
  5612. u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
  5613. nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
  5614. nla_data(nl_rule), nla_len(nl_rule), reg_policy);
  5615. if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
  5616. tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
  5617. continue;
  5618. start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
  5619. end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
  5620. if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
  5621. max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
  5622. if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
  5623. max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
  5624. if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
  5625. flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
  5626. wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
  5627. start, end, max_bw, max_eirp,
  5628. flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
  5629. flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
  5630. flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
  5631. flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
  5632. "",
  5633. flags & NL80211_RRF_DFS ? " (DFS)" : "",
  5634. flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
  5635. flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
  5636. flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
  5637. if (max_bw >= 40)
  5638. nl80211_reg_rule_ht40(start, end, results);
  5639. if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
  5640. nl80211_reg_rule_max_eirp(start, end, max_eirp,
  5641. results);
  5642. }
  5643. nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
  5644. {
  5645. nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
  5646. nla_data(nl_rule), nla_len(nl_rule), reg_policy);
  5647. nl80211_reg_rule_sec(tb_rule, results);
  5648. }
  5649. nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
  5650. {
  5651. nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
  5652. nla_data(nl_rule), nla_len(nl_rule), reg_policy);
  5653. nl80211_reg_rule_vht(tb_rule, results);
  5654. }
  5655. return NL_SKIP;
  5656. }
  5657. static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
  5658. struct phy_info_arg *results)
  5659. {
  5660. struct nl_msg *msg;
  5661. msg = nlmsg_alloc();
  5662. if (!msg)
  5663. return -ENOMEM;
  5664. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
  5665. return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
  5666. }
  5667. static struct hostapd_hw_modes *
  5668. wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
  5669. {
  5670. u32 feat;
  5671. struct i802_bss *bss = priv;
  5672. struct wpa_driver_nl80211_data *drv = bss->drv;
  5673. struct nl_msg *msg;
  5674. struct phy_info_arg result = {
  5675. .num_modes = num_modes,
  5676. .modes = NULL,
  5677. .last_mode = -1,
  5678. };
  5679. *num_modes = 0;
  5680. *flags = 0;
  5681. msg = nlmsg_alloc();
  5682. if (!msg)
  5683. return NULL;
  5684. feat = get_nl80211_protocol_features(drv);
  5685. if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
  5686. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
  5687. else
  5688. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
  5689. NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
  5690. if (nl80211_set_iface_id(msg, bss) < 0)
  5691. goto nla_put_failure;
  5692. if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
  5693. nl80211_set_regulatory_flags(drv, &result);
  5694. return wpa_driver_nl80211_postprocess_modes(result.modes,
  5695. num_modes);
  5696. }
  5697. msg = NULL;
  5698. nla_put_failure:
  5699. nlmsg_free(msg);
  5700. return NULL;
  5701. }
  5702. static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
  5703. const void *data, size_t len,
  5704. int encrypt, int noack)
  5705. {
  5706. __u8 rtap_hdr[] = {
  5707. 0x00, 0x00, /* radiotap version */
  5708. 0x0e, 0x00, /* radiotap length */
  5709. 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
  5710. IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
  5711. 0x00, /* padding */
  5712. 0x00, 0x00, /* RX and TX flags to indicate that */
  5713. 0x00, 0x00, /* this is the injected frame directly */
  5714. };
  5715. struct iovec iov[2] = {
  5716. {
  5717. .iov_base = &rtap_hdr,
  5718. .iov_len = sizeof(rtap_hdr),
  5719. },
  5720. {
  5721. .iov_base = (void *) data,
  5722. .iov_len = len,
  5723. }
  5724. };
  5725. struct msghdr msg = {
  5726. .msg_name = NULL,
  5727. .msg_namelen = 0,
  5728. .msg_iov = iov,
  5729. .msg_iovlen = 2,
  5730. .msg_control = NULL,
  5731. .msg_controllen = 0,
  5732. .msg_flags = 0,
  5733. };
  5734. int res;
  5735. u16 txflags = 0;
  5736. if (encrypt)
  5737. rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
  5738. if (drv->monitor_sock < 0) {
  5739. wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
  5740. "for %s", __func__);
  5741. return -1;
  5742. }
  5743. if (noack)
  5744. txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
  5745. WPA_PUT_LE16(&rtap_hdr[12], txflags);
  5746. res = sendmsg(drv->monitor_sock, &msg, 0);
  5747. if (res < 0) {
  5748. wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
  5749. return -1;
  5750. }
  5751. return 0;
  5752. }
  5753. static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
  5754. const void *data, size_t len,
  5755. int encrypt, int noack,
  5756. unsigned int freq, int no_cck,
  5757. int offchanok, unsigned int wait_time)
  5758. {
  5759. struct wpa_driver_nl80211_data *drv = bss->drv;
  5760. u64 cookie;
  5761. int res;
  5762. if (freq == 0) {
  5763. wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
  5764. bss->freq);
  5765. freq = bss->freq;
  5766. }
  5767. if (drv->use_monitor) {
  5768. wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
  5769. freq, bss->freq);
  5770. return wpa_driver_nl80211_send_mntr(drv, data, len,
  5771. encrypt, noack);
  5772. }
  5773. wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
  5774. res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
  5775. &cookie, no_cck, noack, offchanok);
  5776. if (res == 0 && !noack) {
  5777. const struct ieee80211_mgmt *mgmt;
  5778. u16 fc;
  5779. mgmt = (const struct ieee80211_mgmt *) data;
  5780. fc = le_to_host16(mgmt->frame_control);
  5781. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  5782. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
  5783. wpa_printf(MSG_MSGDUMP,
  5784. "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
  5785. (long long unsigned int)
  5786. drv->send_action_cookie,
  5787. (long long unsigned int) cookie);
  5788. drv->send_action_cookie = cookie;
  5789. }
  5790. }
  5791. return res;
  5792. }
  5793. static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
  5794. size_t data_len, int noack,
  5795. unsigned int freq, int no_cck,
  5796. int offchanok,
  5797. unsigned int wait_time)
  5798. {
  5799. struct wpa_driver_nl80211_data *drv = bss->drv;
  5800. struct ieee80211_mgmt *mgmt;
  5801. int encrypt = 1;
  5802. u16 fc;
  5803. mgmt = (struct ieee80211_mgmt *) data;
  5804. fc = le_to_host16(mgmt->frame_control);
  5805. wpa_printf(MSG_DEBUG, "nl80211: send_mlme - noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x nlmode=%d",
  5806. noack, freq, no_cck, offchanok, wait_time, fc, drv->nlmode);
  5807. if ((is_sta_interface(drv->nlmode) ||
  5808. drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
  5809. WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  5810. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
  5811. /*
  5812. * The use of last_mgmt_freq is a bit of a hack,
  5813. * but it works due to the single-threaded nature
  5814. * of wpa_supplicant.
  5815. */
  5816. if (freq == 0) {
  5817. wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
  5818. drv->last_mgmt_freq);
  5819. freq = drv->last_mgmt_freq;
  5820. }
  5821. return nl80211_send_frame_cmd(bss, freq, 0,
  5822. data, data_len, NULL, 1, noack,
  5823. 1);
  5824. }
  5825. if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
  5826. if (freq == 0) {
  5827. wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
  5828. bss->freq);
  5829. freq = bss->freq;
  5830. }
  5831. return nl80211_send_frame_cmd(bss, freq,
  5832. (int) freq == bss->freq ? 0 :
  5833. wait_time,
  5834. data, data_len,
  5835. &drv->send_action_cookie,
  5836. no_cck, noack, offchanok);
  5837. }
  5838. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  5839. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
  5840. /*
  5841. * Only one of the authentication frame types is encrypted.
  5842. * In order for static WEP encryption to work properly (i.e.,
  5843. * to not encrypt the frame), we need to tell mac80211 about
  5844. * the frames that must not be encrypted.
  5845. */
  5846. u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  5847. u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
  5848. if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
  5849. encrypt = 0;
  5850. }
  5851. wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
  5852. return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
  5853. noack, freq, no_cck, offchanok,
  5854. wait_time);
  5855. }
  5856. static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
  5857. int slot, int ht_opmode, int ap_isolate,
  5858. int *basic_rates)
  5859. {
  5860. struct wpa_driver_nl80211_data *drv = bss->drv;
  5861. struct nl_msg *msg;
  5862. msg = nlmsg_alloc();
  5863. if (!msg)
  5864. return -ENOMEM;
  5865. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
  5866. if (cts >= 0)
  5867. NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
  5868. if (preamble >= 0)
  5869. NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
  5870. if (slot >= 0)
  5871. NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
  5872. if (ht_opmode >= 0)
  5873. NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
  5874. if (ap_isolate >= 0)
  5875. NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
  5876. if (basic_rates) {
  5877. u8 rates[NL80211_MAX_SUPP_RATES];
  5878. u8 rates_len = 0;
  5879. int i;
  5880. for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
  5881. i++)
  5882. rates[rates_len++] = basic_rates[i] / 5;
  5883. NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
  5884. }
  5885. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  5886. return send_and_recv_msgs(drv, msg, NULL, NULL);
  5887. nla_put_failure:
  5888. nlmsg_free(msg);
  5889. return -ENOBUFS;
  5890. }
  5891. static int wpa_driver_nl80211_set_acl(void *priv,
  5892. struct hostapd_acl_params *params)
  5893. {
  5894. struct i802_bss *bss = priv;
  5895. struct wpa_driver_nl80211_data *drv = bss->drv;
  5896. struct nl_msg *msg;
  5897. struct nlattr *acl;
  5898. unsigned int i;
  5899. int ret = 0;
  5900. if (!(drv->capa.max_acl_mac_addrs))
  5901. return -ENOTSUP;
  5902. if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
  5903. return -ENOTSUP;
  5904. msg = nlmsg_alloc();
  5905. if (!msg)
  5906. return -ENOMEM;
  5907. wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
  5908. params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
  5909. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
  5910. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  5911. NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
  5912. NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
  5913. NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
  5914. acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
  5915. if (acl == NULL)
  5916. goto nla_put_failure;
  5917. for (i = 0; i < params->num_mac_acl; i++)
  5918. NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
  5919. nla_nest_end(msg, acl);
  5920. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5921. msg = NULL;
  5922. if (ret) {
  5923. wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
  5924. ret, strerror(-ret));
  5925. }
  5926. nla_put_failure:
  5927. nlmsg_free(msg);
  5928. return ret;
  5929. }
  5930. static int wpa_driver_nl80211_set_ap(void *priv,
  5931. struct wpa_driver_ap_params *params)
  5932. {
  5933. struct i802_bss *bss = priv;
  5934. struct wpa_driver_nl80211_data *drv = bss->drv;
  5935. struct nl_msg *msg;
  5936. u8 cmd = NL80211_CMD_NEW_BEACON;
  5937. int ret;
  5938. int beacon_set;
  5939. int ifindex = if_nametoindex(bss->ifname);
  5940. int num_suites;
  5941. u32 suites[10], suite;
  5942. u32 ver;
  5943. beacon_set = bss->beacon_set;
  5944. msg = nlmsg_alloc();
  5945. if (!msg)
  5946. return -ENOMEM;
  5947. wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
  5948. beacon_set);
  5949. if (beacon_set)
  5950. cmd = NL80211_CMD_SET_BEACON;
  5951. nl80211_cmd(drv, msg, 0, cmd);
  5952. wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
  5953. params->head, params->head_len);
  5954. NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
  5955. wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
  5956. params->tail, params->tail_len);
  5957. NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
  5958. wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
  5959. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  5960. wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
  5961. NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
  5962. wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
  5963. NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
  5964. wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
  5965. params->ssid, params->ssid_len);
  5966. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  5967. params->ssid);
  5968. if (params->proberesp && params->proberesp_len) {
  5969. wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
  5970. params->proberesp, params->proberesp_len);
  5971. NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
  5972. params->proberesp);
  5973. }
  5974. switch (params->hide_ssid) {
  5975. case NO_SSID_HIDING:
  5976. wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
  5977. NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
  5978. NL80211_HIDDEN_SSID_NOT_IN_USE);
  5979. break;
  5980. case HIDDEN_SSID_ZERO_LEN:
  5981. wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
  5982. NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
  5983. NL80211_HIDDEN_SSID_ZERO_LEN);
  5984. break;
  5985. case HIDDEN_SSID_ZERO_CONTENTS:
  5986. wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
  5987. NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
  5988. NL80211_HIDDEN_SSID_ZERO_CONTENTS);
  5989. break;
  5990. }
  5991. wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
  5992. if (params->privacy)
  5993. NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
  5994. wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
  5995. if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
  5996. (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
  5997. /* Leave out the attribute */
  5998. } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
  5999. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
  6000. NL80211_AUTHTYPE_SHARED_KEY);
  6001. else
  6002. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
  6003. NL80211_AUTHTYPE_OPEN_SYSTEM);
  6004. wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
  6005. ver = 0;
  6006. if (params->wpa_version & WPA_PROTO_WPA)
  6007. ver |= NL80211_WPA_VERSION_1;
  6008. if (params->wpa_version & WPA_PROTO_RSN)
  6009. ver |= NL80211_WPA_VERSION_2;
  6010. if (ver)
  6011. NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
  6012. wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
  6013. params->key_mgmt_suites);
  6014. num_suites = 0;
  6015. if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
  6016. suites[num_suites++] = WLAN_AKM_SUITE_8021X;
  6017. if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
  6018. suites[num_suites++] = WLAN_AKM_SUITE_PSK;
  6019. if (num_suites) {
  6020. NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
  6021. num_suites * sizeof(u32), suites);
  6022. }
  6023. if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
  6024. params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
  6025. NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
  6026. wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
  6027. params->pairwise_ciphers);
  6028. num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
  6029. suites, ARRAY_SIZE(suites));
  6030. if (num_suites) {
  6031. NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
  6032. num_suites * sizeof(u32), suites);
  6033. }
  6034. wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
  6035. params->group_cipher);
  6036. suite = wpa_cipher_to_cipher_suite(params->group_cipher);
  6037. if (suite)
  6038. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite);
  6039. if (params->beacon_ies) {
  6040. wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
  6041. params->beacon_ies);
  6042. NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
  6043. wpabuf_head(params->beacon_ies));
  6044. }
  6045. if (params->proberesp_ies) {
  6046. wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
  6047. params->proberesp_ies);
  6048. NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
  6049. wpabuf_len(params->proberesp_ies),
  6050. wpabuf_head(params->proberesp_ies));
  6051. }
  6052. if (params->assocresp_ies) {
  6053. wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
  6054. params->assocresp_ies);
  6055. NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
  6056. wpabuf_len(params->assocresp_ies),
  6057. wpabuf_head(params->assocresp_ies));
  6058. }
  6059. if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
  6060. wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
  6061. params->ap_max_inactivity);
  6062. NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
  6063. params->ap_max_inactivity);
  6064. }
  6065. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6066. if (ret) {
  6067. wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
  6068. ret, strerror(-ret));
  6069. } else {
  6070. bss->beacon_set = 1;
  6071. nl80211_set_bss(bss, params->cts_protect, params->preamble,
  6072. params->short_slot_time, params->ht_opmode,
  6073. params->isolate, params->basic_rates);
  6074. }
  6075. return ret;
  6076. nla_put_failure:
  6077. nlmsg_free(msg);
  6078. return -ENOBUFS;
  6079. }
  6080. static int nl80211_put_freq_params(struct nl_msg *msg,
  6081. struct hostapd_freq_params *freq)
  6082. {
  6083. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
  6084. if (freq->vht_enabled) {
  6085. switch (freq->bandwidth) {
  6086. case 20:
  6087. NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
  6088. NL80211_CHAN_WIDTH_20);
  6089. break;
  6090. case 40:
  6091. NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
  6092. NL80211_CHAN_WIDTH_40);
  6093. break;
  6094. case 80:
  6095. if (freq->center_freq2)
  6096. NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
  6097. NL80211_CHAN_WIDTH_80P80);
  6098. else
  6099. NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
  6100. NL80211_CHAN_WIDTH_80);
  6101. break;
  6102. case 160:
  6103. NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
  6104. NL80211_CHAN_WIDTH_160);
  6105. break;
  6106. default:
  6107. return -EINVAL;
  6108. }
  6109. NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
  6110. if (freq->center_freq2)
  6111. NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
  6112. freq->center_freq2);
  6113. } else if (freq->ht_enabled) {
  6114. switch (freq->sec_channel_offset) {
  6115. case -1:
  6116. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  6117. NL80211_CHAN_HT40MINUS);
  6118. break;
  6119. case 1:
  6120. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  6121. NL80211_CHAN_HT40PLUS);
  6122. break;
  6123. default:
  6124. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  6125. NL80211_CHAN_HT20);
  6126. break;
  6127. }
  6128. }
  6129. return 0;
  6130. nla_put_failure:
  6131. return -ENOBUFS;
  6132. }
  6133. static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
  6134. struct hostapd_freq_params *freq)
  6135. {
  6136. struct wpa_driver_nl80211_data *drv = bss->drv;
  6137. struct nl_msg *msg;
  6138. int ret;
  6139. wpa_printf(MSG_DEBUG,
  6140. "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
  6141. freq->freq, freq->ht_enabled, freq->vht_enabled,
  6142. freq->bandwidth, freq->center_freq1, freq->center_freq2);
  6143. msg = nlmsg_alloc();
  6144. if (!msg)
  6145. return -1;
  6146. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
  6147. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  6148. if (nl80211_put_freq_params(msg, freq) < 0)
  6149. goto nla_put_failure;
  6150. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6151. msg = NULL;
  6152. if (ret == 0) {
  6153. bss->freq = freq->freq;
  6154. return 0;
  6155. }
  6156. wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
  6157. "%d (%s)", freq->freq, ret, strerror(-ret));
  6158. nla_put_failure:
  6159. nlmsg_free(msg);
  6160. return -1;
  6161. }
  6162. static u32 sta_flags_nl80211(int flags)
  6163. {
  6164. u32 f = 0;
  6165. if (flags & WPA_STA_AUTHORIZED)
  6166. f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
  6167. if (flags & WPA_STA_WMM)
  6168. f |= BIT(NL80211_STA_FLAG_WME);
  6169. if (flags & WPA_STA_SHORT_PREAMBLE)
  6170. f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
  6171. if (flags & WPA_STA_MFP)
  6172. f |= BIT(NL80211_STA_FLAG_MFP);
  6173. if (flags & WPA_STA_TDLS_PEER)
  6174. f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
  6175. return f;
  6176. }
  6177. static int wpa_driver_nl80211_sta_add(void *priv,
  6178. struct hostapd_sta_add_params *params)
  6179. {
  6180. struct i802_bss *bss = priv;
  6181. struct wpa_driver_nl80211_data *drv = bss->drv;
  6182. struct nl_msg *msg;
  6183. struct nl80211_sta_flag_update upd;
  6184. int ret = -ENOBUFS;
  6185. if ((params->flags & WPA_STA_TDLS_PEER) &&
  6186. !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
  6187. return -EOPNOTSUPP;
  6188. msg = nlmsg_alloc();
  6189. if (!msg)
  6190. return -ENOMEM;
  6191. wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
  6192. params->set ? "Set" : "Add", MAC2STR(params->addr));
  6193. nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
  6194. NL80211_CMD_NEW_STATION);
  6195. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  6196. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
  6197. NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
  6198. params->supp_rates);
  6199. wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
  6200. params->supp_rates_len);
  6201. if (!params->set) {
  6202. if (params->aid) {
  6203. wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
  6204. NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
  6205. } else {
  6206. /*
  6207. * cfg80211 validates that AID is non-zero, so we have
  6208. * to make this a non-zero value for the TDLS case where
  6209. * a dummy STA entry is used for now.
  6210. */
  6211. wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
  6212. NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
  6213. }
  6214. wpa_printf(MSG_DEBUG, " * listen_interval=%u",
  6215. params->listen_interval);
  6216. NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
  6217. params->listen_interval);
  6218. } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
  6219. wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
  6220. NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
  6221. }
  6222. if (params->ht_capabilities) {
  6223. wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
  6224. (u8 *) params->ht_capabilities,
  6225. sizeof(*params->ht_capabilities));
  6226. NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
  6227. sizeof(*params->ht_capabilities),
  6228. params->ht_capabilities);
  6229. }
  6230. if (params->vht_capabilities) {
  6231. wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
  6232. (u8 *) params->vht_capabilities,
  6233. sizeof(*params->vht_capabilities));
  6234. NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
  6235. sizeof(*params->vht_capabilities),
  6236. params->vht_capabilities);
  6237. }
  6238. if (params->vht_opmode_enabled) {
  6239. wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
  6240. NLA_PUT_U8(msg, NL80211_ATTR_OPMODE_NOTIF,
  6241. params->vht_opmode);
  6242. }
  6243. wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
  6244. NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
  6245. if (params->ext_capab) {
  6246. wpa_hexdump(MSG_DEBUG, " * ext_capab",
  6247. params->ext_capab, params->ext_capab_len);
  6248. NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
  6249. params->ext_capab_len, params->ext_capab);
  6250. }
  6251. if (params->supp_channels) {
  6252. wpa_hexdump(MSG_DEBUG, " * supported channels",
  6253. params->supp_channels, params->supp_channels_len);
  6254. NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
  6255. params->supp_channels_len, params->supp_channels);
  6256. }
  6257. if (params->supp_oper_classes) {
  6258. wpa_hexdump(MSG_DEBUG, " * supported operating classes",
  6259. params->supp_oper_classes,
  6260. params->supp_oper_classes_len);
  6261. NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
  6262. params->supp_oper_classes_len,
  6263. params->supp_oper_classes);
  6264. }
  6265. os_memset(&upd, 0, sizeof(upd));
  6266. upd.mask = sta_flags_nl80211(params->flags);
  6267. upd.set = upd.mask;
  6268. wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
  6269. upd.set, upd.mask);
  6270. NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
  6271. if (params->flags & WPA_STA_WMM) {
  6272. struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
  6273. if (!wme)
  6274. goto nla_put_failure;
  6275. wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
  6276. NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
  6277. params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
  6278. NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
  6279. (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
  6280. WMM_QOSINFO_STA_SP_MASK);
  6281. nla_nest_end(msg, wme);
  6282. }
  6283. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6284. msg = NULL;
  6285. if (ret)
  6286. wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
  6287. "result: %d (%s)", params->set ? "SET" : "NEW", ret,
  6288. strerror(-ret));
  6289. if (ret == -EEXIST)
  6290. ret = 0;
  6291. nla_put_failure:
  6292. nlmsg_free(msg);
  6293. return ret;
  6294. }
  6295. static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
  6296. {
  6297. struct wpa_driver_nl80211_data *drv = bss->drv;
  6298. struct nl_msg *msg;
  6299. int ret;
  6300. msg = nlmsg_alloc();
  6301. if (!msg)
  6302. return -ENOMEM;
  6303. nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
  6304. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  6305. if_nametoindex(bss->ifname));
  6306. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  6307. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6308. wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
  6309. " --> %d (%s)",
  6310. bss->ifname, MAC2STR(addr), ret, strerror(-ret));
  6311. if (ret == -ENOENT)
  6312. return 0;
  6313. return ret;
  6314. nla_put_failure:
  6315. nlmsg_free(msg);
  6316. return -ENOBUFS;
  6317. }
  6318. static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
  6319. int ifidx)
  6320. {
  6321. struct nl_msg *msg;
  6322. wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
  6323. /* stop listening for EAPOL on this interface */
  6324. del_ifidx(drv, ifidx);
  6325. msg = nlmsg_alloc();
  6326. if (!msg)
  6327. goto nla_put_failure;
  6328. nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
  6329. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
  6330. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  6331. return;
  6332. msg = NULL;
  6333. nla_put_failure:
  6334. nlmsg_free(msg);
  6335. wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
  6336. }
  6337. static const char * nl80211_iftype_str(enum nl80211_iftype mode)
  6338. {
  6339. switch (mode) {
  6340. case NL80211_IFTYPE_ADHOC:
  6341. return "ADHOC";
  6342. case NL80211_IFTYPE_STATION:
  6343. return "STATION";
  6344. case NL80211_IFTYPE_AP:
  6345. return "AP";
  6346. case NL80211_IFTYPE_AP_VLAN:
  6347. return "AP_VLAN";
  6348. case NL80211_IFTYPE_WDS:
  6349. return "WDS";
  6350. case NL80211_IFTYPE_MONITOR:
  6351. return "MONITOR";
  6352. case NL80211_IFTYPE_MESH_POINT:
  6353. return "MESH_POINT";
  6354. case NL80211_IFTYPE_P2P_CLIENT:
  6355. return "P2P_CLIENT";
  6356. case NL80211_IFTYPE_P2P_GO:
  6357. return "P2P_GO";
  6358. case NL80211_IFTYPE_P2P_DEVICE:
  6359. return "P2P_DEVICE";
  6360. default:
  6361. return "unknown";
  6362. }
  6363. }
  6364. static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
  6365. const char *ifname,
  6366. enum nl80211_iftype iftype,
  6367. const u8 *addr, int wds,
  6368. int (*handler)(struct nl_msg *, void *),
  6369. void *arg)
  6370. {
  6371. struct nl_msg *msg;
  6372. int ifidx;
  6373. int ret = -ENOBUFS;
  6374. wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
  6375. iftype, nl80211_iftype_str(iftype));
  6376. msg = nlmsg_alloc();
  6377. if (!msg)
  6378. return -1;
  6379. nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
  6380. if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
  6381. goto nla_put_failure;
  6382. NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
  6383. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
  6384. if (iftype == NL80211_IFTYPE_MONITOR) {
  6385. struct nlattr *flags;
  6386. flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
  6387. if (!flags)
  6388. goto nla_put_failure;
  6389. NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
  6390. nla_nest_end(msg, flags);
  6391. } else if (wds) {
  6392. NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
  6393. }
  6394. ret = send_and_recv_msgs(drv, msg, handler, arg);
  6395. msg = NULL;
  6396. if (ret) {
  6397. nla_put_failure:
  6398. nlmsg_free(msg);
  6399. wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
  6400. ifname, ret, strerror(-ret));
  6401. return ret;
  6402. }
  6403. if (iftype == NL80211_IFTYPE_P2P_DEVICE)
  6404. return 0;
  6405. ifidx = if_nametoindex(ifname);
  6406. wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
  6407. ifname, ifidx);
  6408. if (ifidx <= 0)
  6409. return -1;
  6410. /* start listening for EAPOL on this interface */
  6411. add_ifidx(drv, ifidx);
  6412. if (addr && iftype != NL80211_IFTYPE_MONITOR &&
  6413. linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
  6414. nl80211_remove_iface(drv, ifidx);
  6415. return -1;
  6416. }
  6417. return ifidx;
  6418. }
  6419. static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
  6420. const char *ifname, enum nl80211_iftype iftype,
  6421. const u8 *addr, int wds,
  6422. int (*handler)(struct nl_msg *, void *),
  6423. void *arg, int use_existing)
  6424. {
  6425. int ret;
  6426. ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
  6427. arg);
  6428. /* if error occurred and interface exists already */
  6429. if (ret == -ENFILE && if_nametoindex(ifname)) {
  6430. if (use_existing) {
  6431. wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
  6432. ifname);
  6433. if (addr && iftype != NL80211_IFTYPE_MONITOR &&
  6434. linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
  6435. addr) < 0 &&
  6436. (linux_set_iface_flags(drv->global->ioctl_sock,
  6437. ifname, 0) < 0 ||
  6438. linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
  6439. addr) < 0 ||
  6440. linux_set_iface_flags(drv->global->ioctl_sock,
  6441. ifname, 1) < 0))
  6442. return -1;
  6443. return -ENFILE;
  6444. }
  6445. wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
  6446. /* Try to remove the interface that was already there. */
  6447. nl80211_remove_iface(drv, if_nametoindex(ifname));
  6448. /* Try to create the interface again */
  6449. ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
  6450. wds, handler, arg);
  6451. }
  6452. if (ret >= 0 && is_p2p_net_interface(iftype))
  6453. nl80211_disable_11b_rates(drv, ret, 1);
  6454. return ret;
  6455. }
  6456. static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
  6457. {
  6458. struct ieee80211_hdr *hdr;
  6459. u16 fc;
  6460. union wpa_event_data event;
  6461. hdr = (struct ieee80211_hdr *) buf;
  6462. fc = le_to_host16(hdr->frame_control);
  6463. os_memset(&event, 0, sizeof(event));
  6464. event.tx_status.type = WLAN_FC_GET_TYPE(fc);
  6465. event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
  6466. event.tx_status.dst = hdr->addr1;
  6467. event.tx_status.data = buf;
  6468. event.tx_status.data_len = len;
  6469. event.tx_status.ack = ok;
  6470. wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
  6471. }
  6472. static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
  6473. u8 *buf, size_t len)
  6474. {
  6475. struct ieee80211_hdr *hdr = (void *)buf;
  6476. u16 fc;
  6477. union wpa_event_data event;
  6478. if (len < sizeof(*hdr))
  6479. return;
  6480. fc = le_to_host16(hdr->frame_control);
  6481. os_memset(&event, 0, sizeof(event));
  6482. event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
  6483. event.rx_from_unknown.addr = hdr->addr2;
  6484. event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
  6485. (WLAN_FC_FROMDS | WLAN_FC_TODS);
  6486. wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
  6487. }
  6488. static void handle_frame(struct wpa_driver_nl80211_data *drv,
  6489. u8 *buf, size_t len, int datarate, int ssi_signal)
  6490. {
  6491. struct ieee80211_hdr *hdr;
  6492. u16 fc;
  6493. union wpa_event_data event;
  6494. hdr = (struct ieee80211_hdr *) buf;
  6495. fc = le_to_host16(hdr->frame_control);
  6496. switch (WLAN_FC_GET_TYPE(fc)) {
  6497. case WLAN_FC_TYPE_MGMT:
  6498. os_memset(&event, 0, sizeof(event));
  6499. event.rx_mgmt.frame = buf;
  6500. event.rx_mgmt.frame_len = len;
  6501. event.rx_mgmt.datarate = datarate;
  6502. event.rx_mgmt.ssi_signal = ssi_signal;
  6503. wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
  6504. break;
  6505. case WLAN_FC_TYPE_CTRL:
  6506. /* can only get here with PS-Poll frames */
  6507. wpa_printf(MSG_DEBUG, "CTRL");
  6508. from_unknown_sta(drv, buf, len);
  6509. break;
  6510. case WLAN_FC_TYPE_DATA:
  6511. from_unknown_sta(drv, buf, len);
  6512. break;
  6513. }
  6514. }
  6515. static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
  6516. {
  6517. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  6518. int len;
  6519. unsigned char buf[3000];
  6520. struct ieee80211_radiotap_iterator iter;
  6521. int ret;
  6522. int datarate = 0, ssi_signal = 0;
  6523. int injected = 0, failed = 0, rxflags = 0;
  6524. len = recv(sock, buf, sizeof(buf), 0);
  6525. if (len < 0) {
  6526. wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
  6527. strerror(errno));
  6528. return;
  6529. }
  6530. if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
  6531. wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
  6532. return;
  6533. }
  6534. while (1) {
  6535. ret = ieee80211_radiotap_iterator_next(&iter);
  6536. if (ret == -ENOENT)
  6537. break;
  6538. if (ret) {
  6539. wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
  6540. ret);
  6541. return;
  6542. }
  6543. switch (iter.this_arg_index) {
  6544. case IEEE80211_RADIOTAP_FLAGS:
  6545. if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
  6546. len -= 4;
  6547. break;
  6548. case IEEE80211_RADIOTAP_RX_FLAGS:
  6549. rxflags = 1;
  6550. break;
  6551. case IEEE80211_RADIOTAP_TX_FLAGS:
  6552. injected = 1;
  6553. failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
  6554. IEEE80211_RADIOTAP_F_TX_FAIL;
  6555. break;
  6556. case IEEE80211_RADIOTAP_DATA_RETRIES:
  6557. break;
  6558. case IEEE80211_RADIOTAP_CHANNEL:
  6559. /* TODO: convert from freq/flags to channel number */
  6560. break;
  6561. case IEEE80211_RADIOTAP_RATE:
  6562. datarate = *iter.this_arg * 5;
  6563. break;
  6564. case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
  6565. ssi_signal = (s8) *iter.this_arg;
  6566. break;
  6567. }
  6568. }
  6569. if (rxflags && injected)
  6570. return;
  6571. if (!injected)
  6572. handle_frame(drv, buf + iter.max_length,
  6573. len - iter.max_length, datarate, ssi_signal);
  6574. else
  6575. handle_tx_callback(drv->ctx, buf + iter.max_length,
  6576. len - iter.max_length, !failed);
  6577. }
  6578. /*
  6579. * we post-process the filter code later and rewrite
  6580. * this to the offset to the last instruction
  6581. */
  6582. #define PASS 0xFF
  6583. #define FAIL 0xFE
  6584. static struct sock_filter msock_filter_insns[] = {
  6585. /*
  6586. * do a little-endian load of the radiotap length field
  6587. */
  6588. /* load lower byte into A */
  6589. BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
  6590. /* put it into X (== index register) */
  6591. BPF_STMT(BPF_MISC| BPF_TAX, 0),
  6592. /* load upper byte into A */
  6593. BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
  6594. /* left-shift it by 8 */
  6595. BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
  6596. /* or with X */
  6597. BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
  6598. /* put result into X */
  6599. BPF_STMT(BPF_MISC| BPF_TAX, 0),
  6600. /*
  6601. * Allow management frames through, this also gives us those
  6602. * management frames that we sent ourselves with status
  6603. */
  6604. /* load the lower byte of the IEEE 802.11 frame control field */
  6605. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  6606. /* mask off frame type and version */
  6607. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
  6608. /* accept frame if it's both 0, fall through otherwise */
  6609. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
  6610. /*
  6611. * TODO: add a bit to radiotap RX flags that indicates
  6612. * that the sending station is not associated, then
  6613. * add a filter here that filters on our DA and that flag
  6614. * to allow us to deauth frames to that bad station.
  6615. *
  6616. * For now allow all To DS data frames through.
  6617. */
  6618. /* load the IEEE 802.11 frame control field */
  6619. BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
  6620. /* mask off frame type, version and DS status */
  6621. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
  6622. /* accept frame if version 0, type 2 and To DS, fall through otherwise
  6623. */
  6624. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
  6625. #if 0
  6626. /*
  6627. * drop non-data frames
  6628. */
  6629. /* load the lower byte of the frame control field */
  6630. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  6631. /* mask off QoS bit */
  6632. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
  6633. /* drop non-data frames */
  6634. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
  6635. #endif
  6636. /* load the upper byte of the frame control field */
  6637. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
  6638. /* mask off toDS/fromDS */
  6639. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
  6640. /* accept WDS frames */
  6641. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
  6642. /*
  6643. * add header length to index
  6644. */
  6645. /* load the lower byte of the frame control field */
  6646. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  6647. /* mask off QoS bit */
  6648. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
  6649. /* right shift it by 6 to give 0 or 2 */
  6650. BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
  6651. /* add data frame header length */
  6652. BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
  6653. /* add index, was start of 802.11 header */
  6654. BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
  6655. /* move to index, now start of LL header */
  6656. BPF_STMT(BPF_MISC | BPF_TAX, 0),
  6657. /*
  6658. * Accept empty data frames, we use those for
  6659. * polling activity.
  6660. */
  6661. BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
  6662. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
  6663. /*
  6664. * Accept EAPOL frames
  6665. */
  6666. BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
  6667. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
  6668. BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
  6669. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
  6670. /* keep these last two statements or change the code below */
  6671. /* return 0 == "DROP" */
  6672. BPF_STMT(BPF_RET | BPF_K, 0),
  6673. /* return ~0 == "keep all" */
  6674. BPF_STMT(BPF_RET | BPF_K, ~0),
  6675. };
  6676. static struct sock_fprog msock_filter = {
  6677. .len = ARRAY_SIZE(msock_filter_insns),
  6678. .filter = msock_filter_insns,
  6679. };
  6680. static int add_monitor_filter(int s)
  6681. {
  6682. int idx;
  6683. /* rewrite all PASS/FAIL jump offsets */
  6684. for (idx = 0; idx < msock_filter.len; idx++) {
  6685. struct sock_filter *insn = &msock_filter_insns[idx];
  6686. if (BPF_CLASS(insn->code) == BPF_JMP) {
  6687. if (insn->code == (BPF_JMP|BPF_JA)) {
  6688. if (insn->k == PASS)
  6689. insn->k = msock_filter.len - idx - 2;
  6690. else if (insn->k == FAIL)
  6691. insn->k = msock_filter.len - idx - 3;
  6692. }
  6693. if (insn->jt == PASS)
  6694. insn->jt = msock_filter.len - idx - 2;
  6695. else if (insn->jt == FAIL)
  6696. insn->jt = msock_filter.len - idx - 3;
  6697. if (insn->jf == PASS)
  6698. insn->jf = msock_filter.len - idx - 2;
  6699. else if (insn->jf == FAIL)
  6700. insn->jf = msock_filter.len - idx - 3;
  6701. }
  6702. }
  6703. if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
  6704. &msock_filter, sizeof(msock_filter))) {
  6705. wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
  6706. strerror(errno));
  6707. return -1;
  6708. }
  6709. return 0;
  6710. }
  6711. static void nl80211_remove_monitor_interface(
  6712. struct wpa_driver_nl80211_data *drv)
  6713. {
  6714. if (drv->monitor_refcount > 0)
  6715. drv->monitor_refcount--;
  6716. wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
  6717. drv->monitor_refcount);
  6718. if (drv->monitor_refcount > 0)
  6719. return;
  6720. if (drv->monitor_ifidx >= 0) {
  6721. nl80211_remove_iface(drv, drv->monitor_ifidx);
  6722. drv->monitor_ifidx = -1;
  6723. }
  6724. if (drv->monitor_sock >= 0) {
  6725. eloop_unregister_read_sock(drv->monitor_sock);
  6726. close(drv->monitor_sock);
  6727. drv->monitor_sock = -1;
  6728. }
  6729. }
  6730. static int
  6731. nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
  6732. {
  6733. char buf[IFNAMSIZ];
  6734. struct sockaddr_ll ll;
  6735. int optval;
  6736. socklen_t optlen;
  6737. if (drv->monitor_ifidx >= 0) {
  6738. drv->monitor_refcount++;
  6739. wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
  6740. drv->monitor_refcount);
  6741. return 0;
  6742. }
  6743. if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
  6744. /*
  6745. * P2P interface name is of the format p2p-%s-%d. For monitor
  6746. * interface name corresponding to P2P GO, replace "p2p-" with
  6747. * "mon-" to retain the same interface name length and to
  6748. * indicate that it is a monitor interface.
  6749. */
  6750. snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
  6751. } else {
  6752. /* Non-P2P interface with AP functionality. */
  6753. snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
  6754. }
  6755. buf[IFNAMSIZ - 1] = '\0';
  6756. drv->monitor_ifidx =
  6757. nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
  6758. 0, NULL, NULL, 0);
  6759. if (drv->monitor_ifidx == -EOPNOTSUPP) {
  6760. /*
  6761. * This is backward compatibility for a few versions of
  6762. * the kernel only that didn't advertise the right
  6763. * attributes for the only driver that then supported
  6764. * AP mode w/o monitor -- ath6kl.
  6765. */
  6766. wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
  6767. "monitor interface type - try to run without it");
  6768. drv->device_ap_sme = 1;
  6769. }
  6770. if (drv->monitor_ifidx < 0)
  6771. return -1;
  6772. if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
  6773. goto error;
  6774. memset(&ll, 0, sizeof(ll));
  6775. ll.sll_family = AF_PACKET;
  6776. ll.sll_ifindex = drv->monitor_ifidx;
  6777. drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  6778. if (drv->monitor_sock < 0) {
  6779. wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
  6780. strerror(errno));
  6781. goto error;
  6782. }
  6783. if (add_monitor_filter(drv->monitor_sock)) {
  6784. wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
  6785. "interface; do filtering in user space");
  6786. /* This works, but will cost in performance. */
  6787. }
  6788. if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
  6789. wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
  6790. strerror(errno));
  6791. goto error;
  6792. }
  6793. optlen = sizeof(optval);
  6794. optval = 20;
  6795. if (setsockopt
  6796. (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
  6797. wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
  6798. strerror(errno));
  6799. goto error;
  6800. }
  6801. if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
  6802. drv, NULL)) {
  6803. wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
  6804. goto error;
  6805. }
  6806. drv->monitor_refcount++;
  6807. return 0;
  6808. error:
  6809. nl80211_remove_monitor_interface(drv);
  6810. return -1;
  6811. }
  6812. static int nl80211_setup_ap(struct i802_bss *bss)
  6813. {
  6814. struct wpa_driver_nl80211_data *drv = bss->drv;
  6815. wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
  6816. bss->ifname, drv->device_ap_sme, drv->use_monitor);
  6817. /*
  6818. * Disable Probe Request reporting unless we need it in this way for
  6819. * devices that include the AP SME, in the other case (unless using
  6820. * monitor iface) we'll get it through the nl_mgmt socket instead.
  6821. */
  6822. if (!drv->device_ap_sme)
  6823. wpa_driver_nl80211_probe_req_report(bss, 0);
  6824. if (!drv->device_ap_sme && !drv->use_monitor)
  6825. if (nl80211_mgmt_subscribe_ap(bss))
  6826. return -1;
  6827. if (drv->device_ap_sme && !drv->use_monitor)
  6828. if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
  6829. return -1;
  6830. if (!drv->device_ap_sme && drv->use_monitor &&
  6831. nl80211_create_monitor_interface(drv) &&
  6832. !drv->device_ap_sme)
  6833. return -1;
  6834. if (drv->device_ap_sme &&
  6835. wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
  6836. wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
  6837. "Probe Request frame reporting in AP mode");
  6838. /* Try to survive without this */
  6839. }
  6840. return 0;
  6841. }
  6842. static void nl80211_teardown_ap(struct i802_bss *bss)
  6843. {
  6844. struct wpa_driver_nl80211_data *drv = bss->drv;
  6845. wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
  6846. bss->ifname, drv->device_ap_sme, drv->use_monitor);
  6847. if (drv->device_ap_sme) {
  6848. wpa_driver_nl80211_probe_req_report(bss, 0);
  6849. if (!drv->use_monitor)
  6850. nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
  6851. } else if (drv->use_monitor)
  6852. nl80211_remove_monitor_interface(drv);
  6853. else
  6854. nl80211_mgmt_unsubscribe(bss, "AP teardown");
  6855. bss->beacon_set = 0;
  6856. }
  6857. static int nl80211_send_eapol_data(struct i802_bss *bss,
  6858. const u8 *addr, const u8 *data,
  6859. size_t data_len)
  6860. {
  6861. struct sockaddr_ll ll;
  6862. int ret;
  6863. if (bss->drv->eapol_tx_sock < 0) {
  6864. wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
  6865. return -1;
  6866. }
  6867. os_memset(&ll, 0, sizeof(ll));
  6868. ll.sll_family = AF_PACKET;
  6869. ll.sll_ifindex = bss->ifindex;
  6870. ll.sll_protocol = htons(ETH_P_PAE);
  6871. ll.sll_halen = ETH_ALEN;
  6872. os_memcpy(ll.sll_addr, addr, ETH_ALEN);
  6873. ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
  6874. (struct sockaddr *) &ll, sizeof(ll));
  6875. if (ret < 0)
  6876. wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
  6877. strerror(errno));
  6878. return ret;
  6879. }
  6880. static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  6881. static int wpa_driver_nl80211_hapd_send_eapol(
  6882. void *priv, const u8 *addr, const u8 *data,
  6883. size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
  6884. {
  6885. struct i802_bss *bss = priv;
  6886. struct wpa_driver_nl80211_data *drv = bss->drv;
  6887. struct ieee80211_hdr *hdr;
  6888. size_t len;
  6889. u8 *pos;
  6890. int res;
  6891. int qos = flags & WPA_STA_WMM;
  6892. if (drv->device_ap_sme || !drv->use_monitor)
  6893. return nl80211_send_eapol_data(bss, addr, data, data_len);
  6894. len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
  6895. data_len;
  6896. hdr = os_zalloc(len);
  6897. if (hdr == NULL) {
  6898. wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
  6899. (unsigned long) len);
  6900. return -1;
  6901. }
  6902. hdr->frame_control =
  6903. IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
  6904. hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
  6905. if (encrypt)
  6906. hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  6907. if (qos) {
  6908. hdr->frame_control |=
  6909. host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
  6910. }
  6911. memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
  6912. memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
  6913. memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
  6914. pos = (u8 *) (hdr + 1);
  6915. if (qos) {
  6916. /* Set highest priority in QoS header */
  6917. pos[0] = 7;
  6918. pos[1] = 0;
  6919. pos += 2;
  6920. }
  6921. memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
  6922. pos += sizeof(rfc1042_header);
  6923. WPA_PUT_BE16(pos, ETH_P_PAE);
  6924. pos += 2;
  6925. memcpy(pos, data, data_len);
  6926. res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
  6927. 0, 0, 0, 0);
  6928. if (res < 0) {
  6929. wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
  6930. "failed: %d (%s)",
  6931. (unsigned long) len, errno, strerror(errno));
  6932. }
  6933. os_free(hdr);
  6934. return res;
  6935. }
  6936. static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
  6937. int total_flags,
  6938. int flags_or, int flags_and)
  6939. {
  6940. struct i802_bss *bss = priv;
  6941. struct wpa_driver_nl80211_data *drv = bss->drv;
  6942. struct nl_msg *msg;
  6943. struct nlattr *flags;
  6944. struct nl80211_sta_flag_update upd;
  6945. msg = nlmsg_alloc();
  6946. if (!msg)
  6947. return -ENOMEM;
  6948. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
  6949. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  6950. if_nametoindex(bss->ifname));
  6951. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  6952. /*
  6953. * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
  6954. * can be removed eventually.
  6955. */
  6956. flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
  6957. if (!flags)
  6958. goto nla_put_failure;
  6959. if (total_flags & WPA_STA_AUTHORIZED)
  6960. NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
  6961. if (total_flags & WPA_STA_WMM)
  6962. NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
  6963. if (total_flags & WPA_STA_SHORT_PREAMBLE)
  6964. NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
  6965. if (total_flags & WPA_STA_MFP)
  6966. NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
  6967. if (total_flags & WPA_STA_TDLS_PEER)
  6968. NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
  6969. nla_nest_end(msg, flags);
  6970. os_memset(&upd, 0, sizeof(upd));
  6971. upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
  6972. upd.set = sta_flags_nl80211(flags_or);
  6973. NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
  6974. return send_and_recv_msgs(drv, msg, NULL, NULL);
  6975. nla_put_failure:
  6976. nlmsg_free(msg);
  6977. return -ENOBUFS;
  6978. }
  6979. static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
  6980. struct wpa_driver_associate_params *params)
  6981. {
  6982. enum nl80211_iftype nlmode, old_mode;
  6983. struct hostapd_freq_params freq = {
  6984. .freq = params->freq,
  6985. };
  6986. if (params->p2p) {
  6987. wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
  6988. "group (GO)");
  6989. nlmode = NL80211_IFTYPE_P2P_GO;
  6990. } else
  6991. nlmode = NL80211_IFTYPE_AP;
  6992. old_mode = drv->nlmode;
  6993. if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
  6994. nl80211_remove_monitor_interface(drv);
  6995. return -1;
  6996. }
  6997. if (wpa_driver_nl80211_set_freq(drv->first_bss, &freq)) {
  6998. if (old_mode != nlmode)
  6999. wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
  7000. nl80211_remove_monitor_interface(drv);
  7001. return -1;
  7002. }
  7003. return 0;
  7004. }
  7005. static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
  7006. {
  7007. struct nl_msg *msg;
  7008. int ret = -1;
  7009. msg = nlmsg_alloc();
  7010. if (!msg)
  7011. return -1;
  7012. nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
  7013. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  7014. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7015. msg = NULL;
  7016. if (ret) {
  7017. wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
  7018. "(%s)", ret, strerror(-ret));
  7019. goto nla_put_failure;
  7020. }
  7021. ret = 0;
  7022. wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
  7023. nla_put_failure:
  7024. if (wpa_driver_nl80211_set_mode(drv->first_bss,
  7025. NL80211_IFTYPE_STATION)) {
  7026. wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
  7027. "station mode");
  7028. }
  7029. nlmsg_free(msg);
  7030. return ret;
  7031. }
  7032. static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
  7033. struct wpa_driver_associate_params *params)
  7034. {
  7035. struct nl_msg *msg;
  7036. int ret = -1;
  7037. int count = 0;
  7038. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
  7039. if (wpa_driver_nl80211_set_mode(drv->first_bss,
  7040. NL80211_IFTYPE_ADHOC)) {
  7041. wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
  7042. "IBSS mode");
  7043. return -1;
  7044. }
  7045. retry:
  7046. msg = nlmsg_alloc();
  7047. if (!msg)
  7048. return -1;
  7049. nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
  7050. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  7051. if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
  7052. goto nla_put_failure;
  7053. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  7054. params->ssid, params->ssid_len);
  7055. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  7056. params->ssid);
  7057. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  7058. drv->ssid_len = params->ssid_len;
  7059. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  7060. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  7061. if (params->beacon_int > 0) {
  7062. wpa_printf(MSG_DEBUG, " * beacon_int=%d", params->beacon_int);
  7063. NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL,
  7064. params->beacon_int);
  7065. }
  7066. ret = nl80211_set_conn_keys(params, msg);
  7067. if (ret)
  7068. goto nla_put_failure;
  7069. if (params->bssid && params->fixed_bssid) {
  7070. wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
  7071. MAC2STR(params->bssid));
  7072. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  7073. }
  7074. if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
  7075. params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
  7076. params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
  7077. params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
  7078. wpa_printf(MSG_DEBUG, " * control port");
  7079. NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
  7080. }
  7081. if (params->wpa_ie) {
  7082. wpa_hexdump(MSG_DEBUG,
  7083. " * Extra IEs for Beacon/Probe Response frames",
  7084. params->wpa_ie, params->wpa_ie_len);
  7085. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  7086. params->wpa_ie);
  7087. }
  7088. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7089. msg = NULL;
  7090. if (ret) {
  7091. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
  7092. ret, strerror(-ret));
  7093. count++;
  7094. if (ret == -EALREADY && count == 1) {
  7095. wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
  7096. "forced leave");
  7097. nl80211_leave_ibss(drv);
  7098. nlmsg_free(msg);
  7099. goto retry;
  7100. }
  7101. goto nla_put_failure;
  7102. }
  7103. ret = 0;
  7104. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
  7105. nla_put_failure:
  7106. nlmsg_free(msg);
  7107. return ret;
  7108. }
  7109. static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
  7110. struct wpa_driver_associate_params *params,
  7111. struct nl_msg *msg)
  7112. {
  7113. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  7114. if (params->bssid) {
  7115. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  7116. MAC2STR(params->bssid));
  7117. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  7118. }
  7119. if (params->bssid_hint) {
  7120. wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
  7121. MAC2STR(params->bssid_hint));
  7122. NLA_PUT(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
  7123. params->bssid_hint);
  7124. }
  7125. if (params->freq) {
  7126. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  7127. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  7128. drv->assoc_freq = params->freq;
  7129. } else
  7130. drv->assoc_freq = 0;
  7131. if (params->freq_hint) {
  7132. wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
  7133. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
  7134. params->freq_hint);
  7135. }
  7136. if (params->bg_scan_period >= 0) {
  7137. wpa_printf(MSG_DEBUG, " * bg scan period=%d",
  7138. params->bg_scan_period);
  7139. NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
  7140. params->bg_scan_period);
  7141. }
  7142. if (params->ssid) {
  7143. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  7144. params->ssid, params->ssid_len);
  7145. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  7146. params->ssid);
  7147. if (params->ssid_len > sizeof(drv->ssid))
  7148. goto nla_put_failure;
  7149. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  7150. drv->ssid_len = params->ssid_len;
  7151. }
  7152. wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
  7153. if (params->wpa_ie)
  7154. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  7155. params->wpa_ie);
  7156. if (params->wpa_proto) {
  7157. enum nl80211_wpa_versions ver = 0;
  7158. if (params->wpa_proto & WPA_PROTO_WPA)
  7159. ver |= NL80211_WPA_VERSION_1;
  7160. if (params->wpa_proto & WPA_PROTO_RSN)
  7161. ver |= NL80211_WPA_VERSION_2;
  7162. wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
  7163. NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
  7164. }
  7165. if (params->pairwise_suite != WPA_CIPHER_NONE) {
  7166. u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
  7167. wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
  7168. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
  7169. }
  7170. if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
  7171. !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
  7172. /*
  7173. * This is likely to work even though many drivers do not
  7174. * advertise support for operations without GTK.
  7175. */
  7176. wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
  7177. } else if (params->group_suite != WPA_CIPHER_NONE) {
  7178. u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
  7179. wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
  7180. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
  7181. }
  7182. if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
  7183. params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
  7184. params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
  7185. params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
  7186. params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
  7187. params->key_mgmt_suite == WPA_KEY_MGMT_OSEN) {
  7188. int mgmt = WLAN_AKM_SUITE_PSK;
  7189. switch (params->key_mgmt_suite) {
  7190. case WPA_KEY_MGMT_CCKM:
  7191. mgmt = WLAN_AKM_SUITE_CCKM;
  7192. break;
  7193. case WPA_KEY_MGMT_IEEE8021X:
  7194. mgmt = WLAN_AKM_SUITE_8021X;
  7195. break;
  7196. case WPA_KEY_MGMT_FT_IEEE8021X:
  7197. mgmt = WLAN_AKM_SUITE_FT_8021X;
  7198. break;
  7199. case WPA_KEY_MGMT_FT_PSK:
  7200. mgmt = WLAN_AKM_SUITE_FT_PSK;
  7201. break;
  7202. case WPA_KEY_MGMT_OSEN:
  7203. mgmt = WLAN_AKM_SUITE_OSEN;
  7204. break;
  7205. case WPA_KEY_MGMT_PSK:
  7206. default:
  7207. mgmt = WLAN_AKM_SUITE_PSK;
  7208. break;
  7209. }
  7210. wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
  7211. NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
  7212. }
  7213. NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
  7214. if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
  7215. NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
  7216. if (params->disable_ht)
  7217. NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
  7218. if (params->htcaps && params->htcaps_mask) {
  7219. int sz = sizeof(struct ieee80211_ht_capabilities);
  7220. NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
  7221. NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
  7222. params->htcaps_mask);
  7223. }
  7224. #ifdef CONFIG_VHT_OVERRIDES
  7225. if (params->disable_vht) {
  7226. wpa_printf(MSG_DEBUG, " * VHT disabled");
  7227. NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
  7228. }
  7229. if (params->vhtcaps && params->vhtcaps_mask) {
  7230. int sz = sizeof(struct ieee80211_vht_capabilities);
  7231. NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
  7232. NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
  7233. params->vhtcaps_mask);
  7234. }
  7235. #endif /* CONFIG_VHT_OVERRIDES */
  7236. if (params->p2p)
  7237. wpa_printf(MSG_DEBUG, " * P2P group");
  7238. return 0;
  7239. nla_put_failure:
  7240. return -1;
  7241. }
  7242. static int wpa_driver_nl80211_try_connect(
  7243. struct wpa_driver_nl80211_data *drv,
  7244. struct wpa_driver_associate_params *params)
  7245. {
  7246. struct nl_msg *msg;
  7247. enum nl80211_auth_type type;
  7248. int ret;
  7249. int algs;
  7250. msg = nlmsg_alloc();
  7251. if (!msg)
  7252. return -1;
  7253. wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
  7254. nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
  7255. ret = nl80211_connect_common(drv, params, msg);
  7256. if (ret)
  7257. goto nla_put_failure;
  7258. algs = 0;
  7259. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  7260. algs++;
  7261. if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  7262. algs++;
  7263. if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  7264. algs++;
  7265. if (algs > 1) {
  7266. wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
  7267. "selection");
  7268. goto skip_auth_type;
  7269. }
  7270. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  7271. type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  7272. else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  7273. type = NL80211_AUTHTYPE_SHARED_KEY;
  7274. else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  7275. type = NL80211_AUTHTYPE_NETWORK_EAP;
  7276. else if (params->auth_alg & WPA_AUTH_ALG_FT)
  7277. type = NL80211_AUTHTYPE_FT;
  7278. else
  7279. goto nla_put_failure;
  7280. wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
  7281. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
  7282. skip_auth_type:
  7283. ret = nl80211_set_conn_keys(params, msg);
  7284. if (ret)
  7285. goto nla_put_failure;
  7286. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7287. msg = NULL;
  7288. if (ret) {
  7289. wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
  7290. "(%s)", ret, strerror(-ret));
  7291. goto nla_put_failure;
  7292. }
  7293. ret = 0;
  7294. wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
  7295. nla_put_failure:
  7296. nlmsg_free(msg);
  7297. return ret;
  7298. }
  7299. static int wpa_driver_nl80211_connect(
  7300. struct wpa_driver_nl80211_data *drv,
  7301. struct wpa_driver_associate_params *params)
  7302. {
  7303. int ret = wpa_driver_nl80211_try_connect(drv, params);
  7304. if (ret == -EALREADY) {
  7305. /*
  7306. * cfg80211 does not currently accept new connections if
  7307. * we are already connected. As a workaround, force
  7308. * disconnection and try again.
  7309. */
  7310. wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
  7311. "disconnecting before reassociation "
  7312. "attempt");
  7313. if (wpa_driver_nl80211_disconnect(
  7314. drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
  7315. return -1;
  7316. ret = wpa_driver_nl80211_try_connect(drv, params);
  7317. }
  7318. return ret;
  7319. }
  7320. static int wpa_driver_nl80211_associate(
  7321. void *priv, struct wpa_driver_associate_params *params)
  7322. {
  7323. struct i802_bss *bss = priv;
  7324. struct wpa_driver_nl80211_data *drv = bss->drv;
  7325. int ret;
  7326. struct nl_msg *msg;
  7327. if (params->mode == IEEE80211_MODE_AP)
  7328. return wpa_driver_nl80211_ap(drv, params);
  7329. if (params->mode == IEEE80211_MODE_IBSS)
  7330. return wpa_driver_nl80211_ibss(drv, params);
  7331. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
  7332. enum nl80211_iftype nlmode = params->p2p ?
  7333. NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
  7334. if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
  7335. return -1;
  7336. return wpa_driver_nl80211_connect(drv, params);
  7337. }
  7338. nl80211_mark_disconnected(drv);
  7339. msg = nlmsg_alloc();
  7340. if (!msg)
  7341. return -1;
  7342. wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
  7343. drv->ifindex);
  7344. nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
  7345. ret = nl80211_connect_common(drv, params, msg);
  7346. if (ret)
  7347. goto nla_put_failure;
  7348. if (params->prev_bssid) {
  7349. wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
  7350. MAC2STR(params->prev_bssid));
  7351. NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
  7352. params->prev_bssid);
  7353. }
  7354. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7355. msg = NULL;
  7356. if (ret) {
  7357. wpa_dbg(drv->ctx, MSG_DEBUG,
  7358. "nl80211: MLME command failed (assoc): ret=%d (%s)",
  7359. ret, strerror(-ret));
  7360. nl80211_dump_scan(drv);
  7361. goto nla_put_failure;
  7362. }
  7363. ret = 0;
  7364. wpa_printf(MSG_DEBUG, "nl80211: Association request send "
  7365. "successfully");
  7366. nla_put_failure:
  7367. nlmsg_free(msg);
  7368. return ret;
  7369. }
  7370. static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
  7371. int ifindex, enum nl80211_iftype mode)
  7372. {
  7373. struct nl_msg *msg;
  7374. int ret = -ENOBUFS;
  7375. wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
  7376. ifindex, mode, nl80211_iftype_str(mode));
  7377. msg = nlmsg_alloc();
  7378. if (!msg)
  7379. return -ENOMEM;
  7380. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
  7381. if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
  7382. goto nla_put_failure;
  7383. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
  7384. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7385. msg = NULL;
  7386. if (!ret)
  7387. return 0;
  7388. nla_put_failure:
  7389. nlmsg_free(msg);
  7390. wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
  7391. " %d (%s)", ifindex, mode, ret, strerror(-ret));
  7392. return ret;
  7393. }
  7394. static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
  7395. enum nl80211_iftype nlmode)
  7396. {
  7397. struct wpa_driver_nl80211_data *drv = bss->drv;
  7398. int ret = -1;
  7399. int i;
  7400. int was_ap = is_ap_interface(drv->nlmode);
  7401. int res;
  7402. res = nl80211_set_mode(drv, drv->ifindex, nlmode);
  7403. if (res && nlmode == nl80211_get_ifmode(bss))
  7404. res = 0;
  7405. if (res == 0) {
  7406. drv->nlmode = nlmode;
  7407. ret = 0;
  7408. goto done;
  7409. }
  7410. if (res == -ENODEV)
  7411. return -1;
  7412. if (nlmode == drv->nlmode) {
  7413. wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
  7414. "requested mode - ignore error");
  7415. ret = 0;
  7416. goto done; /* Already in the requested mode */
  7417. }
  7418. /* mac80211 doesn't allow mode changes while the device is up, so
  7419. * take the device down, try to set the mode again, and bring the
  7420. * device back up.
  7421. */
  7422. wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
  7423. "interface down");
  7424. for (i = 0; i < 10; i++) {
  7425. res = i802_set_iface_flags(bss, 0);
  7426. if (res == -EACCES || res == -ENODEV)
  7427. break;
  7428. if (res == 0) {
  7429. /* Try to set the mode again while the interface is
  7430. * down */
  7431. ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
  7432. if (ret == -EACCES)
  7433. break;
  7434. res = i802_set_iface_flags(bss, 1);
  7435. if (res && !ret)
  7436. ret = -1;
  7437. else if (ret != -EBUSY)
  7438. break;
  7439. } else
  7440. wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
  7441. "interface down");
  7442. os_sleep(0, 100000);
  7443. }
  7444. if (!ret) {
  7445. wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
  7446. "interface is down");
  7447. drv->nlmode = nlmode;
  7448. drv->ignore_if_down_event = 1;
  7449. }
  7450. done:
  7451. if (ret) {
  7452. wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
  7453. "from %d failed", nlmode, drv->nlmode);
  7454. return ret;
  7455. }
  7456. if (is_p2p_net_interface(nlmode))
  7457. nl80211_disable_11b_rates(drv, drv->ifindex, 1);
  7458. else if (drv->disabled_11b_rates)
  7459. nl80211_disable_11b_rates(drv, drv->ifindex, 0);
  7460. if (is_ap_interface(nlmode)) {
  7461. nl80211_mgmt_unsubscribe(bss, "start AP");
  7462. /* Setup additional AP mode functionality if needed */
  7463. if (nl80211_setup_ap(bss))
  7464. return -1;
  7465. } else if (was_ap) {
  7466. /* Remove additional AP mode functionality */
  7467. nl80211_teardown_ap(bss);
  7468. } else {
  7469. nl80211_mgmt_unsubscribe(bss, "mode change");
  7470. }
  7471. if (!bss->in_deinit && !is_ap_interface(nlmode) &&
  7472. nl80211_mgmt_subscribe_non_ap(bss) < 0)
  7473. wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
  7474. "frame processing - ignore for now");
  7475. return 0;
  7476. }
  7477. static int dfs_info_handler(struct nl_msg *msg, void *arg)
  7478. {
  7479. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  7480. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  7481. int *dfs_capability_ptr = arg;
  7482. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  7483. genlmsg_attrlen(gnlh, 0), NULL);
  7484. if (tb[NL80211_ATTR_VENDOR_DATA]) {
  7485. struct nlattr *nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
  7486. struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
  7487. nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
  7488. nla_data(nl_vend), nla_len(nl_vend), NULL);
  7489. if (tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]) {
  7490. u32 val;
  7491. val = nla_get_u32(tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]);
  7492. wpa_printf(MSG_DEBUG, "nl80211: DFS offload capability: %u",
  7493. val);
  7494. *dfs_capability_ptr = val;
  7495. }
  7496. }
  7497. return NL_SKIP;
  7498. }
  7499. static int wpa_driver_nl80211_get_capa(void *priv,
  7500. struct wpa_driver_capa *capa)
  7501. {
  7502. struct i802_bss *bss = priv;
  7503. struct wpa_driver_nl80211_data *drv = bss->drv;
  7504. struct nl_msg *msg;
  7505. int dfs_capability = 0;
  7506. int ret = 0;
  7507. if (!drv->has_capability)
  7508. return -1;
  7509. os_memcpy(capa, &drv->capa, sizeof(*capa));
  7510. if (drv->extended_capa && drv->extended_capa_mask) {
  7511. capa->extended_capa = drv->extended_capa;
  7512. capa->extended_capa_mask = drv->extended_capa_mask;
  7513. capa->extended_capa_len = drv->extended_capa_len;
  7514. }
  7515. if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
  7516. !drv->allow_p2p_device) {
  7517. wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
  7518. capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
  7519. }
  7520. if (drv->dfs_vendor_cmd_avail == 1) {
  7521. msg = nlmsg_alloc();
  7522. if (!msg)
  7523. return -ENOMEM;
  7524. nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
  7525. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  7526. NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA);
  7527. NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD,
  7528. QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY);
  7529. ret = send_and_recv_msgs(drv, msg, dfs_info_handler,
  7530. &dfs_capability);
  7531. if (!ret) {
  7532. if (dfs_capability)
  7533. capa->flags |= WPA_DRIVER_FLAGS_DFS_OFFLOAD;
  7534. }
  7535. }
  7536. return ret;
  7537. nla_put_failure:
  7538. nlmsg_free(msg);
  7539. return -ENOBUFS;
  7540. }
  7541. static int wpa_driver_nl80211_set_operstate(void *priv, int state)
  7542. {
  7543. struct i802_bss *bss = priv;
  7544. struct wpa_driver_nl80211_data *drv = bss->drv;
  7545. wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
  7546. bss->ifname, drv->operstate, state,
  7547. state ? "UP" : "DORMANT");
  7548. drv->operstate = state;
  7549. return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
  7550. state ? IF_OPER_UP : IF_OPER_DORMANT);
  7551. }
  7552. static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
  7553. {
  7554. struct i802_bss *bss = priv;
  7555. struct wpa_driver_nl80211_data *drv = bss->drv;
  7556. struct nl_msg *msg;
  7557. struct nl80211_sta_flag_update upd;
  7558. int ret = -ENOBUFS;
  7559. if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
  7560. wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
  7561. return 0;
  7562. }
  7563. wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
  7564. MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
  7565. msg = nlmsg_alloc();
  7566. if (!msg)
  7567. return -ENOMEM;
  7568. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
  7569. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  7570. if_nametoindex(bss->ifname));
  7571. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
  7572. os_memset(&upd, 0, sizeof(upd));
  7573. upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
  7574. if (authorized)
  7575. upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
  7576. NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
  7577. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7578. msg = NULL;
  7579. if (!ret)
  7580. return 0;
  7581. nla_put_failure:
  7582. nlmsg_free(msg);
  7583. wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
  7584. ret, strerror(-ret));
  7585. return ret;
  7586. }
  7587. /* Set kernel driver on given frequency (MHz) */
  7588. static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
  7589. {
  7590. struct i802_bss *bss = priv;
  7591. return wpa_driver_nl80211_set_freq(bss, freq);
  7592. }
  7593. static inline int min_int(int a, int b)
  7594. {
  7595. if (a < b)
  7596. return a;
  7597. return b;
  7598. }
  7599. static int get_key_handler(struct nl_msg *msg, void *arg)
  7600. {
  7601. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  7602. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  7603. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  7604. genlmsg_attrlen(gnlh, 0), NULL);
  7605. /*
  7606. * TODO: validate the key index and mac address!
  7607. * Otherwise, there's a race condition as soon as
  7608. * the kernel starts sending key notifications.
  7609. */
  7610. if (tb[NL80211_ATTR_KEY_SEQ])
  7611. memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
  7612. min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
  7613. return NL_SKIP;
  7614. }
  7615. static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
  7616. int idx, u8 *seq)
  7617. {
  7618. struct i802_bss *bss = priv;
  7619. struct wpa_driver_nl80211_data *drv = bss->drv;
  7620. struct nl_msg *msg;
  7621. msg = nlmsg_alloc();
  7622. if (!msg)
  7623. return -ENOMEM;
  7624. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
  7625. if (addr)
  7626. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  7627. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
  7628. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
  7629. memset(seq, 0, 6);
  7630. return send_and_recv_msgs(drv, msg, get_key_handler, seq);
  7631. nla_put_failure:
  7632. nlmsg_free(msg);
  7633. return -ENOBUFS;
  7634. }
  7635. static int i802_set_rts(void *priv, int rts)
  7636. {
  7637. struct i802_bss *bss = priv;
  7638. struct wpa_driver_nl80211_data *drv = bss->drv;
  7639. struct nl_msg *msg;
  7640. int ret = -ENOBUFS;
  7641. u32 val;
  7642. msg = nlmsg_alloc();
  7643. if (!msg)
  7644. return -ENOMEM;
  7645. if (rts >= 2347)
  7646. val = (u32) -1;
  7647. else
  7648. val = rts;
  7649. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
  7650. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  7651. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
  7652. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7653. msg = NULL;
  7654. if (!ret)
  7655. return 0;
  7656. nla_put_failure:
  7657. nlmsg_free(msg);
  7658. wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
  7659. "%d (%s)", rts, ret, strerror(-ret));
  7660. return ret;
  7661. }
  7662. static int i802_set_frag(void *priv, int frag)
  7663. {
  7664. struct i802_bss *bss = priv;
  7665. struct wpa_driver_nl80211_data *drv = bss->drv;
  7666. struct nl_msg *msg;
  7667. int ret = -ENOBUFS;
  7668. u32 val;
  7669. msg = nlmsg_alloc();
  7670. if (!msg)
  7671. return -ENOMEM;
  7672. if (frag >= 2346)
  7673. val = (u32) -1;
  7674. else
  7675. val = frag;
  7676. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
  7677. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  7678. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
  7679. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7680. msg = NULL;
  7681. if (!ret)
  7682. return 0;
  7683. nla_put_failure:
  7684. nlmsg_free(msg);
  7685. wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
  7686. "%d: %d (%s)", frag, ret, strerror(-ret));
  7687. return ret;
  7688. }
  7689. static int i802_flush(void *priv)
  7690. {
  7691. struct i802_bss *bss = priv;
  7692. struct wpa_driver_nl80211_data *drv = bss->drv;
  7693. struct nl_msg *msg;
  7694. int res;
  7695. msg = nlmsg_alloc();
  7696. if (!msg)
  7697. return -1;
  7698. wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
  7699. bss->ifname);
  7700. nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
  7701. /*
  7702. * XXX: FIX! this needs to flush all VLANs too
  7703. */
  7704. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  7705. if_nametoindex(bss->ifname));
  7706. res = send_and_recv_msgs(drv, msg, NULL, NULL);
  7707. if (res) {
  7708. wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
  7709. "(%s)", res, strerror(-res));
  7710. }
  7711. return res;
  7712. nla_put_failure:
  7713. nlmsg_free(msg);
  7714. return -ENOBUFS;
  7715. }
  7716. static int get_sta_handler(struct nl_msg *msg, void *arg)
  7717. {
  7718. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  7719. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  7720. struct hostap_sta_driver_data *data = arg;
  7721. struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
  7722. static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
  7723. [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
  7724. [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
  7725. [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
  7726. [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
  7727. [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
  7728. [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
  7729. };
  7730. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  7731. genlmsg_attrlen(gnlh, 0), NULL);
  7732. /*
  7733. * TODO: validate the interface and mac address!
  7734. * Otherwise, there's a race condition as soon as
  7735. * the kernel starts sending station notifications.
  7736. */
  7737. if (!tb[NL80211_ATTR_STA_INFO]) {
  7738. wpa_printf(MSG_DEBUG, "sta stats missing!");
  7739. return NL_SKIP;
  7740. }
  7741. if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
  7742. tb[NL80211_ATTR_STA_INFO],
  7743. stats_policy)) {
  7744. wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
  7745. return NL_SKIP;
  7746. }
  7747. if (stats[NL80211_STA_INFO_INACTIVE_TIME])
  7748. data->inactive_msec =
  7749. nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
  7750. if (stats[NL80211_STA_INFO_RX_BYTES])
  7751. data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
  7752. if (stats[NL80211_STA_INFO_TX_BYTES])
  7753. data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
  7754. if (stats[NL80211_STA_INFO_RX_PACKETS])
  7755. data->rx_packets =
  7756. nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
  7757. if (stats[NL80211_STA_INFO_TX_PACKETS])
  7758. data->tx_packets =
  7759. nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
  7760. if (stats[NL80211_STA_INFO_TX_FAILED])
  7761. data->tx_retry_failed =
  7762. nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
  7763. return NL_SKIP;
  7764. }
  7765. static int i802_read_sta_data(struct i802_bss *bss,
  7766. struct hostap_sta_driver_data *data,
  7767. const u8 *addr)
  7768. {
  7769. struct wpa_driver_nl80211_data *drv = bss->drv;
  7770. struct nl_msg *msg;
  7771. os_memset(data, 0, sizeof(*data));
  7772. msg = nlmsg_alloc();
  7773. if (!msg)
  7774. return -ENOMEM;
  7775. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
  7776. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  7777. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  7778. return send_and_recv_msgs(drv, msg, get_sta_handler, data);
  7779. nla_put_failure:
  7780. nlmsg_free(msg);
  7781. return -ENOBUFS;
  7782. }
  7783. static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
  7784. int cw_min, int cw_max, int burst_time)
  7785. {
  7786. struct i802_bss *bss = priv;
  7787. struct wpa_driver_nl80211_data *drv = bss->drv;
  7788. struct nl_msg *msg;
  7789. struct nlattr *txq, *params;
  7790. msg = nlmsg_alloc();
  7791. if (!msg)
  7792. return -1;
  7793. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
  7794. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  7795. txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
  7796. if (!txq)
  7797. goto nla_put_failure;
  7798. /* We are only sending parameters for a single TXQ at a time */
  7799. params = nla_nest_start(msg, 1);
  7800. if (!params)
  7801. goto nla_put_failure;
  7802. switch (queue) {
  7803. case 0:
  7804. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
  7805. break;
  7806. case 1:
  7807. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
  7808. break;
  7809. case 2:
  7810. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
  7811. break;
  7812. case 3:
  7813. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
  7814. break;
  7815. }
  7816. /* Burst time is configured in units of 0.1 msec and TXOP parameter in
  7817. * 32 usec, so need to convert the value here. */
  7818. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
  7819. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
  7820. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
  7821. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
  7822. nla_nest_end(msg, params);
  7823. nla_nest_end(msg, txq);
  7824. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  7825. return 0;
  7826. msg = NULL;
  7827. nla_put_failure:
  7828. nlmsg_free(msg);
  7829. return -1;
  7830. }
  7831. static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
  7832. const char *ifname, int vlan_id)
  7833. {
  7834. struct wpa_driver_nl80211_data *drv = bss->drv;
  7835. struct nl_msg *msg;
  7836. int ret = -ENOBUFS;
  7837. msg = nlmsg_alloc();
  7838. if (!msg)
  7839. return -ENOMEM;
  7840. wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
  7841. ", ifname=%s[%d], vlan_id=%d)",
  7842. bss->ifname, if_nametoindex(bss->ifname),
  7843. MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
  7844. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
  7845. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  7846. if_nametoindex(bss->ifname));
  7847. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  7848. NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
  7849. if_nametoindex(ifname));
  7850. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7851. msg = NULL;
  7852. if (ret < 0) {
  7853. wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
  7854. MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
  7855. MAC2STR(addr), ifname, vlan_id, ret,
  7856. strerror(-ret));
  7857. }
  7858. nla_put_failure:
  7859. nlmsg_free(msg);
  7860. return ret;
  7861. }
  7862. static int i802_get_inact_sec(void *priv, const u8 *addr)
  7863. {
  7864. struct hostap_sta_driver_data data;
  7865. int ret;
  7866. data.inactive_msec = (unsigned long) -1;
  7867. ret = i802_read_sta_data(priv, &data, addr);
  7868. if (ret || data.inactive_msec == (unsigned long) -1)
  7869. return -1;
  7870. return data.inactive_msec / 1000;
  7871. }
  7872. static int i802_sta_clear_stats(void *priv, const u8 *addr)
  7873. {
  7874. #if 0
  7875. /* TODO */
  7876. #endif
  7877. return 0;
  7878. }
  7879. static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
  7880. int reason)
  7881. {
  7882. struct i802_bss *bss = priv;
  7883. struct wpa_driver_nl80211_data *drv = bss->drv;
  7884. struct ieee80211_mgmt mgmt;
  7885. if (drv->device_ap_sme)
  7886. return wpa_driver_nl80211_sta_remove(bss, addr);
  7887. memset(&mgmt, 0, sizeof(mgmt));
  7888. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  7889. WLAN_FC_STYPE_DEAUTH);
  7890. memcpy(mgmt.da, addr, ETH_ALEN);
  7891. memcpy(mgmt.sa, own_addr, ETH_ALEN);
  7892. memcpy(mgmt.bssid, own_addr, ETH_ALEN);
  7893. mgmt.u.deauth.reason_code = host_to_le16(reason);
  7894. return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
  7895. IEEE80211_HDRLEN +
  7896. sizeof(mgmt.u.deauth), 0, 0, 0, 0,
  7897. 0);
  7898. }
  7899. static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
  7900. int reason)
  7901. {
  7902. struct i802_bss *bss = priv;
  7903. struct wpa_driver_nl80211_data *drv = bss->drv;
  7904. struct ieee80211_mgmt mgmt;
  7905. if (drv->device_ap_sme)
  7906. return wpa_driver_nl80211_sta_remove(bss, addr);
  7907. memset(&mgmt, 0, sizeof(mgmt));
  7908. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  7909. WLAN_FC_STYPE_DISASSOC);
  7910. memcpy(mgmt.da, addr, ETH_ALEN);
  7911. memcpy(mgmt.sa, own_addr, ETH_ALEN);
  7912. memcpy(mgmt.bssid, own_addr, ETH_ALEN);
  7913. mgmt.u.disassoc.reason_code = host_to_le16(reason);
  7914. return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
  7915. IEEE80211_HDRLEN +
  7916. sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
  7917. 0);
  7918. }
  7919. static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
  7920. {
  7921. char buf[200], *pos, *end;
  7922. int i, res;
  7923. pos = buf;
  7924. end = pos + sizeof(buf);
  7925. for (i = 0; i < drv->num_if_indices; i++) {
  7926. if (!drv->if_indices[i])
  7927. continue;
  7928. res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]);
  7929. if (res < 0 || res >= end - pos)
  7930. break;
  7931. pos += res;
  7932. }
  7933. *pos = '\0';
  7934. wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
  7935. drv->num_if_indices, buf);
  7936. }
  7937. static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  7938. {
  7939. int i;
  7940. int *old;
  7941. wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
  7942. ifidx);
  7943. if (have_ifidx(drv, ifidx)) {
  7944. wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
  7945. ifidx);
  7946. return;
  7947. }
  7948. for (i = 0; i < drv->num_if_indices; i++) {
  7949. if (drv->if_indices[i] == 0) {
  7950. drv->if_indices[i] = ifidx;
  7951. dump_ifidx(drv);
  7952. return;
  7953. }
  7954. }
  7955. if (drv->if_indices != drv->default_if_indices)
  7956. old = drv->if_indices;
  7957. else
  7958. old = NULL;
  7959. drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
  7960. sizeof(int));
  7961. if (!drv->if_indices) {
  7962. if (!old)
  7963. drv->if_indices = drv->default_if_indices;
  7964. else
  7965. drv->if_indices = old;
  7966. wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
  7967. "interfaces");
  7968. wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
  7969. return;
  7970. } else if (!old)
  7971. os_memcpy(drv->if_indices, drv->default_if_indices,
  7972. sizeof(drv->default_if_indices));
  7973. drv->if_indices[drv->num_if_indices] = ifidx;
  7974. drv->num_if_indices++;
  7975. dump_ifidx(drv);
  7976. }
  7977. static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  7978. {
  7979. int i;
  7980. for (i = 0; i < drv->num_if_indices; i++) {
  7981. if (drv->if_indices[i] == ifidx) {
  7982. drv->if_indices[i] = 0;
  7983. break;
  7984. }
  7985. }
  7986. dump_ifidx(drv);
  7987. }
  7988. static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  7989. {
  7990. int i;
  7991. for (i = 0; i < drv->num_if_indices; i++)
  7992. if (drv->if_indices[i] == ifidx)
  7993. return 1;
  7994. return 0;
  7995. }
  7996. static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
  7997. const char *bridge_ifname, char *ifname_wds)
  7998. {
  7999. struct i802_bss *bss = priv;
  8000. struct wpa_driver_nl80211_data *drv = bss->drv;
  8001. char name[IFNAMSIZ + 1];
  8002. os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
  8003. if (ifname_wds)
  8004. os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
  8005. wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
  8006. " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
  8007. if (val) {
  8008. if (!if_nametoindex(name)) {
  8009. if (nl80211_create_iface(drv, name,
  8010. NL80211_IFTYPE_AP_VLAN,
  8011. bss->addr, 1, NULL, NULL, 0) <
  8012. 0)
  8013. return -1;
  8014. if (bridge_ifname &&
  8015. linux_br_add_if(drv->global->ioctl_sock,
  8016. bridge_ifname, name) < 0)
  8017. return -1;
  8018. }
  8019. if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
  8020. wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
  8021. "interface %s up", name);
  8022. }
  8023. return i802_set_sta_vlan(priv, addr, name, 0);
  8024. } else {
  8025. if (bridge_ifname)
  8026. linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
  8027. name);
  8028. i802_set_sta_vlan(priv, addr, bss->ifname, 0);
  8029. nl80211_remove_iface(drv, if_nametoindex(name));
  8030. return 0;
  8031. }
  8032. }
  8033. static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
  8034. {
  8035. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  8036. struct sockaddr_ll lladdr;
  8037. unsigned char buf[3000];
  8038. int len;
  8039. socklen_t fromlen = sizeof(lladdr);
  8040. len = recvfrom(sock, buf, sizeof(buf), 0,
  8041. (struct sockaddr *)&lladdr, &fromlen);
  8042. if (len < 0) {
  8043. wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
  8044. strerror(errno));
  8045. return;
  8046. }
  8047. if (have_ifidx(drv, lladdr.sll_ifindex))
  8048. drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
  8049. }
  8050. static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
  8051. struct i802_bss *bss,
  8052. const char *brname, const char *ifname)
  8053. {
  8054. int ifindex;
  8055. char in_br[IFNAMSIZ];
  8056. os_strlcpy(bss->brname, brname, IFNAMSIZ);
  8057. ifindex = if_nametoindex(brname);
  8058. if (ifindex == 0) {
  8059. /*
  8060. * Bridge was configured, but the bridge device does
  8061. * not exist. Try to add it now.
  8062. */
  8063. if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
  8064. wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
  8065. "bridge interface %s: %s",
  8066. brname, strerror(errno));
  8067. return -1;
  8068. }
  8069. bss->added_bridge = 1;
  8070. add_ifidx(drv, if_nametoindex(brname));
  8071. }
  8072. if (linux_br_get(in_br, ifname) == 0) {
  8073. if (os_strcmp(in_br, brname) == 0)
  8074. return 0; /* already in the bridge */
  8075. wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
  8076. "bridge %s", ifname, in_br);
  8077. if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
  8078. 0) {
  8079. wpa_printf(MSG_ERROR, "nl80211: Failed to "
  8080. "remove interface %s from bridge "
  8081. "%s: %s",
  8082. ifname, brname, strerror(errno));
  8083. return -1;
  8084. }
  8085. }
  8086. wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
  8087. ifname, brname);
  8088. if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
  8089. wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
  8090. "into bridge %s: %s",
  8091. ifname, brname, strerror(errno));
  8092. return -1;
  8093. }
  8094. bss->added_if_into_bridge = 1;
  8095. return 0;
  8096. }
  8097. static void *i802_init(struct hostapd_data *hapd,
  8098. struct wpa_init_params *params)
  8099. {
  8100. struct wpa_driver_nl80211_data *drv;
  8101. struct i802_bss *bss;
  8102. size_t i;
  8103. char brname[IFNAMSIZ];
  8104. int ifindex, br_ifindex;
  8105. int br_added = 0;
  8106. bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
  8107. params->global_priv, 1,
  8108. params->bssid);
  8109. if (bss == NULL)
  8110. return NULL;
  8111. drv = bss->drv;
  8112. if (linux_br_get(brname, params->ifname) == 0) {
  8113. wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
  8114. params->ifname, brname);
  8115. br_ifindex = if_nametoindex(brname);
  8116. } else {
  8117. brname[0] = '\0';
  8118. br_ifindex = 0;
  8119. }
  8120. for (i = 0; i < params->num_bridge; i++) {
  8121. if (params->bridge[i]) {
  8122. ifindex = if_nametoindex(params->bridge[i]);
  8123. if (ifindex)
  8124. add_ifidx(drv, ifindex);
  8125. if (ifindex == br_ifindex)
  8126. br_added = 1;
  8127. }
  8128. }
  8129. if (!br_added && br_ifindex &&
  8130. (params->num_bridge == 0 || !params->bridge[0]))
  8131. add_ifidx(drv, br_ifindex);
  8132. /* start listening for EAPOL on the default AP interface */
  8133. add_ifidx(drv, drv->ifindex);
  8134. if (params->num_bridge && params->bridge[0] &&
  8135. i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
  8136. goto failed;
  8137. drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
  8138. if (drv->eapol_sock < 0) {
  8139. wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
  8140. strerror(errno));
  8141. goto failed;
  8142. }
  8143. if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
  8144. {
  8145. wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
  8146. goto failed;
  8147. }
  8148. if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  8149. params->own_addr))
  8150. goto failed;
  8151. memcpy(bss->addr, params->own_addr, ETH_ALEN);
  8152. return bss;
  8153. failed:
  8154. wpa_driver_nl80211_deinit(bss);
  8155. return NULL;
  8156. }
  8157. static void i802_deinit(void *priv)
  8158. {
  8159. struct i802_bss *bss = priv;
  8160. wpa_driver_nl80211_deinit(bss);
  8161. }
  8162. static enum nl80211_iftype wpa_driver_nl80211_if_type(
  8163. enum wpa_driver_if_type type)
  8164. {
  8165. switch (type) {
  8166. case WPA_IF_STATION:
  8167. return NL80211_IFTYPE_STATION;
  8168. case WPA_IF_P2P_CLIENT:
  8169. case WPA_IF_P2P_GROUP:
  8170. return NL80211_IFTYPE_P2P_CLIENT;
  8171. case WPA_IF_AP_VLAN:
  8172. return NL80211_IFTYPE_AP_VLAN;
  8173. case WPA_IF_AP_BSS:
  8174. return NL80211_IFTYPE_AP;
  8175. case WPA_IF_P2P_GO:
  8176. return NL80211_IFTYPE_P2P_GO;
  8177. case WPA_IF_P2P_DEVICE:
  8178. return NL80211_IFTYPE_P2P_DEVICE;
  8179. }
  8180. return -1;
  8181. }
  8182. #ifdef CONFIG_P2P
  8183. static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
  8184. {
  8185. struct wpa_driver_nl80211_data *drv;
  8186. dl_list_for_each(drv, &global->interfaces,
  8187. struct wpa_driver_nl80211_data, list) {
  8188. if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
  8189. return 1;
  8190. }
  8191. return 0;
  8192. }
  8193. static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
  8194. u8 *new_addr)
  8195. {
  8196. unsigned int idx;
  8197. if (!drv->global)
  8198. return -1;
  8199. os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
  8200. for (idx = 0; idx < 64; idx++) {
  8201. new_addr[0] = drv->first_bss->addr[0] | 0x02;
  8202. new_addr[0] ^= idx << 2;
  8203. if (!nl80211_addr_in_use(drv->global, new_addr))
  8204. break;
  8205. }
  8206. if (idx == 64)
  8207. return -1;
  8208. wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
  8209. MACSTR, MAC2STR(new_addr));
  8210. return 0;
  8211. }
  8212. #endif /* CONFIG_P2P */
  8213. struct wdev_info {
  8214. u64 wdev_id;
  8215. int wdev_id_set;
  8216. u8 macaddr[ETH_ALEN];
  8217. };
  8218. static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
  8219. {
  8220. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  8221. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  8222. struct wdev_info *wi = arg;
  8223. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  8224. genlmsg_attrlen(gnlh, 0), NULL);
  8225. if (tb[NL80211_ATTR_WDEV]) {
  8226. wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
  8227. wi->wdev_id_set = 1;
  8228. }
  8229. if (tb[NL80211_ATTR_MAC])
  8230. os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
  8231. ETH_ALEN);
  8232. return NL_SKIP;
  8233. }
  8234. static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
  8235. const char *ifname, const u8 *addr,
  8236. void *bss_ctx, void **drv_priv,
  8237. char *force_ifname, u8 *if_addr,
  8238. const char *bridge, int use_existing)
  8239. {
  8240. enum nl80211_iftype nlmode;
  8241. struct i802_bss *bss = priv;
  8242. struct wpa_driver_nl80211_data *drv = bss->drv;
  8243. int ifidx;
  8244. int added = 1;
  8245. if (addr)
  8246. os_memcpy(if_addr, addr, ETH_ALEN);
  8247. nlmode = wpa_driver_nl80211_if_type(type);
  8248. if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
  8249. struct wdev_info p2pdev_info;
  8250. os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
  8251. ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
  8252. 0, nl80211_wdev_handler,
  8253. &p2pdev_info, use_existing);
  8254. if (!p2pdev_info.wdev_id_set || ifidx != 0) {
  8255. wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
  8256. ifname);
  8257. return -1;
  8258. }
  8259. drv->global->if_add_wdevid = p2pdev_info.wdev_id;
  8260. drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
  8261. if (!is_zero_ether_addr(p2pdev_info.macaddr))
  8262. os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
  8263. wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
  8264. ifname,
  8265. (long long unsigned int) p2pdev_info.wdev_id);
  8266. } else {
  8267. ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
  8268. 0, NULL, NULL, use_existing);
  8269. if (use_existing && ifidx == -ENFILE) {
  8270. added = 0;
  8271. ifidx = if_nametoindex(ifname);
  8272. } else if (ifidx < 0) {
  8273. return -1;
  8274. }
  8275. }
  8276. if (!addr) {
  8277. if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
  8278. os_memcpy(if_addr, bss->addr, ETH_ALEN);
  8279. else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
  8280. bss->ifname, if_addr) < 0) {
  8281. if (added)
  8282. nl80211_remove_iface(drv, ifidx);
  8283. return -1;
  8284. }
  8285. }
  8286. #ifdef CONFIG_P2P
  8287. if (!addr &&
  8288. (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
  8289. type == WPA_IF_P2P_GO)) {
  8290. /* Enforce unique P2P Interface Address */
  8291. u8 new_addr[ETH_ALEN];
  8292. if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
  8293. new_addr) < 0) {
  8294. nl80211_remove_iface(drv, ifidx);
  8295. return -1;
  8296. }
  8297. if (nl80211_addr_in_use(drv->global, new_addr)) {
  8298. wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
  8299. "for P2P group interface");
  8300. if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
  8301. nl80211_remove_iface(drv, ifidx);
  8302. return -1;
  8303. }
  8304. if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
  8305. new_addr) < 0) {
  8306. nl80211_remove_iface(drv, ifidx);
  8307. return -1;
  8308. }
  8309. }
  8310. os_memcpy(if_addr, new_addr, ETH_ALEN);
  8311. }
  8312. #endif /* CONFIG_P2P */
  8313. if (type == WPA_IF_AP_BSS) {
  8314. struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
  8315. if (new_bss == NULL) {
  8316. if (added)
  8317. nl80211_remove_iface(drv, ifidx);
  8318. return -1;
  8319. }
  8320. if (bridge &&
  8321. i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
  8322. wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
  8323. "interface %s to a bridge %s",
  8324. ifname, bridge);
  8325. if (added)
  8326. nl80211_remove_iface(drv, ifidx);
  8327. os_free(new_bss);
  8328. return -1;
  8329. }
  8330. if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
  8331. {
  8332. nl80211_remove_iface(drv, ifidx);
  8333. os_free(new_bss);
  8334. return -1;
  8335. }
  8336. os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
  8337. os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
  8338. new_bss->ifindex = ifidx;
  8339. new_bss->drv = drv;
  8340. new_bss->next = drv->first_bss->next;
  8341. new_bss->freq = drv->first_bss->freq;
  8342. new_bss->ctx = bss_ctx;
  8343. new_bss->added_if = added;
  8344. drv->first_bss->next = new_bss;
  8345. if (drv_priv)
  8346. *drv_priv = new_bss;
  8347. nl80211_init_bss(new_bss);
  8348. /* Subscribe management frames for this WPA_IF_AP_BSS */
  8349. if (nl80211_setup_ap(new_bss))
  8350. return -1;
  8351. }
  8352. if (drv->global)
  8353. drv->global->if_add_ifindex = ifidx;
  8354. if (ifidx > 0)
  8355. add_ifidx(drv, ifidx);
  8356. return 0;
  8357. }
  8358. static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
  8359. enum wpa_driver_if_type type,
  8360. const char *ifname)
  8361. {
  8362. struct wpa_driver_nl80211_data *drv = bss->drv;
  8363. int ifindex = if_nametoindex(ifname);
  8364. wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
  8365. __func__, type, ifname, ifindex, bss->added_if);
  8366. if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
  8367. nl80211_remove_iface(drv, ifindex);
  8368. else if (ifindex > 0 && !bss->added_if)
  8369. del_ifidx(drv, ifindex);
  8370. if (type != WPA_IF_AP_BSS)
  8371. return 0;
  8372. if (bss->added_if_into_bridge) {
  8373. if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
  8374. bss->ifname) < 0)
  8375. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  8376. "interface %s from bridge %s: %s",
  8377. bss->ifname, bss->brname, strerror(errno));
  8378. }
  8379. if (bss->added_bridge) {
  8380. if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
  8381. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  8382. "bridge %s: %s",
  8383. bss->brname, strerror(errno));
  8384. }
  8385. if (bss != drv->first_bss) {
  8386. struct i802_bss *tbss;
  8387. wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
  8388. for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
  8389. if (tbss->next == bss) {
  8390. tbss->next = bss->next;
  8391. /* Unsubscribe management frames */
  8392. nl80211_teardown_ap(bss);
  8393. nl80211_destroy_bss(bss);
  8394. os_free(bss);
  8395. bss = NULL;
  8396. break;
  8397. }
  8398. }
  8399. if (bss)
  8400. wpa_printf(MSG_INFO, "nl80211: %s - could not find "
  8401. "BSS %p in the list", __func__, bss);
  8402. } else {
  8403. wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
  8404. nl80211_teardown_ap(bss);
  8405. if (!bss->added_if && !drv->first_bss->next)
  8406. wpa_driver_nl80211_del_beacon(drv);
  8407. nl80211_destroy_bss(bss);
  8408. if (!bss->added_if)
  8409. i802_set_iface_flags(bss, 0);
  8410. if (drv->first_bss->next) {
  8411. drv->first_bss = drv->first_bss->next;
  8412. drv->ctx = drv->first_bss->ctx;
  8413. os_free(bss);
  8414. } else {
  8415. wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
  8416. }
  8417. }
  8418. return 0;
  8419. }
  8420. static int cookie_handler(struct nl_msg *msg, void *arg)
  8421. {
  8422. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  8423. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  8424. u64 *cookie = arg;
  8425. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  8426. genlmsg_attrlen(gnlh, 0), NULL);
  8427. if (tb[NL80211_ATTR_COOKIE])
  8428. *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
  8429. return NL_SKIP;
  8430. }
  8431. static int nl80211_send_frame_cmd(struct i802_bss *bss,
  8432. unsigned int freq, unsigned int wait,
  8433. const u8 *buf, size_t buf_len,
  8434. u64 *cookie_out, int no_cck, int no_ack,
  8435. int offchanok)
  8436. {
  8437. struct wpa_driver_nl80211_data *drv = bss->drv;
  8438. struct nl_msg *msg;
  8439. u64 cookie;
  8440. int ret = -1;
  8441. msg = nlmsg_alloc();
  8442. if (!msg)
  8443. return -1;
  8444. wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
  8445. "no_ack=%d offchanok=%d",
  8446. freq, wait, no_cck, no_ack, offchanok);
  8447. wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
  8448. nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
  8449. if (nl80211_set_iface_id(msg, bss) < 0)
  8450. goto nla_put_failure;
  8451. if (freq)
  8452. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
  8453. if (wait)
  8454. NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
  8455. if (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
  8456. drv->test_use_roc_tx))
  8457. NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
  8458. if (no_cck)
  8459. NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
  8460. if (no_ack)
  8461. NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
  8462. NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
  8463. cookie = 0;
  8464. ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
  8465. msg = NULL;
  8466. if (ret) {
  8467. wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
  8468. "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
  8469. freq, wait);
  8470. goto nla_put_failure;
  8471. }
  8472. wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
  8473. "cookie 0x%llx", no_ack ? " (no ACK)" : "",
  8474. (long long unsigned int) cookie);
  8475. if (cookie_out)
  8476. *cookie_out = no_ack ? (u64) -1 : cookie;
  8477. nla_put_failure:
  8478. nlmsg_free(msg);
  8479. return ret;
  8480. }
  8481. static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
  8482. unsigned int freq,
  8483. unsigned int wait_time,
  8484. const u8 *dst, const u8 *src,
  8485. const u8 *bssid,
  8486. const u8 *data, size_t data_len,
  8487. int no_cck)
  8488. {
  8489. struct wpa_driver_nl80211_data *drv = bss->drv;
  8490. int ret = -1;
  8491. u8 *buf;
  8492. struct ieee80211_hdr *hdr;
  8493. wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
  8494. "freq=%u MHz wait=%d ms no_cck=%d)",
  8495. drv->ifindex, freq, wait_time, no_cck);
  8496. buf = os_zalloc(24 + data_len);
  8497. if (buf == NULL)
  8498. return ret;
  8499. os_memcpy(buf + 24, data, data_len);
  8500. hdr = (struct ieee80211_hdr *) buf;
  8501. hdr->frame_control =
  8502. IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
  8503. os_memcpy(hdr->addr1, dst, ETH_ALEN);
  8504. os_memcpy(hdr->addr2, src, ETH_ALEN);
  8505. os_memcpy(hdr->addr3, bssid, ETH_ALEN);
  8506. if (is_ap_interface(drv->nlmode) &&
  8507. (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
  8508. (int) freq == bss->freq || drv->device_ap_sme ||
  8509. !drv->use_monitor))
  8510. ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
  8511. 0, freq, no_cck, 1,
  8512. wait_time);
  8513. else
  8514. ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
  8515. 24 + data_len,
  8516. &drv->send_action_cookie,
  8517. no_cck, 0, 1);
  8518. os_free(buf);
  8519. return ret;
  8520. }
  8521. static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
  8522. {
  8523. struct i802_bss *bss = priv;
  8524. struct wpa_driver_nl80211_data *drv = bss->drv;
  8525. struct nl_msg *msg;
  8526. int ret;
  8527. msg = nlmsg_alloc();
  8528. if (!msg)
  8529. return;
  8530. wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
  8531. (long long unsigned int) drv->send_action_cookie);
  8532. nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
  8533. if (nl80211_set_iface_id(msg, bss) < 0)
  8534. goto nla_put_failure;
  8535. NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
  8536. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  8537. msg = NULL;
  8538. if (ret)
  8539. wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
  8540. "(%s)", ret, strerror(-ret));
  8541. nla_put_failure:
  8542. nlmsg_free(msg);
  8543. }
  8544. static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
  8545. unsigned int duration)
  8546. {
  8547. struct i802_bss *bss = priv;
  8548. struct wpa_driver_nl80211_data *drv = bss->drv;
  8549. struct nl_msg *msg;
  8550. int ret;
  8551. u64 cookie;
  8552. msg = nlmsg_alloc();
  8553. if (!msg)
  8554. return -1;
  8555. nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
  8556. if (nl80211_set_iface_id(msg, bss) < 0)
  8557. goto nla_put_failure;
  8558. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
  8559. NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
  8560. cookie = 0;
  8561. ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
  8562. msg = NULL;
  8563. if (ret == 0) {
  8564. wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
  8565. "0x%llx for freq=%u MHz duration=%u",
  8566. (long long unsigned int) cookie, freq, duration);
  8567. drv->remain_on_chan_cookie = cookie;
  8568. drv->pending_remain_on_chan = 1;
  8569. return 0;
  8570. }
  8571. wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
  8572. "(freq=%d duration=%u): %d (%s)",
  8573. freq, duration, ret, strerror(-ret));
  8574. nla_put_failure:
  8575. nlmsg_free(msg);
  8576. return -1;
  8577. }
  8578. static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
  8579. {
  8580. struct i802_bss *bss = priv;
  8581. struct wpa_driver_nl80211_data *drv = bss->drv;
  8582. struct nl_msg *msg;
  8583. int ret;
  8584. if (!drv->pending_remain_on_chan) {
  8585. wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
  8586. "to cancel");
  8587. return -1;
  8588. }
  8589. wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
  8590. "0x%llx",
  8591. (long long unsigned int) drv->remain_on_chan_cookie);
  8592. msg = nlmsg_alloc();
  8593. if (!msg)
  8594. return -1;
  8595. nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
  8596. if (nl80211_set_iface_id(msg, bss) < 0)
  8597. goto nla_put_failure;
  8598. NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
  8599. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  8600. msg = NULL;
  8601. if (ret == 0)
  8602. return 0;
  8603. wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
  8604. "%d (%s)", ret, strerror(-ret));
  8605. nla_put_failure:
  8606. nlmsg_free(msg);
  8607. return -1;
  8608. }
  8609. static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
  8610. {
  8611. struct wpa_driver_nl80211_data *drv = bss->drv;
  8612. if (!report) {
  8613. if (bss->nl_preq && drv->device_ap_sme &&
  8614. is_ap_interface(drv->nlmode)) {
  8615. /*
  8616. * Do not disable Probe Request reporting that was
  8617. * enabled in nl80211_setup_ap().
  8618. */
  8619. wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
  8620. "Probe Request reporting nl_preq=%p while "
  8621. "in AP mode", bss->nl_preq);
  8622. } else if (bss->nl_preq) {
  8623. wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
  8624. "reporting nl_preq=%p", bss->nl_preq);
  8625. nl80211_destroy_eloop_handle(&bss->nl_preq);
  8626. }
  8627. return 0;
  8628. }
  8629. if (bss->nl_preq) {
  8630. wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
  8631. "already on! nl_preq=%p", bss->nl_preq);
  8632. return 0;
  8633. }
  8634. bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
  8635. if (bss->nl_preq == NULL)
  8636. return -1;
  8637. wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
  8638. "reporting nl_preq=%p", bss->nl_preq);
  8639. if (nl80211_register_frame(bss, bss->nl_preq,
  8640. (WLAN_FC_TYPE_MGMT << 2) |
  8641. (WLAN_FC_STYPE_PROBE_REQ << 4),
  8642. NULL, 0) < 0)
  8643. goto out_err;
  8644. nl80211_register_eloop_read(&bss->nl_preq,
  8645. wpa_driver_nl80211_event_receive,
  8646. bss->nl_cb);
  8647. return 0;
  8648. out_err:
  8649. nl_destroy_handles(&bss->nl_preq);
  8650. return -1;
  8651. }
  8652. static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
  8653. int ifindex, int disabled)
  8654. {
  8655. struct nl_msg *msg;
  8656. struct nlattr *bands, *band;
  8657. int ret;
  8658. msg = nlmsg_alloc();
  8659. if (!msg)
  8660. return -1;
  8661. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
  8662. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  8663. bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
  8664. if (!bands)
  8665. goto nla_put_failure;
  8666. /*
  8667. * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
  8668. * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
  8669. * rates. All 5 GHz rates are left enabled.
  8670. */
  8671. band = nla_nest_start(msg, NL80211_BAND_2GHZ);
  8672. if (!band)
  8673. goto nla_put_failure;
  8674. if (disabled) {
  8675. NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
  8676. "\x0c\x12\x18\x24\x30\x48\x60\x6c");
  8677. }
  8678. nla_nest_end(msg, band);
  8679. nla_nest_end(msg, bands);
  8680. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  8681. msg = NULL;
  8682. if (ret) {
  8683. wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
  8684. "(%s)", ret, strerror(-ret));
  8685. } else
  8686. drv->disabled_11b_rates = disabled;
  8687. return ret;
  8688. nla_put_failure:
  8689. nlmsg_free(msg);
  8690. return -1;
  8691. }
  8692. static int wpa_driver_nl80211_deinit_ap(void *priv)
  8693. {
  8694. struct i802_bss *bss = priv;
  8695. struct wpa_driver_nl80211_data *drv = bss->drv;
  8696. if (!is_ap_interface(drv->nlmode))
  8697. return -1;
  8698. wpa_driver_nl80211_del_beacon(drv);
  8699. /*
  8700. * If the P2P GO interface was dynamically added, then it is
  8701. * possible that the interface change to station is not possible.
  8702. */
  8703. if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
  8704. return 0;
  8705. return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
  8706. }
  8707. static int wpa_driver_nl80211_stop_ap(void *priv)
  8708. {
  8709. struct i802_bss *bss = priv;
  8710. struct wpa_driver_nl80211_data *drv = bss->drv;
  8711. if (!is_ap_interface(drv->nlmode))
  8712. return -1;
  8713. wpa_driver_nl80211_del_beacon(drv);
  8714. bss->beacon_set = 0;
  8715. return 0;
  8716. }
  8717. static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
  8718. {
  8719. struct i802_bss *bss = priv;
  8720. struct wpa_driver_nl80211_data *drv = bss->drv;
  8721. if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
  8722. return -1;
  8723. /*
  8724. * If the P2P Client interface was dynamically added, then it is
  8725. * possible that the interface change to station is not possible.
  8726. */
  8727. if (bss->if_dynamic)
  8728. return 0;
  8729. return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
  8730. }
  8731. static void wpa_driver_nl80211_resume(void *priv)
  8732. {
  8733. struct i802_bss *bss = priv;
  8734. if (i802_set_iface_flags(bss, 1))
  8735. wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
  8736. }
  8737. static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
  8738. const u8 *ies, size_t ies_len)
  8739. {
  8740. struct i802_bss *bss = priv;
  8741. struct wpa_driver_nl80211_data *drv = bss->drv;
  8742. int ret;
  8743. u8 *data, *pos;
  8744. size_t data_len;
  8745. const u8 *own_addr = bss->addr;
  8746. if (action != 1) {
  8747. wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
  8748. "action %d", action);
  8749. return -1;
  8750. }
  8751. /*
  8752. * Action frame payload:
  8753. * Category[1] = 6 (Fast BSS Transition)
  8754. * Action[1] = 1 (Fast BSS Transition Request)
  8755. * STA Address
  8756. * Target AP Address
  8757. * FT IEs
  8758. */
  8759. data_len = 2 + 2 * ETH_ALEN + ies_len;
  8760. data = os_malloc(data_len);
  8761. if (data == NULL)
  8762. return -1;
  8763. pos = data;
  8764. *pos++ = 0x06; /* FT Action category */
  8765. *pos++ = action;
  8766. os_memcpy(pos, own_addr, ETH_ALEN);
  8767. pos += ETH_ALEN;
  8768. os_memcpy(pos, target_ap, ETH_ALEN);
  8769. pos += ETH_ALEN;
  8770. os_memcpy(pos, ies, ies_len);
  8771. ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
  8772. drv->bssid, own_addr, drv->bssid,
  8773. data, data_len, 0);
  8774. os_free(data);
  8775. return ret;
  8776. }
  8777. static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
  8778. {
  8779. struct i802_bss *bss = priv;
  8780. struct wpa_driver_nl80211_data *drv = bss->drv;
  8781. struct nl_msg *msg;
  8782. struct nlattr *cqm;
  8783. int ret = -1;
  8784. wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
  8785. "hysteresis=%d", threshold, hysteresis);
  8786. msg = nlmsg_alloc();
  8787. if (!msg)
  8788. return -1;
  8789. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
  8790. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  8791. cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
  8792. if (cqm == NULL)
  8793. goto nla_put_failure;
  8794. NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
  8795. NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
  8796. nla_nest_end(msg, cqm);
  8797. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  8798. msg = NULL;
  8799. nla_put_failure:
  8800. nlmsg_free(msg);
  8801. return ret;
  8802. }
  8803. static int get_channel_width(struct nl_msg *msg, void *arg)
  8804. {
  8805. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  8806. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  8807. struct wpa_signal_info *sig_change = arg;
  8808. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  8809. genlmsg_attrlen(gnlh, 0), NULL);
  8810. sig_change->center_frq1 = -1;
  8811. sig_change->center_frq2 = -1;
  8812. sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
  8813. if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
  8814. sig_change->chanwidth = convert2width(
  8815. nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
  8816. if (tb[NL80211_ATTR_CENTER_FREQ1])
  8817. sig_change->center_frq1 =
  8818. nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
  8819. if (tb[NL80211_ATTR_CENTER_FREQ2])
  8820. sig_change->center_frq2 =
  8821. nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
  8822. }
  8823. return NL_SKIP;
  8824. }
  8825. static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
  8826. struct wpa_signal_info *sig)
  8827. {
  8828. struct nl_msg *msg;
  8829. msg = nlmsg_alloc();
  8830. if (!msg)
  8831. return -ENOMEM;
  8832. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
  8833. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  8834. return send_and_recv_msgs(drv, msg, get_channel_width, sig);
  8835. nla_put_failure:
  8836. nlmsg_free(msg);
  8837. return -ENOBUFS;
  8838. }
  8839. static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
  8840. {
  8841. struct i802_bss *bss = priv;
  8842. struct wpa_driver_nl80211_data *drv = bss->drv;
  8843. int res;
  8844. os_memset(si, 0, sizeof(*si));
  8845. res = nl80211_get_link_signal(drv, si);
  8846. if (res != 0)
  8847. return res;
  8848. res = nl80211_get_channel_width(drv, si);
  8849. if (res != 0)
  8850. return res;
  8851. return nl80211_get_link_noise(drv, si);
  8852. }
  8853. static int wpa_driver_nl80211_shared_freq(void *priv)
  8854. {
  8855. struct i802_bss *bss = priv;
  8856. struct wpa_driver_nl80211_data *drv = bss->drv;
  8857. struct wpa_driver_nl80211_data *driver;
  8858. int freq = 0;
  8859. /*
  8860. * If the same PHY is in connected state with some other interface,
  8861. * then retrieve the assoc freq.
  8862. */
  8863. wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
  8864. drv->phyname);
  8865. dl_list_for_each(driver, &drv->global->interfaces,
  8866. struct wpa_driver_nl80211_data, list) {
  8867. if (drv == driver ||
  8868. os_strcmp(drv->phyname, driver->phyname) != 0 ||
  8869. !driver->associated)
  8870. continue;
  8871. wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
  8872. MACSTR,
  8873. driver->phyname, driver->first_bss->ifname,
  8874. MAC2STR(driver->first_bss->addr));
  8875. if (is_ap_interface(driver->nlmode))
  8876. freq = driver->first_bss->freq;
  8877. else
  8878. freq = nl80211_get_assoc_freq(driver);
  8879. wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
  8880. drv->phyname, freq);
  8881. }
  8882. if (!freq)
  8883. wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
  8884. "PHY (%s) in associated state", drv->phyname);
  8885. return freq;
  8886. }
  8887. static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
  8888. int encrypt)
  8889. {
  8890. struct i802_bss *bss = priv;
  8891. return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
  8892. 0, 0, 0, 0);
  8893. }
  8894. static int nl80211_set_param(void *priv, const char *param)
  8895. {
  8896. wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
  8897. if (param == NULL)
  8898. return 0;
  8899. #ifdef CONFIG_P2P
  8900. if (os_strstr(param, "use_p2p_group_interface=1")) {
  8901. struct i802_bss *bss = priv;
  8902. struct wpa_driver_nl80211_data *drv = bss->drv;
  8903. wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
  8904. "interface");
  8905. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
  8906. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
  8907. }
  8908. if (os_strstr(param, "p2p_device=1")) {
  8909. struct i802_bss *bss = priv;
  8910. struct wpa_driver_nl80211_data *drv = bss->drv;
  8911. drv->allow_p2p_device = 1;
  8912. }
  8913. #endif /* CONFIG_P2P */
  8914. if (os_strstr(param, "use_monitor=1")) {
  8915. struct i802_bss *bss = priv;
  8916. struct wpa_driver_nl80211_data *drv = bss->drv;
  8917. drv->use_monitor = 1;
  8918. }
  8919. if (os_strstr(param, "force_connect_cmd=1")) {
  8920. struct i802_bss *bss = priv;
  8921. struct wpa_driver_nl80211_data *drv = bss->drv;
  8922. drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
  8923. }
  8924. if (os_strstr(param, "no_offchannel_tx=1")) {
  8925. struct i802_bss *bss = priv;
  8926. struct wpa_driver_nl80211_data *drv = bss->drv;
  8927. drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
  8928. drv->test_use_roc_tx = 1;
  8929. }
  8930. return 0;
  8931. }
  8932. static void * nl80211_global_init(void)
  8933. {
  8934. struct nl80211_global *global;
  8935. struct netlink_config *cfg;
  8936. global = os_zalloc(sizeof(*global));
  8937. if (global == NULL)
  8938. return NULL;
  8939. global->ioctl_sock = -1;
  8940. dl_list_init(&global->interfaces);
  8941. global->if_add_ifindex = -1;
  8942. cfg = os_zalloc(sizeof(*cfg));
  8943. if (cfg == NULL)
  8944. goto err;
  8945. cfg->ctx = global;
  8946. cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
  8947. cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
  8948. global->netlink = netlink_init(cfg);
  8949. if (global->netlink == NULL) {
  8950. os_free(cfg);
  8951. goto err;
  8952. }
  8953. if (wpa_driver_nl80211_init_nl_global(global) < 0)
  8954. goto err;
  8955. global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
  8956. if (global->ioctl_sock < 0) {
  8957. wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
  8958. strerror(errno));
  8959. goto err;
  8960. }
  8961. return global;
  8962. err:
  8963. nl80211_global_deinit(global);
  8964. return NULL;
  8965. }
  8966. static void nl80211_global_deinit(void *priv)
  8967. {
  8968. struct nl80211_global *global = priv;
  8969. if (global == NULL)
  8970. return;
  8971. if (!dl_list_empty(&global->interfaces)) {
  8972. wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
  8973. "nl80211_global_deinit",
  8974. dl_list_len(&global->interfaces));
  8975. }
  8976. if (global->netlink)
  8977. netlink_deinit(global->netlink);
  8978. nl_destroy_handles(&global->nl);
  8979. if (global->nl_event)
  8980. nl80211_destroy_eloop_handle(&global->nl_event);
  8981. nl_cb_put(global->nl_cb);
  8982. if (global->ioctl_sock >= 0)
  8983. close(global->ioctl_sock);
  8984. os_free(global);
  8985. }
  8986. static const char * nl80211_get_radio_name(void *priv)
  8987. {
  8988. struct i802_bss *bss = priv;
  8989. struct wpa_driver_nl80211_data *drv = bss->drv;
  8990. return drv->phyname;
  8991. }
  8992. static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
  8993. const u8 *pmkid)
  8994. {
  8995. struct nl_msg *msg;
  8996. msg = nlmsg_alloc();
  8997. if (!msg)
  8998. return -ENOMEM;
  8999. nl80211_cmd(bss->drv, msg, 0, cmd);
  9000. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  9001. if (pmkid)
  9002. NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
  9003. if (bssid)
  9004. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
  9005. return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  9006. nla_put_failure:
  9007. nlmsg_free(msg);
  9008. return -ENOBUFS;
  9009. }
  9010. static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
  9011. {
  9012. struct i802_bss *bss = priv;
  9013. wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
  9014. return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
  9015. }
  9016. static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
  9017. {
  9018. struct i802_bss *bss = priv;
  9019. wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
  9020. MAC2STR(bssid));
  9021. return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
  9022. }
  9023. static int nl80211_flush_pmkid(void *priv)
  9024. {
  9025. struct i802_bss *bss = priv;
  9026. wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
  9027. return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
  9028. }
  9029. static void clean_survey_results(struct survey_results *survey_results)
  9030. {
  9031. struct freq_survey *survey, *tmp;
  9032. if (dl_list_empty(&survey_results->survey_list))
  9033. return;
  9034. dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
  9035. struct freq_survey, list) {
  9036. dl_list_del(&survey->list);
  9037. os_free(survey);
  9038. }
  9039. }
  9040. static void add_survey(struct nlattr **sinfo, u32 ifidx,
  9041. struct dl_list *survey_list)
  9042. {
  9043. struct freq_survey *survey;
  9044. survey = os_zalloc(sizeof(struct freq_survey));
  9045. if (!survey)
  9046. return;
  9047. survey->ifidx = ifidx;
  9048. survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
  9049. survey->filled = 0;
  9050. if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
  9051. survey->nf = (int8_t)
  9052. nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
  9053. survey->filled |= SURVEY_HAS_NF;
  9054. }
  9055. if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
  9056. survey->channel_time =
  9057. nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
  9058. survey->filled |= SURVEY_HAS_CHAN_TIME;
  9059. }
  9060. if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
  9061. survey->channel_time_busy =
  9062. nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
  9063. survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
  9064. }
  9065. if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
  9066. survey->channel_time_rx =
  9067. nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
  9068. survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
  9069. }
  9070. if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
  9071. survey->channel_time_tx =
  9072. nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
  9073. survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
  9074. }
  9075. 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)",
  9076. survey->freq,
  9077. survey->nf,
  9078. (unsigned long int) survey->channel_time,
  9079. (unsigned long int) survey->channel_time_busy,
  9080. (unsigned long int) survey->channel_time_tx,
  9081. (unsigned long int) survey->channel_time_rx,
  9082. survey->filled);
  9083. dl_list_add_tail(survey_list, &survey->list);
  9084. }
  9085. static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
  9086. unsigned int freq_filter)
  9087. {
  9088. if (!freq_filter)
  9089. return 1;
  9090. return freq_filter == surveyed_freq;
  9091. }
  9092. static int survey_handler(struct nl_msg *msg, void *arg)
  9093. {
  9094. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  9095. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  9096. struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
  9097. struct survey_results *survey_results;
  9098. u32 surveyed_freq = 0;
  9099. u32 ifidx;
  9100. static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
  9101. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  9102. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  9103. };
  9104. survey_results = (struct survey_results *) arg;
  9105. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  9106. genlmsg_attrlen(gnlh, 0), NULL);
  9107. if (!tb[NL80211_ATTR_IFINDEX])
  9108. return NL_SKIP;
  9109. ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
  9110. if (!tb[NL80211_ATTR_SURVEY_INFO])
  9111. return NL_SKIP;
  9112. if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
  9113. tb[NL80211_ATTR_SURVEY_INFO],
  9114. survey_policy))
  9115. return NL_SKIP;
  9116. if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
  9117. wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
  9118. return NL_SKIP;
  9119. }
  9120. surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
  9121. if (!check_survey_ok(sinfo, surveyed_freq,
  9122. survey_results->freq_filter))
  9123. return NL_SKIP;
  9124. if (survey_results->freq_filter &&
  9125. survey_results->freq_filter != surveyed_freq) {
  9126. wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
  9127. surveyed_freq);
  9128. return NL_SKIP;
  9129. }
  9130. add_survey(sinfo, ifidx, &survey_results->survey_list);
  9131. return NL_SKIP;
  9132. }
  9133. static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
  9134. {
  9135. struct i802_bss *bss = priv;
  9136. struct wpa_driver_nl80211_data *drv = bss->drv;
  9137. struct nl_msg *msg;
  9138. int err = -ENOBUFS;
  9139. union wpa_event_data data;
  9140. struct survey_results *survey_results;
  9141. os_memset(&data, 0, sizeof(data));
  9142. survey_results = &data.survey_results;
  9143. dl_list_init(&survey_results->survey_list);
  9144. msg = nlmsg_alloc();
  9145. if (!msg)
  9146. goto nla_put_failure;
  9147. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
  9148. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  9149. if (freq)
  9150. data.survey_results.freq_filter = freq;
  9151. do {
  9152. wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
  9153. err = send_and_recv_msgs(drv, msg, survey_handler,
  9154. survey_results);
  9155. } while (err > 0);
  9156. if (err) {
  9157. wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
  9158. goto out_clean;
  9159. }
  9160. wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
  9161. out_clean:
  9162. clean_survey_results(survey_results);
  9163. nla_put_failure:
  9164. return err;
  9165. }
  9166. static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
  9167. const u8 *replay_ctr)
  9168. {
  9169. struct i802_bss *bss = priv;
  9170. struct wpa_driver_nl80211_data *drv = bss->drv;
  9171. struct nlattr *replay_nested;
  9172. struct nl_msg *msg;
  9173. msg = nlmsg_alloc();
  9174. if (!msg)
  9175. return;
  9176. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
  9177. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  9178. replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
  9179. if (!replay_nested)
  9180. goto nla_put_failure;
  9181. NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
  9182. NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
  9183. NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
  9184. replay_ctr);
  9185. nla_nest_end(msg, replay_nested);
  9186. send_and_recv_msgs(drv, msg, NULL, NULL);
  9187. return;
  9188. nla_put_failure:
  9189. nlmsg_free(msg);
  9190. }
  9191. static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
  9192. const u8 *addr, int qos)
  9193. {
  9194. /* send data frame to poll STA and check whether
  9195. * this frame is ACKed */
  9196. struct {
  9197. struct ieee80211_hdr hdr;
  9198. u16 qos_ctl;
  9199. } STRUCT_PACKED nulldata;
  9200. size_t size;
  9201. /* Send data frame to poll STA and check whether this frame is ACKed */
  9202. os_memset(&nulldata, 0, sizeof(nulldata));
  9203. if (qos) {
  9204. nulldata.hdr.frame_control =
  9205. IEEE80211_FC(WLAN_FC_TYPE_DATA,
  9206. WLAN_FC_STYPE_QOS_NULL);
  9207. size = sizeof(nulldata);
  9208. } else {
  9209. nulldata.hdr.frame_control =
  9210. IEEE80211_FC(WLAN_FC_TYPE_DATA,
  9211. WLAN_FC_STYPE_NULLFUNC);
  9212. size = sizeof(struct ieee80211_hdr);
  9213. }
  9214. nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
  9215. os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
  9216. os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
  9217. os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
  9218. if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
  9219. 0, 0) < 0)
  9220. wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
  9221. "send poll frame");
  9222. }
  9223. static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
  9224. int qos)
  9225. {
  9226. struct i802_bss *bss = priv;
  9227. struct wpa_driver_nl80211_data *drv = bss->drv;
  9228. struct nl_msg *msg;
  9229. if (!drv->poll_command_supported) {
  9230. nl80211_send_null_frame(bss, own_addr, addr, qos);
  9231. return;
  9232. }
  9233. msg = nlmsg_alloc();
  9234. if (!msg)
  9235. return;
  9236. nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
  9237. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  9238. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  9239. send_and_recv_msgs(drv, msg, NULL, NULL);
  9240. return;
  9241. nla_put_failure:
  9242. nlmsg_free(msg);
  9243. }
  9244. static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
  9245. {
  9246. struct nl_msg *msg;
  9247. msg = nlmsg_alloc();
  9248. if (!msg)
  9249. return -ENOMEM;
  9250. nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
  9251. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  9252. NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
  9253. enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
  9254. return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  9255. nla_put_failure:
  9256. nlmsg_free(msg);
  9257. return -ENOBUFS;
  9258. }
  9259. static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
  9260. int ctwindow)
  9261. {
  9262. struct i802_bss *bss = priv;
  9263. wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
  9264. "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
  9265. if (opp_ps != -1 || ctwindow != -1) {
  9266. #ifdef ANDROID_P2P
  9267. wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
  9268. #else /* ANDROID_P2P */
  9269. return -1; /* Not yet supported */
  9270. #endif /* ANDROID_P2P */
  9271. }
  9272. if (legacy_ps == -1)
  9273. return 0;
  9274. if (legacy_ps != 0 && legacy_ps != 1)
  9275. return -1; /* Not yet supported */
  9276. return nl80211_set_power_save(bss, legacy_ps);
  9277. }
  9278. static int nl80211_start_radar_detection(void *priv,
  9279. struct hostapd_freq_params *freq)
  9280. {
  9281. struct i802_bss *bss = priv;
  9282. struct wpa_driver_nl80211_data *drv = bss->drv;
  9283. struct nl_msg *msg;
  9284. int ret;
  9285. 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)",
  9286. freq->freq, freq->ht_enabled, freq->vht_enabled,
  9287. freq->bandwidth, freq->center_freq1, freq->center_freq2);
  9288. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
  9289. wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
  9290. "detection");
  9291. return -1;
  9292. }
  9293. msg = nlmsg_alloc();
  9294. if (!msg)
  9295. return -1;
  9296. nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
  9297. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  9298. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
  9299. if (freq->vht_enabled) {
  9300. switch (freq->bandwidth) {
  9301. case 20:
  9302. NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
  9303. NL80211_CHAN_WIDTH_20);
  9304. break;
  9305. case 40:
  9306. NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
  9307. NL80211_CHAN_WIDTH_40);
  9308. break;
  9309. case 80:
  9310. if (freq->center_freq2)
  9311. NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
  9312. NL80211_CHAN_WIDTH_80P80);
  9313. else
  9314. NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
  9315. NL80211_CHAN_WIDTH_80);
  9316. break;
  9317. case 160:
  9318. NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
  9319. NL80211_CHAN_WIDTH_160);
  9320. break;
  9321. default:
  9322. return -1;
  9323. }
  9324. NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
  9325. if (freq->center_freq2)
  9326. NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
  9327. freq->center_freq2);
  9328. } else if (freq->ht_enabled) {
  9329. switch (freq->sec_channel_offset) {
  9330. case -1:
  9331. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  9332. NL80211_CHAN_HT40MINUS);
  9333. break;
  9334. case 1:
  9335. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  9336. NL80211_CHAN_HT40PLUS);
  9337. break;
  9338. default:
  9339. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  9340. NL80211_CHAN_HT20);
  9341. break;
  9342. }
  9343. }
  9344. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  9345. if (ret == 0)
  9346. return 0;
  9347. wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
  9348. "%d (%s)", ret, strerror(-ret));
  9349. nla_put_failure:
  9350. return -1;
  9351. }
  9352. #ifdef CONFIG_TDLS
  9353. static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
  9354. u8 dialog_token, u16 status_code,
  9355. const u8 *buf, size_t len)
  9356. {
  9357. struct i802_bss *bss = priv;
  9358. struct wpa_driver_nl80211_data *drv = bss->drv;
  9359. struct nl_msg *msg;
  9360. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
  9361. return -EOPNOTSUPP;
  9362. if (!dst)
  9363. return -EINVAL;
  9364. msg = nlmsg_alloc();
  9365. if (!msg)
  9366. return -ENOMEM;
  9367. nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
  9368. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  9369. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
  9370. NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
  9371. NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
  9372. NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
  9373. NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
  9374. return send_and_recv_msgs(drv, msg, NULL, NULL);
  9375. nla_put_failure:
  9376. nlmsg_free(msg);
  9377. return -ENOBUFS;
  9378. }
  9379. static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
  9380. {
  9381. struct i802_bss *bss = priv;
  9382. struct wpa_driver_nl80211_data *drv = bss->drv;
  9383. struct nl_msg *msg;
  9384. enum nl80211_tdls_operation nl80211_oper;
  9385. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
  9386. return -EOPNOTSUPP;
  9387. switch (oper) {
  9388. case TDLS_DISCOVERY_REQ:
  9389. nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
  9390. break;
  9391. case TDLS_SETUP:
  9392. nl80211_oper = NL80211_TDLS_SETUP;
  9393. break;
  9394. case TDLS_TEARDOWN:
  9395. nl80211_oper = NL80211_TDLS_TEARDOWN;
  9396. break;
  9397. case TDLS_ENABLE_LINK:
  9398. nl80211_oper = NL80211_TDLS_ENABLE_LINK;
  9399. break;
  9400. case TDLS_DISABLE_LINK:
  9401. nl80211_oper = NL80211_TDLS_DISABLE_LINK;
  9402. break;
  9403. case TDLS_ENABLE:
  9404. return 0;
  9405. case TDLS_DISABLE:
  9406. return 0;
  9407. default:
  9408. return -EINVAL;
  9409. }
  9410. msg = nlmsg_alloc();
  9411. if (!msg)
  9412. return -ENOMEM;
  9413. nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
  9414. NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
  9415. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  9416. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
  9417. return send_and_recv_msgs(drv, msg, NULL, NULL);
  9418. nla_put_failure:
  9419. nlmsg_free(msg);
  9420. return -ENOBUFS;
  9421. }
  9422. #endif /* CONFIG TDLS */
  9423. #ifdef ANDROID
  9424. typedef struct android_wifi_priv_cmd {
  9425. char *buf;
  9426. int used_len;
  9427. int total_len;
  9428. } android_wifi_priv_cmd;
  9429. static int drv_errors = 0;
  9430. static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
  9431. {
  9432. drv_errors++;
  9433. if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
  9434. drv_errors = 0;
  9435. wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
  9436. }
  9437. }
  9438. static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
  9439. {
  9440. struct wpa_driver_nl80211_data *drv = bss->drv;
  9441. struct ifreq ifr;
  9442. android_wifi_priv_cmd priv_cmd;
  9443. char buf[MAX_DRV_CMD_SIZE];
  9444. int ret;
  9445. os_memset(&ifr, 0, sizeof(ifr));
  9446. os_memset(&priv_cmd, 0, sizeof(priv_cmd));
  9447. os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
  9448. os_memset(buf, 0, sizeof(buf));
  9449. os_strlcpy(buf, cmd, sizeof(buf));
  9450. priv_cmd.buf = buf;
  9451. priv_cmd.used_len = sizeof(buf);
  9452. priv_cmd.total_len = sizeof(buf);
  9453. ifr.ifr_data = &priv_cmd;
  9454. ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
  9455. if (ret < 0) {
  9456. wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
  9457. __func__);
  9458. wpa_driver_send_hang_msg(drv);
  9459. return ret;
  9460. }
  9461. drv_errors = 0;
  9462. return 0;
  9463. }
  9464. static int android_pno_start(struct i802_bss *bss,
  9465. struct wpa_driver_scan_params *params)
  9466. {
  9467. struct wpa_driver_nl80211_data *drv = bss->drv;
  9468. struct ifreq ifr;
  9469. android_wifi_priv_cmd priv_cmd;
  9470. int ret = 0, i = 0, bp;
  9471. char buf[WEXT_PNO_MAX_COMMAND_SIZE];
  9472. bp = WEXT_PNOSETUP_HEADER_SIZE;
  9473. os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
  9474. buf[bp++] = WEXT_PNO_TLV_PREFIX;
  9475. buf[bp++] = WEXT_PNO_TLV_VERSION;
  9476. buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
  9477. buf[bp++] = WEXT_PNO_TLV_RESERVED;
  9478. while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
  9479. /* Check that there is enough space needed for 1 more SSID, the
  9480. * other sections and null termination */
  9481. if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
  9482. WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
  9483. break;
  9484. wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
  9485. params->ssids[i].ssid,
  9486. params->ssids[i].ssid_len);
  9487. buf[bp++] = WEXT_PNO_SSID_SECTION;
  9488. buf[bp++] = params->ssids[i].ssid_len;
  9489. os_memcpy(&buf[bp], params->ssids[i].ssid,
  9490. params->ssids[i].ssid_len);
  9491. bp += params->ssids[i].ssid_len;
  9492. i++;
  9493. }
  9494. buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
  9495. os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
  9496. WEXT_PNO_SCAN_INTERVAL);
  9497. bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
  9498. buf[bp++] = WEXT_PNO_REPEAT_SECTION;
  9499. os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
  9500. WEXT_PNO_REPEAT);
  9501. bp += WEXT_PNO_REPEAT_LENGTH;
  9502. buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
  9503. os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
  9504. WEXT_PNO_MAX_REPEAT);
  9505. bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
  9506. memset(&ifr, 0, sizeof(ifr));
  9507. memset(&priv_cmd, 0, sizeof(priv_cmd));
  9508. os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
  9509. priv_cmd.buf = buf;
  9510. priv_cmd.used_len = bp;
  9511. priv_cmd.total_len = bp;
  9512. ifr.ifr_data = &priv_cmd;
  9513. ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
  9514. if (ret < 0) {
  9515. wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
  9516. ret);
  9517. wpa_driver_send_hang_msg(drv);
  9518. return ret;
  9519. }
  9520. drv_errors = 0;
  9521. return android_priv_cmd(bss, "PNOFORCE 1");
  9522. }
  9523. static int android_pno_stop(struct i802_bss *bss)
  9524. {
  9525. return android_priv_cmd(bss, "PNOFORCE 0");
  9526. }
  9527. #endif /* ANDROID */
  9528. static int driver_nl80211_set_key(const char *ifname, void *priv,
  9529. enum wpa_alg alg, const u8 *addr,
  9530. int key_idx, int set_tx,
  9531. const u8 *seq, size_t seq_len,
  9532. const u8 *key, size_t key_len)
  9533. {
  9534. struct i802_bss *bss = priv;
  9535. return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
  9536. set_tx, seq, seq_len, key, key_len);
  9537. }
  9538. static int driver_nl80211_scan2(void *priv,
  9539. struct wpa_driver_scan_params *params)
  9540. {
  9541. struct i802_bss *bss = priv;
  9542. return wpa_driver_nl80211_scan(bss, params);
  9543. }
  9544. static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
  9545. int reason_code)
  9546. {
  9547. struct i802_bss *bss = priv;
  9548. return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
  9549. }
  9550. static int driver_nl80211_authenticate(void *priv,
  9551. struct wpa_driver_auth_params *params)
  9552. {
  9553. struct i802_bss *bss = priv;
  9554. return wpa_driver_nl80211_authenticate(bss, params);
  9555. }
  9556. static void driver_nl80211_deinit(void *priv)
  9557. {
  9558. struct i802_bss *bss = priv;
  9559. wpa_driver_nl80211_deinit(bss);
  9560. }
  9561. static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
  9562. const char *ifname)
  9563. {
  9564. struct i802_bss *bss = priv;
  9565. return wpa_driver_nl80211_if_remove(bss, type, ifname);
  9566. }
  9567. static int driver_nl80211_send_mlme(void *priv, const u8 *data,
  9568. size_t data_len, int noack)
  9569. {
  9570. struct i802_bss *bss = priv;
  9571. return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
  9572. 0, 0, 0, 0);
  9573. }
  9574. static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
  9575. {
  9576. struct i802_bss *bss = priv;
  9577. return wpa_driver_nl80211_sta_remove(bss, addr);
  9578. }
  9579. static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
  9580. const char *ifname, int vlan_id)
  9581. {
  9582. struct i802_bss *bss = priv;
  9583. return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
  9584. }
  9585. static int driver_nl80211_read_sta_data(void *priv,
  9586. struct hostap_sta_driver_data *data,
  9587. const u8 *addr)
  9588. {
  9589. struct i802_bss *bss = priv;
  9590. return i802_read_sta_data(bss, data, addr);
  9591. }
  9592. static int driver_nl80211_send_action(void *priv, unsigned int freq,
  9593. unsigned int wait_time,
  9594. const u8 *dst, const u8 *src,
  9595. const u8 *bssid,
  9596. const u8 *data, size_t data_len,
  9597. int no_cck)
  9598. {
  9599. struct i802_bss *bss = priv;
  9600. return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
  9601. bssid, data, data_len, no_cck);
  9602. }
  9603. static int driver_nl80211_probe_req_report(void *priv, int report)
  9604. {
  9605. struct i802_bss *bss = priv;
  9606. return wpa_driver_nl80211_probe_req_report(bss, report);
  9607. }
  9608. static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
  9609. const u8 *ies, size_t ies_len)
  9610. {
  9611. int ret;
  9612. struct nl_msg *msg;
  9613. struct i802_bss *bss = priv;
  9614. struct wpa_driver_nl80211_data *drv = bss->drv;
  9615. u16 mdid = WPA_GET_LE16(md);
  9616. msg = nlmsg_alloc();
  9617. if (!msg)
  9618. return -ENOMEM;
  9619. wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
  9620. nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
  9621. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  9622. NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
  9623. NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
  9624. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  9625. if (ret) {
  9626. wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
  9627. "err=%d (%s)", ret, strerror(-ret));
  9628. }
  9629. return ret;
  9630. nla_put_failure:
  9631. nlmsg_free(msg);
  9632. return -ENOBUFS;
  9633. }
  9634. const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
  9635. {
  9636. struct i802_bss *bss = priv;
  9637. struct wpa_driver_nl80211_data *drv = bss->drv;
  9638. if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
  9639. return NULL;
  9640. return bss->addr;
  9641. }
  9642. static const char * scan_state_str(enum scan_states scan_state)
  9643. {
  9644. switch (scan_state) {
  9645. case NO_SCAN:
  9646. return "NO_SCAN";
  9647. case SCAN_REQUESTED:
  9648. return "SCAN_REQUESTED";
  9649. case SCAN_STARTED:
  9650. return "SCAN_STARTED";
  9651. case SCAN_COMPLETED:
  9652. return "SCAN_COMPLETED";
  9653. case SCAN_ABORTED:
  9654. return "SCAN_ABORTED";
  9655. case SCHED_SCAN_STARTED:
  9656. return "SCHED_SCAN_STARTED";
  9657. case SCHED_SCAN_STOPPED:
  9658. return "SCHED_SCAN_STOPPED";
  9659. case SCHED_SCAN_RESULTS:
  9660. return "SCHED_SCAN_RESULTS";
  9661. }
  9662. return "??";
  9663. }
  9664. static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
  9665. {
  9666. struct i802_bss *bss = priv;
  9667. struct wpa_driver_nl80211_data *drv = bss->drv;
  9668. int res;
  9669. char *pos, *end;
  9670. pos = buf;
  9671. end = buf + buflen;
  9672. res = os_snprintf(pos, end - pos,
  9673. "ifindex=%d\n"
  9674. "ifname=%s\n"
  9675. "brname=%s\n"
  9676. "addr=" MACSTR "\n"
  9677. "freq=%d\n"
  9678. "%s%s%s%s%s",
  9679. bss->ifindex,
  9680. bss->ifname,
  9681. bss->brname,
  9682. MAC2STR(bss->addr),
  9683. bss->freq,
  9684. bss->beacon_set ? "beacon_set=1\n" : "",
  9685. bss->added_if_into_bridge ?
  9686. "added_if_into_bridge=1\n" : "",
  9687. bss->added_bridge ? "added_bridge=1\n" : "",
  9688. bss->in_deinit ? "in_deinit=1\n" : "",
  9689. bss->if_dynamic ? "if_dynamic=1\n" : "");
  9690. if (res < 0 || res >= end - pos)
  9691. return pos - buf;
  9692. pos += res;
  9693. if (bss->wdev_id_set) {
  9694. res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
  9695. (unsigned long long) bss->wdev_id);
  9696. if (res < 0 || res >= end - pos)
  9697. return pos - buf;
  9698. pos += res;
  9699. }
  9700. res = os_snprintf(pos, end - pos,
  9701. "phyname=%s\n"
  9702. "drv_ifindex=%d\n"
  9703. "operstate=%d\n"
  9704. "scan_state=%s\n"
  9705. "auth_bssid=" MACSTR "\n"
  9706. "auth_attempt_bssid=" MACSTR "\n"
  9707. "bssid=" MACSTR "\n"
  9708. "prev_bssid=" MACSTR "\n"
  9709. "associated=%d\n"
  9710. "assoc_freq=%u\n"
  9711. "monitor_sock=%d\n"
  9712. "monitor_ifidx=%d\n"
  9713. "monitor_refcount=%d\n"
  9714. "last_mgmt_freq=%u\n"
  9715. "eapol_tx_sock=%d\n"
  9716. "%s%s%s%s%s%s%s%s%s%s%s%s%s",
  9717. drv->phyname,
  9718. drv->ifindex,
  9719. drv->operstate,
  9720. scan_state_str(drv->scan_state),
  9721. MAC2STR(drv->auth_bssid),
  9722. MAC2STR(drv->auth_attempt_bssid),
  9723. MAC2STR(drv->bssid),
  9724. MAC2STR(drv->prev_bssid),
  9725. drv->associated,
  9726. drv->assoc_freq,
  9727. drv->monitor_sock,
  9728. drv->monitor_ifidx,
  9729. drv->monitor_refcount,
  9730. drv->last_mgmt_freq,
  9731. drv->eapol_tx_sock,
  9732. drv->ignore_if_down_event ?
  9733. "ignore_if_down_event=1\n" : "",
  9734. drv->scan_complete_events ?
  9735. "scan_complete_events=1\n" : "",
  9736. drv->disabled_11b_rates ?
  9737. "disabled_11b_rates=1\n" : "",
  9738. drv->pending_remain_on_chan ?
  9739. "pending_remain_on_chan=1\n" : "",
  9740. drv->in_interface_list ? "in_interface_list=1\n" : "",
  9741. drv->device_ap_sme ? "device_ap_sme=1\n" : "",
  9742. drv->poll_command_supported ?
  9743. "poll_command_supported=1\n" : "",
  9744. drv->data_tx_status ? "data_tx_status=1\n" : "",
  9745. drv->scan_for_auth ? "scan_for_auth=1\n" : "",
  9746. drv->retry_auth ? "retry_auth=1\n" : "",
  9747. drv->use_monitor ? "use_monitor=1\n" : "",
  9748. drv->ignore_next_local_disconnect ?
  9749. "ignore_next_local_disconnect=1\n" : "",
  9750. drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
  9751. if (res < 0 || res >= end - pos)
  9752. return pos - buf;
  9753. pos += res;
  9754. if (drv->has_capability) {
  9755. res = os_snprintf(pos, end - pos,
  9756. "capa.key_mgmt=0x%x\n"
  9757. "capa.enc=0x%x\n"
  9758. "capa.auth=0x%x\n"
  9759. "capa.flags=0x%x\n"
  9760. "capa.max_scan_ssids=%d\n"
  9761. "capa.max_sched_scan_ssids=%d\n"
  9762. "capa.sched_scan_supported=%d\n"
  9763. "capa.max_match_sets=%d\n"
  9764. "capa.max_remain_on_chan=%u\n"
  9765. "capa.max_stations=%u\n"
  9766. "capa.probe_resp_offloads=0x%x\n"
  9767. "capa.max_acl_mac_addrs=%u\n"
  9768. "capa.num_multichan_concurrent=%u\n",
  9769. drv->capa.key_mgmt,
  9770. drv->capa.enc,
  9771. drv->capa.auth,
  9772. drv->capa.flags,
  9773. drv->capa.max_scan_ssids,
  9774. drv->capa.max_sched_scan_ssids,
  9775. drv->capa.sched_scan_supported,
  9776. drv->capa.max_match_sets,
  9777. drv->capa.max_remain_on_chan,
  9778. drv->capa.max_stations,
  9779. drv->capa.probe_resp_offloads,
  9780. drv->capa.max_acl_mac_addrs,
  9781. drv->capa.num_multichan_concurrent);
  9782. if (res < 0 || res >= end - pos)
  9783. return pos - buf;
  9784. pos += res;
  9785. }
  9786. return pos - buf;
  9787. }
  9788. static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
  9789. {
  9790. if (settings->head)
  9791. NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD,
  9792. settings->head_len, settings->head);
  9793. if (settings->tail)
  9794. NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL,
  9795. settings->tail_len, settings->tail);
  9796. if (settings->beacon_ies)
  9797. NLA_PUT(msg, NL80211_ATTR_IE,
  9798. settings->beacon_ies_len, settings->beacon_ies);
  9799. if (settings->proberesp_ies)
  9800. NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
  9801. settings->proberesp_ies_len, settings->proberesp_ies);
  9802. if (settings->assocresp_ies)
  9803. NLA_PUT(msg,
  9804. NL80211_ATTR_IE_ASSOC_RESP,
  9805. settings->assocresp_ies_len, settings->assocresp_ies);
  9806. if (settings->probe_resp)
  9807. NLA_PUT(msg, NL80211_ATTR_PROBE_RESP,
  9808. settings->probe_resp_len, settings->probe_resp);
  9809. return 0;
  9810. nla_put_failure:
  9811. return -ENOBUFS;
  9812. }
  9813. static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
  9814. {
  9815. struct nl_msg *msg;
  9816. struct i802_bss *bss = priv;
  9817. struct wpa_driver_nl80211_data *drv = bss->drv;
  9818. struct nlattr *beacon_csa;
  9819. int ret = -ENOBUFS;
  9820. wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
  9821. settings->cs_count, settings->block_tx,
  9822. settings->freq_params.freq, settings->freq_params.bandwidth,
  9823. settings->freq_params.center_freq1,
  9824. settings->freq_params.center_freq2);
  9825. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
  9826. wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
  9827. return -EOPNOTSUPP;
  9828. }
  9829. if ((drv->nlmode != NL80211_IFTYPE_AP) &&
  9830. (drv->nlmode != NL80211_IFTYPE_P2P_GO))
  9831. return -EOPNOTSUPP;
  9832. /* check settings validity */
  9833. if (!settings->beacon_csa.tail ||
  9834. ((settings->beacon_csa.tail_len <=
  9835. settings->counter_offset_beacon) ||
  9836. (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
  9837. settings->cs_count)))
  9838. return -EINVAL;
  9839. if (settings->beacon_csa.probe_resp &&
  9840. ((settings->beacon_csa.probe_resp_len <=
  9841. settings->counter_offset_presp) ||
  9842. (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
  9843. settings->cs_count)))
  9844. return -EINVAL;
  9845. msg = nlmsg_alloc();
  9846. if (!msg)
  9847. return -ENOMEM;
  9848. nl80211_cmd(drv, msg, 0, NL80211_CMD_CHANNEL_SWITCH);
  9849. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  9850. NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, settings->cs_count);
  9851. ret = nl80211_put_freq_params(msg, &settings->freq_params);
  9852. if (ret)
  9853. goto error;
  9854. if (settings->block_tx)
  9855. NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
  9856. /* beacon_after params */
  9857. ret = set_beacon_data(msg, &settings->beacon_after);
  9858. if (ret)
  9859. goto error;
  9860. /* beacon_csa params */
  9861. beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
  9862. if (!beacon_csa)
  9863. goto nla_put_failure;
  9864. ret = set_beacon_data(msg, &settings->beacon_csa);
  9865. if (ret)
  9866. goto error;
  9867. NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
  9868. settings->counter_offset_beacon);
  9869. if (settings->beacon_csa.probe_resp)
  9870. NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
  9871. settings->counter_offset_presp);
  9872. nla_nest_end(msg, beacon_csa);
  9873. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  9874. if (ret) {
  9875. wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
  9876. ret, strerror(-ret));
  9877. }
  9878. return ret;
  9879. nla_put_failure:
  9880. ret = -ENOBUFS;
  9881. error:
  9882. nlmsg_free(msg);
  9883. wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
  9884. return ret;
  9885. }
  9886. #ifdef CONFIG_TESTING_OPTIONS
  9887. static int cmd_reply_handler(struct nl_msg *msg, void *arg)
  9888. {
  9889. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  9890. struct wpabuf *buf = arg;
  9891. if (!buf)
  9892. return NL_SKIP;
  9893. if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
  9894. wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
  9895. return NL_SKIP;
  9896. }
  9897. wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
  9898. genlmsg_attrlen(gnlh, 0));
  9899. return NL_SKIP;
  9900. }
  9901. #endif /* CONFIG_TESTING_OPTIONS */
  9902. static int vendor_reply_handler(struct nl_msg *msg, void *arg)
  9903. {
  9904. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  9905. struct nlattr *nl_vendor_reply, *nl;
  9906. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  9907. struct wpabuf *buf = arg;
  9908. int rem;
  9909. if (!buf)
  9910. return NL_SKIP;
  9911. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  9912. genlmsg_attrlen(gnlh, 0), NULL);
  9913. nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
  9914. if (!nl_vendor_reply)
  9915. return NL_SKIP;
  9916. if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
  9917. wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
  9918. return NL_SKIP;
  9919. }
  9920. nla_for_each_nested(nl, nl_vendor_reply, rem) {
  9921. wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
  9922. }
  9923. return NL_SKIP;
  9924. }
  9925. static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
  9926. unsigned int subcmd, const u8 *data,
  9927. size_t data_len, struct wpabuf *buf)
  9928. {
  9929. struct i802_bss *bss = priv;
  9930. struct wpa_driver_nl80211_data *drv = bss->drv;
  9931. struct nl_msg *msg;
  9932. int ret;
  9933. msg = nlmsg_alloc();
  9934. if (!msg)
  9935. return -ENOMEM;
  9936. #ifdef CONFIG_TESTING_OPTIONS
  9937. if (vendor_id == 0xffffffff) {
  9938. nl80211_cmd(drv, msg, 0, subcmd);
  9939. if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
  9940. 0)
  9941. goto nla_put_failure;
  9942. ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
  9943. if (ret)
  9944. wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
  9945. ret);
  9946. return ret;
  9947. }
  9948. #endif /* CONFIG_TESTING_OPTIONS */
  9949. nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
  9950. if (nl80211_set_iface_id(msg, bss) < 0)
  9951. goto nla_put_failure;
  9952. NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, vendor_id);
  9953. NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd);
  9954. if (data)
  9955. NLA_PUT(msg, NL80211_ATTR_VENDOR_DATA, data_len, data);
  9956. ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
  9957. if (ret)
  9958. wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
  9959. ret);
  9960. return ret;
  9961. nla_put_failure:
  9962. nlmsg_free(msg);
  9963. return -ENOBUFS;
  9964. }
  9965. static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
  9966. u8 qos_map_set_len)
  9967. {
  9968. struct i802_bss *bss = priv;
  9969. struct wpa_driver_nl80211_data *drv = bss->drv;
  9970. struct nl_msg *msg;
  9971. int ret;
  9972. msg = nlmsg_alloc();
  9973. if (!msg)
  9974. return -ENOMEM;
  9975. wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
  9976. qos_map_set, qos_map_set_len);
  9977. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_QOS_MAP);
  9978. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  9979. NLA_PUT(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set);
  9980. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  9981. if (ret)
  9982. wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
  9983. return ret;
  9984. nla_put_failure:
  9985. nlmsg_free(msg);
  9986. return -ENOBUFS;
  9987. }
  9988. const struct wpa_driver_ops wpa_driver_nl80211_ops = {
  9989. .name = "nl80211",
  9990. .desc = "Linux nl80211/cfg80211",
  9991. .get_bssid = wpa_driver_nl80211_get_bssid,
  9992. .get_ssid = wpa_driver_nl80211_get_ssid,
  9993. .set_key = driver_nl80211_set_key,
  9994. .scan2 = driver_nl80211_scan2,
  9995. .sched_scan = wpa_driver_nl80211_sched_scan,
  9996. .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
  9997. .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
  9998. .deauthenticate = driver_nl80211_deauthenticate,
  9999. .authenticate = driver_nl80211_authenticate,
  10000. .associate = wpa_driver_nl80211_associate,
  10001. .global_init = nl80211_global_init,
  10002. .global_deinit = nl80211_global_deinit,
  10003. .init2 = wpa_driver_nl80211_init,
  10004. .deinit = driver_nl80211_deinit,
  10005. .get_capa = wpa_driver_nl80211_get_capa,
  10006. .set_operstate = wpa_driver_nl80211_set_operstate,
  10007. .set_supp_port = wpa_driver_nl80211_set_supp_port,
  10008. .set_country = wpa_driver_nl80211_set_country,
  10009. .get_country = wpa_driver_nl80211_get_country,
  10010. .set_ap = wpa_driver_nl80211_set_ap,
  10011. .set_acl = wpa_driver_nl80211_set_acl,
  10012. .if_add = wpa_driver_nl80211_if_add,
  10013. .if_remove = driver_nl80211_if_remove,
  10014. .send_mlme = driver_nl80211_send_mlme,
  10015. .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
  10016. .sta_add = wpa_driver_nl80211_sta_add,
  10017. .sta_remove = driver_nl80211_sta_remove,
  10018. .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
  10019. .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
  10020. .hapd_init = i802_init,
  10021. .hapd_deinit = i802_deinit,
  10022. .set_wds_sta = i802_set_wds_sta,
  10023. .get_seqnum = i802_get_seqnum,
  10024. .flush = i802_flush,
  10025. .get_inact_sec = i802_get_inact_sec,
  10026. .sta_clear_stats = i802_sta_clear_stats,
  10027. .set_rts = i802_set_rts,
  10028. .set_frag = i802_set_frag,
  10029. .set_tx_queue_params = i802_set_tx_queue_params,
  10030. .set_sta_vlan = driver_nl80211_set_sta_vlan,
  10031. .sta_deauth = i802_sta_deauth,
  10032. .sta_disassoc = i802_sta_disassoc,
  10033. .read_sta_data = driver_nl80211_read_sta_data,
  10034. .set_freq = i802_set_freq,
  10035. .send_action = driver_nl80211_send_action,
  10036. .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
  10037. .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
  10038. .cancel_remain_on_channel =
  10039. wpa_driver_nl80211_cancel_remain_on_channel,
  10040. .probe_req_report = driver_nl80211_probe_req_report,
  10041. .deinit_ap = wpa_driver_nl80211_deinit_ap,
  10042. .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
  10043. .resume = wpa_driver_nl80211_resume,
  10044. .send_ft_action = nl80211_send_ft_action,
  10045. .signal_monitor = nl80211_signal_monitor,
  10046. .signal_poll = nl80211_signal_poll,
  10047. .send_frame = nl80211_send_frame,
  10048. .shared_freq = wpa_driver_nl80211_shared_freq,
  10049. .set_param = nl80211_set_param,
  10050. .get_radio_name = nl80211_get_radio_name,
  10051. .add_pmkid = nl80211_add_pmkid,
  10052. .remove_pmkid = nl80211_remove_pmkid,
  10053. .flush_pmkid = nl80211_flush_pmkid,
  10054. .set_rekey_info = nl80211_set_rekey_info,
  10055. .poll_client = nl80211_poll_client,
  10056. .set_p2p_powersave = nl80211_set_p2p_powersave,
  10057. .start_dfs_cac = nl80211_start_radar_detection,
  10058. .stop_ap = wpa_driver_nl80211_stop_ap,
  10059. #ifdef CONFIG_TDLS
  10060. .send_tdls_mgmt = nl80211_send_tdls_mgmt,
  10061. .tdls_oper = nl80211_tdls_oper,
  10062. #endif /* CONFIG_TDLS */
  10063. .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
  10064. .get_mac_addr = wpa_driver_nl80211_get_macaddr,
  10065. .get_survey = wpa_driver_nl80211_get_survey,
  10066. .status = wpa_driver_nl80211_status,
  10067. .switch_channel = nl80211_switch_channel,
  10068. #ifdef ANDROID_P2P
  10069. .set_noa = wpa_driver_set_p2p_noa,
  10070. .get_noa = wpa_driver_get_p2p_noa,
  10071. .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
  10072. #endif /* ANDROID_P2P */
  10073. #ifdef ANDROID
  10074. .driver_cmd = wpa_driver_nl80211_driver_cmd,
  10075. #endif /* ANDROID */
  10076. .vendor_cmd = nl80211_vendor_cmd,
  10077. .set_qos_map = nl80211_set_qos_map,
  10078. };