driver_nl80211.c 302 KB

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