driver_nl80211.c 236 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206
  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. /* system/core/libnl_2 in AOSP does not include nla_put_u32() */
  57. int nla_put_u32(struct nl_msg *msg, int attrtype, uint32_t value)
  58. {
  59. return nla_put(msg, attrtype, sizeof(uint32_t), &value);
  60. }
  61. #endif /* ANDROID */
  62. #ifdef CONFIG_LIBNL20
  63. /* libnl 2.0 compatibility code */
  64. #define nl_handle nl_sock
  65. #define nl80211_handle_alloc nl_socket_alloc_cb
  66. #define nl80211_handle_destroy nl_socket_free
  67. #else
  68. /*
  69. * libnl 1.1 has a bug, it tries to allocate socket numbers densely
  70. * but when you free a socket again it will mess up its bitmap and
  71. * and use the wrong number the next time it needs a socket ID.
  72. * Therefore, we wrap the handle alloc/destroy and add our own pid
  73. * accounting.
  74. */
  75. static uint32_t port_bitmap[32] = { 0 };
  76. static struct nl_handle *nl80211_handle_alloc(void *cb)
  77. {
  78. struct nl_handle *handle;
  79. uint32_t pid = getpid() & 0x3FFFFF;
  80. int i;
  81. handle = nl_handle_alloc_cb(cb);
  82. for (i = 0; i < 1024; i++) {
  83. if (port_bitmap[i / 32] & (1 << (i % 32)))
  84. continue;
  85. port_bitmap[i / 32] |= 1 << (i % 32);
  86. pid += i << 22;
  87. break;
  88. }
  89. nl_socket_set_local_port(handle, pid);
  90. return handle;
  91. }
  92. static void nl80211_handle_destroy(struct nl_handle *handle)
  93. {
  94. uint32_t port = nl_socket_get_local_port(handle);
  95. port >>= 22;
  96. port_bitmap[port / 32] &= ~(1 << (port % 32));
  97. nl_handle_destroy(handle);
  98. }
  99. #endif /* CONFIG_LIBNL20 */
  100. static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
  101. {
  102. struct nl_handle *handle;
  103. handle = nl80211_handle_alloc(cb);
  104. if (handle == NULL) {
  105. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  106. "callbacks (%s)", dbg);
  107. return NULL;
  108. }
  109. if (genl_connect(handle)) {
  110. wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
  111. "netlink (%s)", dbg);
  112. nl80211_handle_destroy(handle);
  113. return NULL;
  114. }
  115. return handle;
  116. }
  117. static void nl_destroy_handles(struct nl_handle **handle)
  118. {
  119. if (*handle == NULL)
  120. return;
  121. nl80211_handle_destroy(*handle);
  122. *handle = NULL;
  123. }
  124. #ifndef IFF_LOWER_UP
  125. #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
  126. #endif
  127. #ifndef IFF_DORMANT
  128. #define IFF_DORMANT 0x20000 /* driver signals dormant */
  129. #endif
  130. #ifndef IF_OPER_DORMANT
  131. #define IF_OPER_DORMANT 5
  132. #endif
  133. #ifndef IF_OPER_UP
  134. #define IF_OPER_UP 6
  135. #endif
  136. struct nl80211_global {
  137. struct dl_list interfaces;
  138. int if_add_ifindex;
  139. struct netlink_data *netlink;
  140. struct nl_cb *nl_cb;
  141. struct nl_handle *nl;
  142. int nl80211_id;
  143. int ioctl_sock; /* socket for ioctl() use */
  144. struct nl_handle *nl_event;
  145. };
  146. struct nl80211_wiphy_data {
  147. struct dl_list list;
  148. struct dl_list bsss;
  149. struct dl_list drvs;
  150. struct nl_handle *nl_beacons;
  151. struct nl_cb *nl_cb;
  152. int wiphy_idx;
  153. };
  154. static void nl80211_global_deinit(void *priv);
  155. static void wpa_driver_nl80211_deinit(void *priv);
  156. struct i802_bss {
  157. struct wpa_driver_nl80211_data *drv;
  158. struct i802_bss *next;
  159. int ifindex;
  160. char ifname[IFNAMSIZ + 1];
  161. char brname[IFNAMSIZ];
  162. unsigned int beacon_set:1;
  163. unsigned int added_if_into_bridge:1;
  164. unsigned int added_bridge:1;
  165. unsigned int in_deinit:1;
  166. u8 addr[ETH_ALEN];
  167. int freq;
  168. struct nl_handle *nl_preq, *nl_mgmt;
  169. struct nl_cb *nl_cb;
  170. struct nl80211_wiphy_data *wiphy_data;
  171. struct dl_list wiphy_list;
  172. };
  173. struct wpa_driver_nl80211_data {
  174. struct nl80211_global *global;
  175. struct dl_list list;
  176. struct dl_list wiphy_list;
  177. char phyname[32];
  178. void *ctx;
  179. int ifindex;
  180. int if_removed;
  181. int if_disabled;
  182. int ignore_if_down_event;
  183. struct rfkill_data *rfkill;
  184. struct wpa_driver_capa capa;
  185. int has_capability;
  186. int operstate;
  187. int scan_complete_events;
  188. struct nl_cb *nl_cb;
  189. u8 auth_bssid[ETH_ALEN];
  190. u8 bssid[ETH_ALEN];
  191. int associated;
  192. u8 ssid[32];
  193. size_t ssid_len;
  194. enum nl80211_iftype nlmode;
  195. enum nl80211_iftype ap_scan_as_station;
  196. unsigned int assoc_freq;
  197. int monitor_sock;
  198. int monitor_ifidx;
  199. int monitor_refcount;
  200. unsigned int disabled_11b_rates:1;
  201. unsigned int pending_remain_on_chan:1;
  202. unsigned int in_interface_list:1;
  203. unsigned int device_ap_sme:1;
  204. unsigned int poll_command_supported:1;
  205. unsigned int data_tx_status:1;
  206. unsigned int scan_for_auth:1;
  207. unsigned int retry_auth:1;
  208. unsigned int use_monitor:1;
  209. u64 remain_on_chan_cookie;
  210. u64 send_action_cookie;
  211. unsigned int last_mgmt_freq;
  212. struct wpa_driver_scan_filter *filter_ssids;
  213. size_t num_filter_ssids;
  214. struct i802_bss first_bss;
  215. int eapol_tx_sock;
  216. #ifdef HOSTAPD
  217. int eapol_sock; /* socket for EAPOL frames */
  218. int default_if_indices[16];
  219. int *if_indices;
  220. int num_if_indices;
  221. int last_freq;
  222. int last_freq_ht;
  223. #endif /* HOSTAPD */
  224. /* From failed authentication command */
  225. int auth_freq;
  226. u8 auth_bssid_[ETH_ALEN];
  227. u8 auth_ssid[32];
  228. size_t auth_ssid_len;
  229. int auth_alg;
  230. u8 *auth_ie;
  231. size_t auth_ie_len;
  232. u8 auth_wep_key[4][16];
  233. size_t auth_wep_key_len[4];
  234. int auth_wep_tx_keyidx;
  235. int auth_local_state_change;
  236. int auth_p2p;
  237. };
  238. static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
  239. void *timeout_ctx);
  240. static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
  241. enum nl80211_iftype nlmode);
  242. static int
  243. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
  244. static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
  245. const u8 *addr, int cmd, u16 reason_code,
  246. int local_state_change);
  247. static void nl80211_remove_monitor_interface(
  248. struct wpa_driver_nl80211_data *drv);
  249. static int nl80211_send_frame_cmd(struct i802_bss *bss,
  250. unsigned int freq, unsigned int wait,
  251. const u8 *buf, size_t buf_len, u64 *cookie,
  252. int no_cck, int no_ack, int offchanok);
  253. static int wpa_driver_nl80211_probe_req_report(void *priv, int report);
  254. #ifdef ANDROID
  255. static int android_pno_start(struct i802_bss *bss,
  256. struct wpa_driver_scan_params *params);
  257. static int android_pno_stop(struct i802_bss *bss);
  258. #endif /* ANDROID */
  259. #ifdef HOSTAPD
  260. static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  261. static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  262. static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  263. static int wpa_driver_nl80211_if_remove(void *priv,
  264. enum wpa_driver_if_type type,
  265. const char *ifname);
  266. #else /* HOSTAPD */
  267. static inline void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  268. {
  269. }
  270. static inline void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  271. {
  272. }
  273. static inline int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  274. {
  275. return 0;
  276. }
  277. #endif /* HOSTAPD */
  278. static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
  279. static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
  280. int ifindex, int disabled);
  281. static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
  282. static int wpa_driver_nl80211_authenticate_retry(
  283. struct wpa_driver_nl80211_data *drv);
  284. static int is_ap_interface(enum nl80211_iftype nlmode)
  285. {
  286. return (nlmode == NL80211_IFTYPE_AP ||
  287. nlmode == NL80211_IFTYPE_P2P_GO);
  288. }
  289. static int is_sta_interface(enum nl80211_iftype nlmode)
  290. {
  291. return (nlmode == NL80211_IFTYPE_STATION ||
  292. nlmode == NL80211_IFTYPE_P2P_CLIENT);
  293. }
  294. static int is_p2p_interface(enum nl80211_iftype nlmode)
  295. {
  296. return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
  297. nlmode == NL80211_IFTYPE_P2P_GO);
  298. }
  299. struct nl80211_bss_info_arg {
  300. struct wpa_driver_nl80211_data *drv;
  301. struct wpa_scan_results *res;
  302. unsigned int assoc_freq;
  303. u8 assoc_bssid[ETH_ALEN];
  304. };
  305. static int bss_info_handler(struct nl_msg *msg, void *arg);
  306. /* nl80211 code */
  307. static int ack_handler(struct nl_msg *msg, void *arg)
  308. {
  309. int *err = arg;
  310. *err = 0;
  311. return NL_STOP;
  312. }
  313. static int finish_handler(struct nl_msg *msg, void *arg)
  314. {
  315. int *ret = arg;
  316. *ret = 0;
  317. return NL_SKIP;
  318. }
  319. static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
  320. void *arg)
  321. {
  322. int *ret = arg;
  323. *ret = err->error;
  324. return NL_SKIP;
  325. }
  326. static int no_seq_check(struct nl_msg *msg, void *arg)
  327. {
  328. return NL_OK;
  329. }
  330. static int send_and_recv(struct nl80211_global *global,
  331. struct nl_handle *nl_handle, struct nl_msg *msg,
  332. int (*valid_handler)(struct nl_msg *, void *),
  333. void *valid_data)
  334. {
  335. struct nl_cb *cb;
  336. int err = -ENOMEM;
  337. cb = nl_cb_clone(global->nl_cb);
  338. if (!cb)
  339. goto out;
  340. err = nl_send_auto_complete(nl_handle, msg);
  341. if (err < 0)
  342. goto out;
  343. err = 1;
  344. nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
  345. nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
  346. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
  347. if (valid_handler)
  348. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
  349. valid_handler, valid_data);
  350. while (err > 0)
  351. nl_recvmsgs(nl_handle, cb);
  352. out:
  353. nl_cb_put(cb);
  354. nlmsg_free(msg);
  355. return err;
  356. }
  357. static int send_and_recv_msgs_global(struct nl80211_global *global,
  358. struct nl_msg *msg,
  359. int (*valid_handler)(struct nl_msg *, void *),
  360. void *valid_data)
  361. {
  362. return send_and_recv(global, global->nl, msg, valid_handler,
  363. valid_data);
  364. }
  365. static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
  366. struct nl_msg *msg,
  367. int (*valid_handler)(struct nl_msg *, void *),
  368. void *valid_data)
  369. {
  370. return send_and_recv(drv->global, drv->global->nl, msg,
  371. valid_handler, valid_data);
  372. }
  373. struct family_data {
  374. const char *group;
  375. int id;
  376. };
  377. static int family_handler(struct nl_msg *msg, void *arg)
  378. {
  379. struct family_data *res = arg;
  380. struct nlattr *tb[CTRL_ATTR_MAX + 1];
  381. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  382. struct nlattr *mcgrp;
  383. int i;
  384. nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  385. genlmsg_attrlen(gnlh, 0), NULL);
  386. if (!tb[CTRL_ATTR_MCAST_GROUPS])
  387. return NL_SKIP;
  388. nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
  389. struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
  390. nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
  391. nla_len(mcgrp), NULL);
  392. if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
  393. !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
  394. os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
  395. res->group,
  396. nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
  397. continue;
  398. res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
  399. break;
  400. };
  401. return NL_SKIP;
  402. }
  403. static int nl_get_multicast_id(struct nl80211_global *global,
  404. const char *family, const char *group)
  405. {
  406. struct nl_msg *msg;
  407. int ret = -1;
  408. struct family_data res = { group, -ENOENT };
  409. msg = nlmsg_alloc();
  410. if (!msg)
  411. return -ENOMEM;
  412. genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
  413. 0, 0, CTRL_CMD_GETFAMILY, 0);
  414. NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
  415. ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
  416. msg = NULL;
  417. if (ret == 0)
  418. ret = res.id;
  419. nla_put_failure:
  420. nlmsg_free(msg);
  421. return ret;
  422. }
  423. static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
  424. struct nl_msg *msg, int flags, uint8_t cmd)
  425. {
  426. return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
  427. 0, flags, cmd, 0);
  428. }
  429. struct wiphy_idx_data {
  430. int wiphy_idx;
  431. };
  432. static int netdev_info_handler(struct nl_msg *msg, void *arg)
  433. {
  434. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  435. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  436. struct wiphy_idx_data *info = arg;
  437. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  438. genlmsg_attrlen(gnlh, 0), NULL);
  439. if (tb[NL80211_ATTR_WIPHY])
  440. info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
  441. return NL_SKIP;
  442. }
  443. static int nl80211_get_wiphy_index(struct i802_bss *bss)
  444. {
  445. struct nl_msg *msg;
  446. struct wiphy_idx_data data = {
  447. .wiphy_idx = -1,
  448. };
  449. msg = nlmsg_alloc();
  450. if (!msg)
  451. return -1;
  452. nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
  453. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  454. if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
  455. return data.wiphy_idx;
  456. msg = NULL;
  457. nla_put_failure:
  458. nlmsg_free(msg);
  459. return -1;
  460. }
  461. static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
  462. struct nl80211_wiphy_data *w)
  463. {
  464. struct nl_msg *msg;
  465. int ret = -1;
  466. msg = nlmsg_alloc();
  467. if (!msg)
  468. return -1;
  469. nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
  470. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
  471. ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
  472. msg = NULL;
  473. if (ret) {
  474. wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
  475. "failed: ret=%d (%s)",
  476. ret, strerror(-ret));
  477. goto nla_put_failure;
  478. }
  479. ret = 0;
  480. nla_put_failure:
  481. nlmsg_free(msg);
  482. return ret;
  483. }
  484. static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
  485. {
  486. struct nl80211_wiphy_data *w = eloop_ctx;
  487. wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
  488. nl_recvmsgs(handle, w->nl_cb);
  489. }
  490. static int process_beacon_event(struct nl_msg *msg, void *arg)
  491. {
  492. struct nl80211_wiphy_data *w = arg;
  493. struct wpa_driver_nl80211_data *drv;
  494. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  495. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  496. union wpa_event_data event;
  497. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  498. genlmsg_attrlen(gnlh, 0), NULL);
  499. if (gnlh->cmd != NL80211_CMD_FRAME) {
  500. wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
  501. gnlh->cmd);
  502. return NL_SKIP;
  503. }
  504. if (!tb[NL80211_ATTR_FRAME])
  505. return NL_SKIP;
  506. dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
  507. wiphy_list) {
  508. os_memset(&event, 0, sizeof(event));
  509. event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
  510. event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
  511. wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
  512. }
  513. return NL_SKIP;
  514. }
  515. static struct nl80211_wiphy_data *
  516. nl80211_get_wiphy_data_ap(struct i802_bss *bss)
  517. {
  518. static DEFINE_DL_LIST(nl80211_wiphys);
  519. struct nl80211_wiphy_data *w;
  520. int wiphy_idx, found = 0;
  521. struct i802_bss *tmp_bss;
  522. if (bss->wiphy_data != NULL)
  523. return bss->wiphy_data;
  524. wiphy_idx = nl80211_get_wiphy_index(bss);
  525. dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
  526. if (w->wiphy_idx == wiphy_idx)
  527. goto add;
  528. }
  529. /* alloc new one */
  530. w = os_zalloc(sizeof(*w));
  531. if (w == NULL)
  532. return NULL;
  533. w->wiphy_idx = wiphy_idx;
  534. dl_list_init(&w->bsss);
  535. dl_list_init(&w->drvs);
  536. w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  537. if (!w->nl_cb) {
  538. os_free(w);
  539. return NULL;
  540. }
  541. nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
  542. nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
  543. w);
  544. w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
  545. "wiphy beacons");
  546. if (w->nl_beacons == NULL) {
  547. os_free(w);
  548. return NULL;
  549. }
  550. if (nl80211_register_beacons(bss->drv, w)) {
  551. nl_destroy_handles(&w->nl_beacons);
  552. os_free(w);
  553. return NULL;
  554. }
  555. eloop_register_read_sock(nl_socket_get_fd(w->nl_beacons),
  556. nl80211_recv_beacons, w, w->nl_beacons);
  557. dl_list_add(&nl80211_wiphys, &w->list);
  558. add:
  559. /* drv entry for this bss already there? */
  560. dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
  561. if (tmp_bss->drv == bss->drv) {
  562. found = 1;
  563. break;
  564. }
  565. }
  566. /* if not add it */
  567. if (!found)
  568. dl_list_add(&w->drvs, &bss->drv->wiphy_list);
  569. dl_list_add(&w->bsss, &bss->wiphy_list);
  570. bss->wiphy_data = w;
  571. return w;
  572. }
  573. static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
  574. {
  575. struct nl80211_wiphy_data *w = bss->wiphy_data;
  576. struct i802_bss *tmp_bss;
  577. int found = 0;
  578. if (w == NULL)
  579. return;
  580. bss->wiphy_data = NULL;
  581. dl_list_del(&bss->wiphy_list);
  582. /* still any for this drv present? */
  583. dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
  584. if (tmp_bss->drv == bss->drv) {
  585. found = 1;
  586. break;
  587. }
  588. }
  589. /* if not remove it */
  590. if (!found)
  591. dl_list_del(&bss->drv->wiphy_list);
  592. if (!dl_list_empty(&w->bsss))
  593. return;
  594. eloop_unregister_read_sock(nl_socket_get_fd(w->nl_beacons));
  595. nl_cb_put(w->nl_cb);
  596. nl_destroy_handles(&w->nl_beacons);
  597. dl_list_del(&w->list);
  598. os_free(w);
  599. }
  600. static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
  601. {
  602. struct i802_bss *bss = priv;
  603. struct wpa_driver_nl80211_data *drv = bss->drv;
  604. if (!drv->associated)
  605. return -1;
  606. os_memcpy(bssid, drv->bssid, ETH_ALEN);
  607. return 0;
  608. }
  609. static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
  610. {
  611. struct i802_bss *bss = priv;
  612. struct wpa_driver_nl80211_data *drv = bss->drv;
  613. if (!drv->associated)
  614. return -1;
  615. os_memcpy(ssid, drv->ssid, drv->ssid_len);
  616. return drv->ssid_len;
  617. }
  618. static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
  619. char *buf, size_t len, int del)
  620. {
  621. union wpa_event_data event;
  622. os_memset(&event, 0, sizeof(event));
  623. if (len > sizeof(event.interface_status.ifname))
  624. len = sizeof(event.interface_status.ifname) - 1;
  625. os_memcpy(event.interface_status.ifname, buf, len);
  626. event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
  627. EVENT_INTERFACE_ADDED;
  628. wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
  629. del ? "DEL" : "NEW",
  630. event.interface_status.ifname,
  631. del ? "removed" : "added");
  632. if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) {
  633. if (del) {
  634. if (drv->if_removed) {
  635. wpa_printf(MSG_DEBUG, "nl80211: if_removed "
  636. "already set - ignore event");
  637. return;
  638. }
  639. drv->if_removed = 1;
  640. } else {
  641. if (if_nametoindex(drv->first_bss.ifname) == 0) {
  642. wpa_printf(MSG_DEBUG, "nl80211: Interface %s "
  643. "does not exist - ignore "
  644. "RTM_NEWLINK",
  645. drv->first_bss.ifname);
  646. return;
  647. }
  648. if (!drv->if_removed) {
  649. wpa_printf(MSG_DEBUG, "nl80211: if_removed "
  650. "already cleared - ignore event");
  651. return;
  652. }
  653. drv->if_removed = 0;
  654. }
  655. }
  656. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
  657. }
  658. static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
  659. u8 *buf, size_t len)
  660. {
  661. int attrlen, rta_len;
  662. struct rtattr *attr;
  663. attrlen = len;
  664. attr = (struct rtattr *) buf;
  665. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  666. while (RTA_OK(attr, attrlen)) {
  667. if (attr->rta_type == IFLA_IFNAME) {
  668. if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname)
  669. == 0)
  670. return 1;
  671. else
  672. break;
  673. }
  674. attr = RTA_NEXT(attr, attrlen);
  675. }
  676. return 0;
  677. }
  678. static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
  679. int ifindex, u8 *buf, size_t len)
  680. {
  681. if (drv->ifindex == ifindex)
  682. return 1;
  683. if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
  684. drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname);
  685. wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
  686. "interface");
  687. wpa_driver_nl80211_finish_drv_init(drv);
  688. return 1;
  689. }
  690. return 0;
  691. }
  692. static struct wpa_driver_nl80211_data *
  693. nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
  694. {
  695. struct wpa_driver_nl80211_data *drv;
  696. dl_list_for_each(drv, &global->interfaces,
  697. struct wpa_driver_nl80211_data, list) {
  698. if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
  699. have_ifidx(drv, idx))
  700. return drv;
  701. }
  702. return NULL;
  703. }
  704. static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
  705. struct ifinfomsg *ifi,
  706. u8 *buf, size_t len)
  707. {
  708. struct nl80211_global *global = ctx;
  709. struct wpa_driver_nl80211_data *drv;
  710. int attrlen, rta_len;
  711. struct rtattr *attr;
  712. u32 brid = 0;
  713. char namebuf[IFNAMSIZ];
  714. drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
  715. if (!drv) {
  716. wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
  717. "ifindex %d", ifi->ifi_index);
  718. return;
  719. }
  720. wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
  721. "(%s%s%s%s)",
  722. drv->operstate, ifi->ifi_flags,
  723. (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
  724. (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
  725. (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
  726. (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
  727. if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
  728. if (if_indextoname(ifi->ifi_index, namebuf) &&
  729. linux_iface_up(drv->global->ioctl_sock,
  730. drv->first_bss.ifname) > 0) {
  731. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
  732. "event since interface %s is up", namebuf);
  733. return;
  734. }
  735. wpa_printf(MSG_DEBUG, "nl80211: Interface down");
  736. if (drv->ignore_if_down_event) {
  737. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
  738. "event generated by mode change");
  739. drv->ignore_if_down_event = 0;
  740. } else {
  741. drv->if_disabled = 1;
  742. wpa_supplicant_event(drv->ctx,
  743. EVENT_INTERFACE_DISABLED, NULL);
  744. }
  745. }
  746. if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
  747. if (if_indextoname(ifi->ifi_index, namebuf) &&
  748. linux_iface_up(drv->global->ioctl_sock,
  749. drv->first_bss.ifname) == 0) {
  750. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
  751. "event since interface %s is down",
  752. namebuf);
  753. } else if (if_nametoindex(drv->first_bss.ifname) == 0) {
  754. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
  755. "event since interface %s does not exist",
  756. drv->first_bss.ifname);
  757. } else if (drv->if_removed) {
  758. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
  759. "event since interface %s is marked "
  760. "removed", drv->first_bss.ifname);
  761. } else {
  762. wpa_printf(MSG_DEBUG, "nl80211: Interface up");
  763. drv->if_disabled = 0;
  764. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
  765. NULL);
  766. }
  767. }
  768. /*
  769. * Some drivers send the association event before the operup event--in
  770. * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
  771. * fails. This will hit us when wpa_supplicant does not need to do
  772. * IEEE 802.1X authentication
  773. */
  774. if (drv->operstate == 1 &&
  775. (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
  776. !(ifi->ifi_flags & IFF_RUNNING))
  777. netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
  778. -1, IF_OPER_UP);
  779. attrlen = len;
  780. attr = (struct rtattr *) buf;
  781. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  782. while (RTA_OK(attr, attrlen)) {
  783. if (attr->rta_type == IFLA_IFNAME) {
  784. wpa_driver_nl80211_event_link(
  785. drv,
  786. ((char *) attr) + rta_len,
  787. attr->rta_len - rta_len, 0);
  788. } else if (attr->rta_type == IFLA_MASTER)
  789. brid = nla_get_u32((struct nlattr *) attr);
  790. attr = RTA_NEXT(attr, attrlen);
  791. }
  792. if (ifi->ifi_family == AF_BRIDGE && brid) {
  793. /* device has been added to bridge */
  794. if_indextoname(brid, namebuf);
  795. wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
  796. brid, namebuf);
  797. add_ifidx(drv, brid);
  798. }
  799. }
  800. static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
  801. struct ifinfomsg *ifi,
  802. u8 *buf, size_t len)
  803. {
  804. struct nl80211_global *global = ctx;
  805. struct wpa_driver_nl80211_data *drv;
  806. int attrlen, rta_len;
  807. struct rtattr *attr;
  808. u32 brid = 0;
  809. drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
  810. if (!drv) {
  811. wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for "
  812. "foreign ifindex %d", ifi->ifi_index);
  813. return;
  814. }
  815. attrlen = len;
  816. attr = (struct rtattr *) buf;
  817. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  818. while (RTA_OK(attr, attrlen)) {
  819. if (attr->rta_type == IFLA_IFNAME) {
  820. wpa_driver_nl80211_event_link(
  821. drv,
  822. ((char *) attr) + rta_len,
  823. attr->rta_len - rta_len, 1);
  824. } else if (attr->rta_type == IFLA_MASTER)
  825. brid = nla_get_u32((struct nlattr *) attr);
  826. attr = RTA_NEXT(attr, attrlen);
  827. }
  828. if (ifi->ifi_family == AF_BRIDGE && brid) {
  829. /* device has been removed from bridge */
  830. char namebuf[IFNAMSIZ];
  831. if_indextoname(brid, namebuf);
  832. wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
  833. "%s", brid, namebuf);
  834. del_ifidx(drv, brid);
  835. }
  836. }
  837. static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
  838. const u8 *frame, size_t len)
  839. {
  840. const struct ieee80211_mgmt *mgmt;
  841. union wpa_event_data event;
  842. mgmt = (const struct ieee80211_mgmt *) frame;
  843. if (len < 24 + sizeof(mgmt->u.auth)) {
  844. wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
  845. "frame");
  846. return;
  847. }
  848. os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
  849. os_memset(&event, 0, sizeof(event));
  850. os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
  851. event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
  852. event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
  853. if (len > 24 + sizeof(mgmt->u.auth)) {
  854. event.auth.ies = mgmt->u.auth.variable;
  855. event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
  856. }
  857. wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
  858. }
  859. static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
  860. {
  861. struct nl_msg *msg;
  862. int ret;
  863. struct nl80211_bss_info_arg arg;
  864. os_memset(&arg, 0, sizeof(arg));
  865. msg = nlmsg_alloc();
  866. if (!msg)
  867. goto nla_put_failure;
  868. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
  869. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  870. arg.drv = drv;
  871. ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
  872. msg = NULL;
  873. if (ret == 0) {
  874. wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
  875. "associated BSS from scan results: %u MHz",
  876. arg.assoc_freq);
  877. return arg.assoc_freq ? arg.assoc_freq : drv->assoc_freq;
  878. }
  879. wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
  880. "(%s)", ret, strerror(-ret));
  881. nla_put_failure:
  882. nlmsg_free(msg);
  883. return drv->assoc_freq;
  884. }
  885. static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
  886. const u8 *frame, size_t len)
  887. {
  888. const struct ieee80211_mgmt *mgmt;
  889. union wpa_event_data event;
  890. u16 status;
  891. mgmt = (const struct ieee80211_mgmt *) frame;
  892. if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
  893. wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
  894. "frame");
  895. return;
  896. }
  897. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  898. if (status != WLAN_STATUS_SUCCESS) {
  899. os_memset(&event, 0, sizeof(event));
  900. event.assoc_reject.bssid = mgmt->bssid;
  901. if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
  902. event.assoc_reject.resp_ies =
  903. (u8 *) mgmt->u.assoc_resp.variable;
  904. event.assoc_reject.resp_ies_len =
  905. len - 24 - sizeof(mgmt->u.assoc_resp);
  906. }
  907. event.assoc_reject.status_code = status;
  908. wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
  909. return;
  910. }
  911. drv->associated = 1;
  912. os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
  913. os_memset(&event, 0, sizeof(event));
  914. if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
  915. event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
  916. event.assoc_info.resp_ies_len =
  917. len - 24 - sizeof(mgmt->u.assoc_resp);
  918. }
  919. event.assoc_info.freq = drv->assoc_freq;
  920. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
  921. }
  922. static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
  923. enum nl80211_commands cmd, struct nlattr *status,
  924. struct nlattr *addr, struct nlattr *req_ie,
  925. struct nlattr *resp_ie)
  926. {
  927. union wpa_event_data event;
  928. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  929. /*
  930. * Avoid reporting two association events that would confuse
  931. * the core code.
  932. */
  933. wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
  934. "when using userspace SME", cmd);
  935. return;
  936. }
  937. os_memset(&event, 0, sizeof(event));
  938. if (cmd == NL80211_CMD_CONNECT &&
  939. nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
  940. if (addr)
  941. event.assoc_reject.bssid = nla_data(addr);
  942. if (resp_ie) {
  943. event.assoc_reject.resp_ies = nla_data(resp_ie);
  944. event.assoc_reject.resp_ies_len = nla_len(resp_ie);
  945. }
  946. event.assoc_reject.status_code = nla_get_u16(status);
  947. wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
  948. return;
  949. }
  950. drv->associated = 1;
  951. if (addr)
  952. os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
  953. if (req_ie) {
  954. event.assoc_info.req_ies = nla_data(req_ie);
  955. event.assoc_info.req_ies_len = nla_len(req_ie);
  956. }
  957. if (resp_ie) {
  958. event.assoc_info.resp_ies = nla_data(resp_ie);
  959. event.assoc_info.resp_ies_len = nla_len(resp_ie);
  960. }
  961. event.assoc_info.freq = nl80211_get_assoc_freq(drv);
  962. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
  963. }
  964. static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
  965. struct nlattr *reason, struct nlattr *addr,
  966. struct nlattr *by_ap)
  967. {
  968. union wpa_event_data data;
  969. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  970. /*
  971. * Avoid reporting two disassociation events that could
  972. * confuse the core code.
  973. */
  974. wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
  975. "event when using userspace SME");
  976. return;
  977. }
  978. drv->associated = 0;
  979. os_memset(&data, 0, sizeof(data));
  980. if (reason)
  981. data.deauth_info.reason_code = nla_get_u16(reason);
  982. data.deauth_info.locally_generated = by_ap == NULL;
  983. wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
  984. }
  985. static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
  986. struct nlattr *freq, struct nlattr *type)
  987. {
  988. union wpa_event_data data;
  989. int ht_enabled = 1;
  990. int chan_offset = 0;
  991. wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
  992. if (!freq || !type)
  993. return;
  994. switch (nla_get_u32(type)) {
  995. case NL80211_CHAN_NO_HT:
  996. ht_enabled = 0;
  997. break;
  998. case NL80211_CHAN_HT20:
  999. break;
  1000. case NL80211_CHAN_HT40PLUS:
  1001. chan_offset = 1;
  1002. break;
  1003. case NL80211_CHAN_HT40MINUS:
  1004. chan_offset = -1;
  1005. break;
  1006. }
  1007. data.ch_switch.freq = nla_get_u32(freq);
  1008. data.ch_switch.ht_enabled = ht_enabled;
  1009. data.ch_switch.ch_offset = chan_offset;
  1010. wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
  1011. }
  1012. static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
  1013. enum nl80211_commands cmd, struct nlattr *addr)
  1014. {
  1015. union wpa_event_data event;
  1016. enum wpa_event_type ev;
  1017. if (nla_len(addr) != ETH_ALEN)
  1018. return;
  1019. wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
  1020. cmd, MAC2STR((u8 *) nla_data(addr)));
  1021. if (cmd == NL80211_CMD_AUTHENTICATE)
  1022. ev = EVENT_AUTH_TIMED_OUT;
  1023. else if (cmd == NL80211_CMD_ASSOCIATE)
  1024. ev = EVENT_ASSOC_TIMED_OUT;
  1025. else
  1026. return;
  1027. os_memset(&event, 0, sizeof(event));
  1028. os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
  1029. wpa_supplicant_event(drv->ctx, ev, &event);
  1030. }
  1031. static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
  1032. struct nlattr *freq, struct nlattr *sig,
  1033. const u8 *frame, size_t len)
  1034. {
  1035. const struct ieee80211_mgmt *mgmt;
  1036. union wpa_event_data event;
  1037. u16 fc, stype;
  1038. int ssi_signal = 0;
  1039. mgmt = (const struct ieee80211_mgmt *) frame;
  1040. if (len < 24) {
  1041. wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
  1042. return;
  1043. }
  1044. fc = le_to_host16(mgmt->frame_control);
  1045. stype = WLAN_FC_GET_STYPE(fc);
  1046. if (sig)
  1047. ssi_signal = (s32) nla_get_u32(sig);
  1048. os_memset(&event, 0, sizeof(event));
  1049. if (freq) {
  1050. event.rx_action.freq = nla_get_u32(freq);
  1051. drv->last_mgmt_freq = event.rx_action.freq;
  1052. }
  1053. if (stype == WLAN_FC_STYPE_ACTION) {
  1054. event.rx_action.da = mgmt->da;
  1055. event.rx_action.sa = mgmt->sa;
  1056. event.rx_action.bssid = mgmt->bssid;
  1057. event.rx_action.category = mgmt->u.action.category;
  1058. event.rx_action.data = &mgmt->u.action.category + 1;
  1059. event.rx_action.len = frame + len - event.rx_action.data;
  1060. wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
  1061. } else {
  1062. event.rx_mgmt.frame = frame;
  1063. event.rx_mgmt.frame_len = len;
  1064. event.rx_mgmt.ssi_signal = ssi_signal;
  1065. wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
  1066. }
  1067. }
  1068. static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
  1069. struct nlattr *cookie, const u8 *frame,
  1070. size_t len, struct nlattr *ack)
  1071. {
  1072. union wpa_event_data event;
  1073. const struct ieee80211_hdr *hdr;
  1074. u16 fc;
  1075. if (!is_ap_interface(drv->nlmode)) {
  1076. u64 cookie_val;
  1077. if (!cookie)
  1078. return;
  1079. cookie_val = nla_get_u64(cookie);
  1080. wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
  1081. " cookie=0%llx%s (ack=%d)",
  1082. (long long unsigned int) cookie_val,
  1083. cookie_val == drv->send_action_cookie ?
  1084. " (match)" : " (unknown)", ack != NULL);
  1085. if (cookie_val != drv->send_action_cookie)
  1086. return;
  1087. }
  1088. hdr = (const struct ieee80211_hdr *) frame;
  1089. fc = le_to_host16(hdr->frame_control);
  1090. os_memset(&event, 0, sizeof(event));
  1091. event.tx_status.type = WLAN_FC_GET_TYPE(fc);
  1092. event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
  1093. event.tx_status.dst = hdr->addr1;
  1094. event.tx_status.data = frame;
  1095. event.tx_status.data_len = len;
  1096. event.tx_status.ack = ack != NULL;
  1097. wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
  1098. }
  1099. static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
  1100. enum wpa_event_type type,
  1101. const u8 *frame, size_t len)
  1102. {
  1103. const struct ieee80211_mgmt *mgmt;
  1104. union wpa_event_data event;
  1105. const u8 *bssid = NULL;
  1106. u16 reason_code = 0;
  1107. mgmt = (const struct ieee80211_mgmt *) frame;
  1108. if (len >= 24) {
  1109. bssid = mgmt->bssid;
  1110. if (drv->associated != 0 &&
  1111. os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
  1112. os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
  1113. /*
  1114. * We have presumably received this deauth as a
  1115. * response to a clear_state_mismatch() outgoing
  1116. * deauth. Don't let it take us offline!
  1117. */
  1118. wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
  1119. "from Unknown BSSID " MACSTR " -- ignoring",
  1120. MAC2STR(bssid));
  1121. return;
  1122. }
  1123. }
  1124. drv->associated = 0;
  1125. os_memset(&event, 0, sizeof(event));
  1126. /* Note: Same offset for Reason Code in both frame subtypes */
  1127. if (len >= 24 + sizeof(mgmt->u.deauth))
  1128. reason_code = le_to_host16(mgmt->u.deauth.reason_code);
  1129. if (type == EVENT_DISASSOC) {
  1130. event.disassoc_info.locally_generated =
  1131. !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
  1132. event.disassoc_info.addr = bssid;
  1133. event.disassoc_info.reason_code = reason_code;
  1134. if (frame + len > mgmt->u.disassoc.variable) {
  1135. event.disassoc_info.ie = mgmt->u.disassoc.variable;
  1136. event.disassoc_info.ie_len = frame + len -
  1137. mgmt->u.disassoc.variable;
  1138. }
  1139. } else {
  1140. event.deauth_info.locally_generated =
  1141. !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
  1142. event.deauth_info.addr = bssid;
  1143. event.deauth_info.reason_code = reason_code;
  1144. if (frame + len > mgmt->u.deauth.variable) {
  1145. event.deauth_info.ie = mgmt->u.deauth.variable;
  1146. event.deauth_info.ie_len = frame + len -
  1147. mgmt->u.deauth.variable;
  1148. }
  1149. }
  1150. wpa_supplicant_event(drv->ctx, type, &event);
  1151. }
  1152. static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
  1153. enum wpa_event_type type,
  1154. const u8 *frame, size_t len)
  1155. {
  1156. const struct ieee80211_mgmt *mgmt;
  1157. union wpa_event_data event;
  1158. u16 reason_code = 0;
  1159. if (len < 24)
  1160. return;
  1161. mgmt = (const struct ieee80211_mgmt *) frame;
  1162. os_memset(&event, 0, sizeof(event));
  1163. /* Note: Same offset for Reason Code in both frame subtypes */
  1164. if (len >= 24 + sizeof(mgmt->u.deauth))
  1165. reason_code = le_to_host16(mgmt->u.deauth.reason_code);
  1166. if (type == EVENT_UNPROT_DISASSOC) {
  1167. event.unprot_disassoc.sa = mgmt->sa;
  1168. event.unprot_disassoc.da = mgmt->da;
  1169. event.unprot_disassoc.reason_code = reason_code;
  1170. } else {
  1171. event.unprot_deauth.sa = mgmt->sa;
  1172. event.unprot_deauth.da = mgmt->da;
  1173. event.unprot_deauth.reason_code = reason_code;
  1174. }
  1175. wpa_supplicant_event(drv->ctx, type, &event);
  1176. }
  1177. static void mlme_event(struct wpa_driver_nl80211_data *drv,
  1178. enum nl80211_commands cmd, struct nlattr *frame,
  1179. struct nlattr *addr, struct nlattr *timed_out,
  1180. struct nlattr *freq, struct nlattr *ack,
  1181. struct nlattr *cookie, struct nlattr *sig)
  1182. {
  1183. if (timed_out && addr) {
  1184. mlme_timeout_event(drv, cmd, addr);
  1185. return;
  1186. }
  1187. if (frame == NULL) {
  1188. wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
  1189. "data", cmd);
  1190. return;
  1191. }
  1192. wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd);
  1193. wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
  1194. nla_data(frame), nla_len(frame));
  1195. switch (cmd) {
  1196. case NL80211_CMD_AUTHENTICATE:
  1197. mlme_event_auth(drv, nla_data(frame), nla_len(frame));
  1198. break;
  1199. case NL80211_CMD_ASSOCIATE:
  1200. mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
  1201. break;
  1202. case NL80211_CMD_DEAUTHENTICATE:
  1203. mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
  1204. nla_data(frame), nla_len(frame));
  1205. break;
  1206. case NL80211_CMD_DISASSOCIATE:
  1207. mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
  1208. nla_data(frame), nla_len(frame));
  1209. break;
  1210. case NL80211_CMD_FRAME:
  1211. mlme_event_mgmt(drv, freq, sig, nla_data(frame),
  1212. nla_len(frame));
  1213. break;
  1214. case NL80211_CMD_FRAME_TX_STATUS:
  1215. mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
  1216. nla_len(frame), ack);
  1217. break;
  1218. case NL80211_CMD_UNPROT_DEAUTHENTICATE:
  1219. mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
  1220. nla_data(frame), nla_len(frame));
  1221. break;
  1222. case NL80211_CMD_UNPROT_DISASSOCIATE:
  1223. mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
  1224. nla_data(frame), nla_len(frame));
  1225. break;
  1226. default:
  1227. break;
  1228. }
  1229. }
  1230. static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
  1231. struct nlattr *tb[])
  1232. {
  1233. union wpa_event_data data;
  1234. wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
  1235. os_memset(&data, 0, sizeof(data));
  1236. if (tb[NL80211_ATTR_MAC]) {
  1237. wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
  1238. nla_data(tb[NL80211_ATTR_MAC]),
  1239. nla_len(tb[NL80211_ATTR_MAC]));
  1240. data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
  1241. }
  1242. if (tb[NL80211_ATTR_KEY_SEQ]) {
  1243. wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
  1244. nla_data(tb[NL80211_ATTR_KEY_SEQ]),
  1245. nla_len(tb[NL80211_ATTR_KEY_SEQ]));
  1246. }
  1247. if (tb[NL80211_ATTR_KEY_TYPE]) {
  1248. enum nl80211_key_type key_type =
  1249. nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
  1250. wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
  1251. if (key_type == NL80211_KEYTYPE_PAIRWISE)
  1252. data.michael_mic_failure.unicast = 1;
  1253. } else
  1254. data.michael_mic_failure.unicast = 1;
  1255. if (tb[NL80211_ATTR_KEY_IDX]) {
  1256. u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
  1257. wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
  1258. }
  1259. wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
  1260. }
  1261. static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
  1262. struct nlattr *tb[])
  1263. {
  1264. if (tb[NL80211_ATTR_MAC] == NULL) {
  1265. wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
  1266. "event");
  1267. return;
  1268. }
  1269. os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
  1270. drv->associated = 1;
  1271. wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
  1272. MAC2STR(drv->bssid));
  1273. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
  1274. }
  1275. static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
  1276. int cancel_event, struct nlattr *tb[])
  1277. {
  1278. unsigned int freq, chan_type, duration;
  1279. union wpa_event_data data;
  1280. u64 cookie;
  1281. if (tb[NL80211_ATTR_WIPHY_FREQ])
  1282. freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
  1283. else
  1284. freq = 0;
  1285. if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
  1286. chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
  1287. else
  1288. chan_type = 0;
  1289. if (tb[NL80211_ATTR_DURATION])
  1290. duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
  1291. else
  1292. duration = 0;
  1293. if (tb[NL80211_ATTR_COOKIE])
  1294. cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
  1295. else
  1296. cookie = 0;
  1297. wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
  1298. "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
  1299. cancel_event, freq, chan_type, duration,
  1300. (long long unsigned int) cookie,
  1301. cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
  1302. if (cookie != drv->remain_on_chan_cookie)
  1303. return; /* not for us */
  1304. if (cancel_event)
  1305. drv->pending_remain_on_chan = 0;
  1306. os_memset(&data, 0, sizeof(data));
  1307. data.remain_on_channel.freq = freq;
  1308. data.remain_on_channel.duration = duration;
  1309. wpa_supplicant_event(drv->ctx, cancel_event ?
  1310. EVENT_CANCEL_REMAIN_ON_CHANNEL :
  1311. EVENT_REMAIN_ON_CHANNEL, &data);
  1312. }
  1313. static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
  1314. struct nlattr *tb[])
  1315. {
  1316. union wpa_event_data event;
  1317. struct nlattr *nl;
  1318. int rem;
  1319. struct scan_info *info;
  1320. #define MAX_REPORT_FREQS 50
  1321. int freqs[MAX_REPORT_FREQS];
  1322. int num_freqs = 0;
  1323. if (drv->scan_for_auth) {
  1324. drv->scan_for_auth = 0;
  1325. wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
  1326. "cfg80211 BSS entry");
  1327. wpa_driver_nl80211_authenticate_retry(drv);
  1328. return;
  1329. }
  1330. os_memset(&event, 0, sizeof(event));
  1331. info = &event.scan_info;
  1332. info->aborted = aborted;
  1333. if (tb[NL80211_ATTR_SCAN_SSIDS]) {
  1334. nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
  1335. struct wpa_driver_scan_ssid *s =
  1336. &info->ssids[info->num_ssids];
  1337. s->ssid = nla_data(nl);
  1338. s->ssid_len = nla_len(nl);
  1339. info->num_ssids++;
  1340. if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
  1341. break;
  1342. }
  1343. }
  1344. if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
  1345. nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
  1346. {
  1347. freqs[num_freqs] = nla_get_u32(nl);
  1348. num_freqs++;
  1349. if (num_freqs == MAX_REPORT_FREQS - 1)
  1350. break;
  1351. }
  1352. info->freqs = freqs;
  1353. info->num_freqs = num_freqs;
  1354. }
  1355. wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
  1356. }
  1357. static int get_link_signal(struct nl_msg *msg, void *arg)
  1358. {
  1359. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1360. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1361. struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
  1362. static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
  1363. [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
  1364. };
  1365. struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
  1366. static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
  1367. [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
  1368. [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
  1369. [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
  1370. [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
  1371. };
  1372. struct wpa_signal_info *sig_change = arg;
  1373. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1374. genlmsg_attrlen(gnlh, 0), NULL);
  1375. if (!tb[NL80211_ATTR_STA_INFO] ||
  1376. nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
  1377. tb[NL80211_ATTR_STA_INFO], policy))
  1378. return NL_SKIP;
  1379. if (!sinfo[NL80211_STA_INFO_SIGNAL])
  1380. return NL_SKIP;
  1381. sig_change->current_signal =
  1382. (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
  1383. if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
  1384. if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
  1385. sinfo[NL80211_STA_INFO_TX_BITRATE],
  1386. rate_policy)) {
  1387. sig_change->current_txrate = 0;
  1388. } else {
  1389. if (rinfo[NL80211_RATE_INFO_BITRATE]) {
  1390. sig_change->current_txrate =
  1391. nla_get_u16(rinfo[
  1392. NL80211_RATE_INFO_BITRATE]) * 100;
  1393. }
  1394. }
  1395. }
  1396. return NL_SKIP;
  1397. }
  1398. static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
  1399. struct wpa_signal_info *sig)
  1400. {
  1401. struct nl_msg *msg;
  1402. sig->current_signal = -9999;
  1403. sig->current_txrate = 0;
  1404. msg = nlmsg_alloc();
  1405. if (!msg)
  1406. return -ENOMEM;
  1407. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
  1408. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1409. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
  1410. return send_and_recv_msgs(drv, msg, get_link_signal, sig);
  1411. nla_put_failure:
  1412. nlmsg_free(msg);
  1413. return -ENOBUFS;
  1414. }
  1415. static int get_link_noise(struct nl_msg *msg, void *arg)
  1416. {
  1417. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1418. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1419. struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
  1420. static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
  1421. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  1422. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  1423. };
  1424. struct wpa_signal_info *sig_change = arg;
  1425. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1426. genlmsg_attrlen(gnlh, 0), NULL);
  1427. if (!tb[NL80211_ATTR_SURVEY_INFO]) {
  1428. wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
  1429. return NL_SKIP;
  1430. }
  1431. if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
  1432. tb[NL80211_ATTR_SURVEY_INFO],
  1433. survey_policy)) {
  1434. wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
  1435. "attributes!");
  1436. return NL_SKIP;
  1437. }
  1438. if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
  1439. return NL_SKIP;
  1440. if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
  1441. sig_change->frequency)
  1442. return NL_SKIP;
  1443. if (!sinfo[NL80211_SURVEY_INFO_NOISE])
  1444. return NL_SKIP;
  1445. sig_change->current_noise =
  1446. (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
  1447. return NL_SKIP;
  1448. }
  1449. static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
  1450. struct wpa_signal_info *sig_change)
  1451. {
  1452. struct nl_msg *msg;
  1453. sig_change->current_noise = 9999;
  1454. sig_change->frequency = drv->assoc_freq;
  1455. msg = nlmsg_alloc();
  1456. if (!msg)
  1457. return -ENOMEM;
  1458. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
  1459. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1460. return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
  1461. nla_put_failure:
  1462. nlmsg_free(msg);
  1463. return -ENOBUFS;
  1464. }
  1465. static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
  1466. {
  1467. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1468. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1469. struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
  1470. static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
  1471. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  1472. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  1473. };
  1474. struct wpa_scan_results *scan_results = arg;
  1475. struct wpa_scan_res *scan_res;
  1476. size_t i;
  1477. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1478. genlmsg_attrlen(gnlh, 0), NULL);
  1479. if (!tb[NL80211_ATTR_SURVEY_INFO]) {
  1480. wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
  1481. return NL_SKIP;
  1482. }
  1483. if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
  1484. tb[NL80211_ATTR_SURVEY_INFO],
  1485. survey_policy)) {
  1486. wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
  1487. "attributes");
  1488. return NL_SKIP;
  1489. }
  1490. if (!sinfo[NL80211_SURVEY_INFO_NOISE])
  1491. return NL_SKIP;
  1492. if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
  1493. return NL_SKIP;
  1494. for (i = 0; i < scan_results->num; ++i) {
  1495. scan_res = scan_results->res[i];
  1496. if (!scan_res)
  1497. continue;
  1498. if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
  1499. scan_res->freq)
  1500. continue;
  1501. if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
  1502. continue;
  1503. scan_res->noise = (s8)
  1504. nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
  1505. scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
  1506. }
  1507. return NL_SKIP;
  1508. }
  1509. static int nl80211_get_noise_for_scan_results(
  1510. struct wpa_driver_nl80211_data *drv,
  1511. struct wpa_scan_results *scan_res)
  1512. {
  1513. struct nl_msg *msg;
  1514. msg = nlmsg_alloc();
  1515. if (!msg)
  1516. return -ENOMEM;
  1517. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
  1518. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1519. return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
  1520. scan_res);
  1521. nla_put_failure:
  1522. nlmsg_free(msg);
  1523. return -ENOBUFS;
  1524. }
  1525. static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
  1526. struct nlattr *tb[])
  1527. {
  1528. static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
  1529. [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
  1530. [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
  1531. [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
  1532. [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
  1533. };
  1534. struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
  1535. enum nl80211_cqm_rssi_threshold_event event;
  1536. union wpa_event_data ed;
  1537. struct wpa_signal_info sig;
  1538. int res;
  1539. if (tb[NL80211_ATTR_CQM] == NULL ||
  1540. nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
  1541. cqm_policy)) {
  1542. wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
  1543. return;
  1544. }
  1545. os_memset(&ed, 0, sizeof(ed));
  1546. if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
  1547. if (!tb[NL80211_ATTR_MAC])
  1548. return;
  1549. os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
  1550. ETH_ALEN);
  1551. wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
  1552. return;
  1553. }
  1554. if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
  1555. return;
  1556. event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
  1557. if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
  1558. wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
  1559. "event: RSSI high");
  1560. ed.signal_change.above_threshold = 1;
  1561. } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
  1562. wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
  1563. "event: RSSI low");
  1564. ed.signal_change.above_threshold = 0;
  1565. } else
  1566. return;
  1567. res = nl80211_get_link_signal(drv, &sig);
  1568. if (res == 0) {
  1569. ed.signal_change.current_signal = sig.current_signal;
  1570. ed.signal_change.current_txrate = sig.current_txrate;
  1571. wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
  1572. sig.current_signal, sig.current_txrate);
  1573. }
  1574. res = nl80211_get_link_noise(drv, &sig);
  1575. if (res == 0) {
  1576. ed.signal_change.current_noise = sig.current_noise;
  1577. wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
  1578. sig.current_noise);
  1579. }
  1580. wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
  1581. }
  1582. static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
  1583. struct nlattr **tb)
  1584. {
  1585. u8 *addr;
  1586. union wpa_event_data data;
  1587. if (tb[NL80211_ATTR_MAC] == NULL)
  1588. return;
  1589. addr = nla_data(tb[NL80211_ATTR_MAC]);
  1590. wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
  1591. if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
  1592. u8 *ies = NULL;
  1593. size_t ies_len = 0;
  1594. if (tb[NL80211_ATTR_IE]) {
  1595. ies = nla_data(tb[NL80211_ATTR_IE]);
  1596. ies_len = nla_len(tb[NL80211_ATTR_IE]);
  1597. }
  1598. wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
  1599. drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
  1600. return;
  1601. }
  1602. if (drv->nlmode != NL80211_IFTYPE_ADHOC)
  1603. return;
  1604. os_memset(&data, 0, sizeof(data));
  1605. os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
  1606. wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
  1607. }
  1608. static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
  1609. struct nlattr **tb)
  1610. {
  1611. u8 *addr;
  1612. union wpa_event_data data;
  1613. if (tb[NL80211_ATTR_MAC] == NULL)
  1614. return;
  1615. addr = nla_data(tb[NL80211_ATTR_MAC]);
  1616. wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
  1617. MAC2STR(addr));
  1618. if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
  1619. drv_event_disassoc(drv->ctx, addr);
  1620. return;
  1621. }
  1622. if (drv->nlmode != NL80211_IFTYPE_ADHOC)
  1623. return;
  1624. os_memset(&data, 0, sizeof(data));
  1625. os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
  1626. wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
  1627. }
  1628. static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
  1629. struct nlattr **tb)
  1630. {
  1631. struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
  1632. static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
  1633. [NL80211_REKEY_DATA_KEK] = {
  1634. .minlen = NL80211_KEK_LEN,
  1635. .maxlen = NL80211_KEK_LEN,
  1636. },
  1637. [NL80211_REKEY_DATA_KCK] = {
  1638. .minlen = NL80211_KCK_LEN,
  1639. .maxlen = NL80211_KCK_LEN,
  1640. },
  1641. [NL80211_REKEY_DATA_REPLAY_CTR] = {
  1642. .minlen = NL80211_REPLAY_CTR_LEN,
  1643. .maxlen = NL80211_REPLAY_CTR_LEN,
  1644. },
  1645. };
  1646. union wpa_event_data data;
  1647. if (!tb[NL80211_ATTR_MAC])
  1648. return;
  1649. if (!tb[NL80211_ATTR_REKEY_DATA])
  1650. return;
  1651. if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
  1652. tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
  1653. return;
  1654. if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
  1655. return;
  1656. os_memset(&data, 0, sizeof(data));
  1657. data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
  1658. wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
  1659. MAC2STR(data.driver_gtk_rekey.bssid));
  1660. data.driver_gtk_rekey.replay_ctr =
  1661. nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
  1662. wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
  1663. data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
  1664. wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
  1665. }
  1666. static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
  1667. struct nlattr **tb)
  1668. {
  1669. struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
  1670. static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
  1671. [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
  1672. [NL80211_PMKSA_CANDIDATE_BSSID] = {
  1673. .minlen = ETH_ALEN,
  1674. .maxlen = ETH_ALEN,
  1675. },
  1676. [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
  1677. };
  1678. union wpa_event_data data;
  1679. if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
  1680. return;
  1681. if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
  1682. tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
  1683. return;
  1684. if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
  1685. !cand[NL80211_PMKSA_CANDIDATE_BSSID])
  1686. return;
  1687. os_memset(&data, 0, sizeof(data));
  1688. os_memcpy(data.pmkid_candidate.bssid,
  1689. nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
  1690. data.pmkid_candidate.index =
  1691. nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
  1692. data.pmkid_candidate.preauth =
  1693. cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
  1694. wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
  1695. }
  1696. static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
  1697. struct nlattr **tb)
  1698. {
  1699. union wpa_event_data data;
  1700. if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
  1701. return;
  1702. os_memset(&data, 0, sizeof(data));
  1703. os_memcpy(data.client_poll.addr,
  1704. nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
  1705. wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
  1706. }
  1707. static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
  1708. int wds)
  1709. {
  1710. struct wpa_driver_nl80211_data *drv = bss->drv;
  1711. union wpa_event_data event;
  1712. if (!tb[NL80211_ATTR_MAC])
  1713. return;
  1714. os_memset(&event, 0, sizeof(event));
  1715. event.rx_from_unknown.bssid = bss->addr;
  1716. event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
  1717. event.rx_from_unknown.wds = wds;
  1718. wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
  1719. }
  1720. static void do_process_drv_event(struct wpa_driver_nl80211_data *drv,
  1721. int cmd, struct nlattr **tb)
  1722. {
  1723. if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
  1724. (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
  1725. cmd == NL80211_CMD_SCAN_ABORTED)) {
  1726. wpa_driver_nl80211_set_mode(&drv->first_bss,
  1727. drv->ap_scan_as_station);
  1728. drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
  1729. }
  1730. switch (cmd) {
  1731. case NL80211_CMD_TRIGGER_SCAN:
  1732. wpa_printf(MSG_DEBUG, "nl80211: Scan trigger");
  1733. break;
  1734. case NL80211_CMD_START_SCHED_SCAN:
  1735. wpa_printf(MSG_DEBUG, "nl80211: Sched scan started");
  1736. break;
  1737. case NL80211_CMD_SCHED_SCAN_STOPPED:
  1738. wpa_printf(MSG_DEBUG, "nl80211: Sched scan stopped");
  1739. wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
  1740. break;
  1741. case NL80211_CMD_NEW_SCAN_RESULTS:
  1742. wpa_printf(MSG_DEBUG, "nl80211: New scan results available");
  1743. drv->scan_complete_events = 1;
  1744. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
  1745. drv->ctx);
  1746. send_scan_event(drv, 0, tb);
  1747. break;
  1748. case NL80211_CMD_SCHED_SCAN_RESULTS:
  1749. wpa_printf(MSG_DEBUG,
  1750. "nl80211: New sched scan results available");
  1751. send_scan_event(drv, 0, tb);
  1752. break;
  1753. case NL80211_CMD_SCAN_ABORTED:
  1754. wpa_printf(MSG_DEBUG, "nl80211: Scan aborted");
  1755. /*
  1756. * Need to indicate that scan results are available in order
  1757. * not to make wpa_supplicant stop its scanning.
  1758. */
  1759. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
  1760. drv->ctx);
  1761. send_scan_event(drv, 1, tb);
  1762. break;
  1763. case NL80211_CMD_AUTHENTICATE:
  1764. case NL80211_CMD_ASSOCIATE:
  1765. case NL80211_CMD_DEAUTHENTICATE:
  1766. case NL80211_CMD_DISASSOCIATE:
  1767. case NL80211_CMD_FRAME_TX_STATUS:
  1768. case NL80211_CMD_UNPROT_DEAUTHENTICATE:
  1769. case NL80211_CMD_UNPROT_DISASSOCIATE:
  1770. mlme_event(drv, cmd, tb[NL80211_ATTR_FRAME],
  1771. tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
  1772. tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
  1773. tb[NL80211_ATTR_COOKIE],
  1774. tb[NL80211_ATTR_RX_SIGNAL_DBM]);
  1775. break;
  1776. case NL80211_CMD_CONNECT:
  1777. case NL80211_CMD_ROAM:
  1778. mlme_event_connect(drv, cmd,
  1779. tb[NL80211_ATTR_STATUS_CODE],
  1780. tb[NL80211_ATTR_MAC],
  1781. tb[NL80211_ATTR_REQ_IE],
  1782. tb[NL80211_ATTR_RESP_IE]);
  1783. break;
  1784. case NL80211_CMD_CH_SWITCH_NOTIFY:
  1785. mlme_event_ch_switch(drv, tb[NL80211_ATTR_WIPHY_FREQ],
  1786. tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
  1787. break;
  1788. case NL80211_CMD_DISCONNECT:
  1789. mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
  1790. tb[NL80211_ATTR_MAC],
  1791. tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
  1792. break;
  1793. case NL80211_CMD_MICHAEL_MIC_FAILURE:
  1794. mlme_event_michael_mic_failure(drv, tb);
  1795. break;
  1796. case NL80211_CMD_JOIN_IBSS:
  1797. mlme_event_join_ibss(drv, tb);
  1798. break;
  1799. case NL80211_CMD_REMAIN_ON_CHANNEL:
  1800. mlme_event_remain_on_channel(drv, 0, tb);
  1801. break;
  1802. case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
  1803. mlme_event_remain_on_channel(drv, 1, tb);
  1804. break;
  1805. case NL80211_CMD_NOTIFY_CQM:
  1806. nl80211_cqm_event(drv, tb);
  1807. break;
  1808. case NL80211_CMD_REG_CHANGE:
  1809. wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
  1810. wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
  1811. NULL);
  1812. break;
  1813. case NL80211_CMD_REG_BEACON_HINT:
  1814. wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
  1815. wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
  1816. NULL);
  1817. break;
  1818. case NL80211_CMD_NEW_STATION:
  1819. nl80211_new_station_event(drv, tb);
  1820. break;
  1821. case NL80211_CMD_DEL_STATION:
  1822. nl80211_del_station_event(drv, tb);
  1823. break;
  1824. case NL80211_CMD_SET_REKEY_OFFLOAD:
  1825. nl80211_rekey_offload_event(drv, tb);
  1826. break;
  1827. case NL80211_CMD_PMKSA_CANDIDATE:
  1828. nl80211_pmksa_candidate_event(drv, tb);
  1829. break;
  1830. case NL80211_CMD_PROBE_CLIENT:
  1831. nl80211_client_probe_event(drv, tb);
  1832. break;
  1833. default:
  1834. wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
  1835. "(cmd=%d)", cmd);
  1836. break;
  1837. }
  1838. }
  1839. static int process_drv_event(struct nl_msg *msg, void *arg)
  1840. {
  1841. struct wpa_driver_nl80211_data *drv = arg;
  1842. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1843. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1844. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1845. genlmsg_attrlen(gnlh, 0), NULL);
  1846. if (tb[NL80211_ATTR_IFINDEX]) {
  1847. int ifindex = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
  1848. if (ifindex != drv->ifindex && !have_ifidx(drv, ifindex)) {
  1849. wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d)"
  1850. " for foreign interface (ifindex %d)",
  1851. gnlh->cmd, ifindex);
  1852. return NL_SKIP;
  1853. }
  1854. }
  1855. do_process_drv_event(drv, gnlh->cmd, tb);
  1856. return NL_SKIP;
  1857. }
  1858. static int process_global_event(struct nl_msg *msg, void *arg)
  1859. {
  1860. struct nl80211_global *global = arg;
  1861. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1862. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1863. struct wpa_driver_nl80211_data *drv, *tmp;
  1864. int ifidx = -1;
  1865. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1866. genlmsg_attrlen(gnlh, 0), NULL);
  1867. if (tb[NL80211_ATTR_IFINDEX])
  1868. ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
  1869. dl_list_for_each_safe(drv, tmp, &global->interfaces,
  1870. struct wpa_driver_nl80211_data, list) {
  1871. if (ifidx == -1 || ifidx == drv->ifindex ||
  1872. have_ifidx(drv, ifidx))
  1873. do_process_drv_event(drv, gnlh->cmd, tb);
  1874. }
  1875. return NL_SKIP;
  1876. }
  1877. static int process_bss_event(struct nl_msg *msg, void *arg)
  1878. {
  1879. struct i802_bss *bss = arg;
  1880. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1881. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1882. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1883. genlmsg_attrlen(gnlh, 0), NULL);
  1884. switch (gnlh->cmd) {
  1885. case NL80211_CMD_FRAME:
  1886. case NL80211_CMD_FRAME_TX_STATUS:
  1887. mlme_event(bss->drv, gnlh->cmd, tb[NL80211_ATTR_FRAME],
  1888. tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
  1889. tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
  1890. tb[NL80211_ATTR_COOKIE],
  1891. tb[NL80211_ATTR_RX_SIGNAL_DBM]);
  1892. break;
  1893. case NL80211_CMD_UNEXPECTED_FRAME:
  1894. nl80211_spurious_frame(bss, tb, 0);
  1895. break;
  1896. case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
  1897. nl80211_spurious_frame(bss, tb, 1);
  1898. break;
  1899. default:
  1900. wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
  1901. "(cmd=%d)", gnlh->cmd);
  1902. break;
  1903. }
  1904. return NL_SKIP;
  1905. }
  1906. static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
  1907. void *handle)
  1908. {
  1909. struct nl_cb *cb = eloop_ctx;
  1910. wpa_printf(MSG_DEBUG, "nl80211: Event message available");
  1911. nl_recvmsgs(handle, cb);
  1912. }
  1913. /**
  1914. * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
  1915. * @priv: driver_nl80211 private data
  1916. * @alpha2_arg: country to which to switch to
  1917. * Returns: 0 on success, -1 on failure
  1918. *
  1919. * This asks nl80211 to set the regulatory domain for given
  1920. * country ISO / IEC alpha2.
  1921. */
  1922. static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
  1923. {
  1924. struct i802_bss *bss = priv;
  1925. struct wpa_driver_nl80211_data *drv = bss->drv;
  1926. char alpha2[3];
  1927. struct nl_msg *msg;
  1928. msg = nlmsg_alloc();
  1929. if (!msg)
  1930. return -ENOMEM;
  1931. alpha2[0] = alpha2_arg[0];
  1932. alpha2[1] = alpha2_arg[1];
  1933. alpha2[2] = '\0';
  1934. nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
  1935. NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
  1936. if (send_and_recv_msgs(drv, msg, NULL, NULL))
  1937. return -EINVAL;
  1938. return 0;
  1939. nla_put_failure:
  1940. nlmsg_free(msg);
  1941. return -EINVAL;
  1942. }
  1943. struct wiphy_info_data {
  1944. struct wpa_driver_capa *capa;
  1945. unsigned int error:1;
  1946. unsigned int device_ap_sme:1;
  1947. unsigned int poll_command_supported:1;
  1948. unsigned int data_tx_status:1;
  1949. unsigned int monitor_supported:1;
  1950. };
  1951. static unsigned int probe_resp_offload_support(int supp_protocols)
  1952. {
  1953. unsigned int prot = 0;
  1954. if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
  1955. prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
  1956. if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
  1957. prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
  1958. if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
  1959. prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
  1960. if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
  1961. prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
  1962. return prot;
  1963. }
  1964. static int wiphy_info_handler(struct nl_msg *msg, void *arg)
  1965. {
  1966. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1967. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1968. struct wiphy_info_data *info = arg;
  1969. int p2p_go_supported = 0, p2p_client_supported = 0;
  1970. int p2p_concurrent = 0, p2p_multichan_concurrent = 0;
  1971. int auth_supported = 0, connect_supported = 0;
  1972. struct wpa_driver_capa *capa = info->capa;
  1973. static struct nla_policy
  1974. iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
  1975. [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
  1976. [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
  1977. [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
  1978. [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
  1979. },
  1980. iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
  1981. [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
  1982. [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
  1983. };
  1984. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1985. genlmsg_attrlen(gnlh, 0), NULL);
  1986. if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
  1987. capa->max_scan_ssids =
  1988. nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
  1989. if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
  1990. capa->max_sched_scan_ssids =
  1991. nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
  1992. if (tb[NL80211_ATTR_MAX_MATCH_SETS])
  1993. capa->max_match_sets =
  1994. nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
  1995. if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) {
  1996. struct nlattr *nl_mode;
  1997. int i;
  1998. nla_for_each_nested(nl_mode,
  1999. tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) {
  2000. switch (nla_type(nl_mode)) {
  2001. case NL80211_IFTYPE_AP:
  2002. capa->flags |= WPA_DRIVER_FLAGS_AP;
  2003. break;
  2004. case NL80211_IFTYPE_P2P_GO:
  2005. p2p_go_supported = 1;
  2006. break;
  2007. case NL80211_IFTYPE_P2P_CLIENT:
  2008. p2p_client_supported = 1;
  2009. break;
  2010. case NL80211_IFTYPE_MONITOR:
  2011. info->monitor_supported = 1;
  2012. break;
  2013. }
  2014. }
  2015. }
  2016. if (tb[NL80211_ATTR_INTERFACE_COMBINATIONS]) {
  2017. struct nlattr *nl_combi;
  2018. int rem_combi;
  2019. nla_for_each_nested(nl_combi,
  2020. tb[NL80211_ATTR_INTERFACE_COMBINATIONS],
  2021. rem_combi) {
  2022. struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
  2023. struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
  2024. struct nlattr *nl_limit, *nl_mode;
  2025. int err, rem_limit, rem_mode;
  2026. int combination_has_p2p = 0, combination_has_mgd = 0;
  2027. err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
  2028. nl_combi,
  2029. iface_combination_policy);
  2030. if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
  2031. !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
  2032. !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
  2033. goto broken_combination;
  2034. nla_for_each_nested(nl_limit,
  2035. tb_comb[NL80211_IFACE_COMB_LIMITS],
  2036. rem_limit) {
  2037. err = nla_parse_nested(tb_limit,
  2038. MAX_NL80211_IFACE_LIMIT,
  2039. nl_limit,
  2040. iface_limit_policy);
  2041. if (err ||
  2042. !tb_limit[NL80211_IFACE_LIMIT_TYPES])
  2043. goto broken_combination;
  2044. nla_for_each_nested(
  2045. nl_mode,
  2046. tb_limit[NL80211_IFACE_LIMIT_TYPES],
  2047. rem_mode) {
  2048. int ift = nla_type(nl_mode);
  2049. if (ift == NL80211_IFTYPE_P2P_GO ||
  2050. ift == NL80211_IFTYPE_P2P_CLIENT)
  2051. combination_has_p2p = 1;
  2052. if (ift == NL80211_IFTYPE_STATION)
  2053. combination_has_mgd = 1;
  2054. }
  2055. if (combination_has_p2p && combination_has_mgd)
  2056. break;
  2057. }
  2058. if (combination_has_p2p && combination_has_mgd) {
  2059. p2p_concurrent = 1;
  2060. if (nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]) > 1)
  2061. p2p_multichan_concurrent = 1;
  2062. break;
  2063. }
  2064. broken_combination:
  2065. ;
  2066. }
  2067. }
  2068. if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) {
  2069. struct nlattr *nl_cmd;
  2070. int i;
  2071. nla_for_each_nested(nl_cmd,
  2072. tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) {
  2073. switch (nla_get_u32(nl_cmd)) {
  2074. case NL80211_CMD_AUTHENTICATE:
  2075. auth_supported = 1;
  2076. break;
  2077. case NL80211_CMD_CONNECT:
  2078. connect_supported = 1;
  2079. break;
  2080. case NL80211_CMD_START_SCHED_SCAN:
  2081. capa->sched_scan_supported = 1;
  2082. break;
  2083. case NL80211_CMD_PROBE_CLIENT:
  2084. info->poll_command_supported = 1;
  2085. break;
  2086. }
  2087. }
  2088. }
  2089. if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
  2090. wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
  2091. "off-channel TX");
  2092. capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
  2093. }
  2094. if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
  2095. wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
  2096. capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
  2097. }
  2098. /* default to 5000 since early versions of mac80211 don't set it */
  2099. capa->max_remain_on_chan = 5000;
  2100. if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
  2101. capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
  2102. if (tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION])
  2103. capa->max_remain_on_chan =
  2104. nla_get_u32(tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
  2105. if (auth_supported)
  2106. capa->flags |= WPA_DRIVER_FLAGS_SME;
  2107. else if (!connect_supported) {
  2108. wpa_printf(MSG_INFO, "nl80211: Driver does not support "
  2109. "authentication/association or connect commands");
  2110. info->error = 1;
  2111. }
  2112. if (p2p_go_supported && p2p_client_supported)
  2113. capa->flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
  2114. if (p2p_concurrent) {
  2115. wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
  2116. "interface (driver advertised support)");
  2117. capa->flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
  2118. capa->flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
  2119. if (p2p_multichan_concurrent) {
  2120. wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
  2121. "concurrent (driver advertised support)");
  2122. capa->flags |=
  2123. WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT;
  2124. }
  2125. }
  2126. if (tb[NL80211_ATTR_TDLS_SUPPORT]) {
  2127. wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
  2128. capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
  2129. if (tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]) {
  2130. wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
  2131. capa->flags |=
  2132. WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
  2133. }
  2134. }
  2135. if (tb[NL80211_ATTR_DEVICE_AP_SME])
  2136. info->device_ap_sme = 1;
  2137. if (tb[NL80211_ATTR_FEATURE_FLAGS]) {
  2138. u32 flags = nla_get_u32(tb[NL80211_ATTR_FEATURE_FLAGS]);
  2139. if (flags & NL80211_FEATURE_SK_TX_STATUS)
  2140. info->data_tx_status = 1;
  2141. if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
  2142. capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
  2143. }
  2144. if (tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]) {
  2145. int protocols =
  2146. nla_get_u32(tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
  2147. wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response "
  2148. "offload in AP mode");
  2149. capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
  2150. capa->probe_resp_offloads =
  2151. probe_resp_offload_support(protocols);
  2152. }
  2153. return NL_SKIP;
  2154. }
  2155. static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
  2156. struct wiphy_info_data *info)
  2157. {
  2158. struct nl_msg *msg;
  2159. os_memset(info, 0, sizeof(*info));
  2160. info->capa = &drv->capa;
  2161. msg = nlmsg_alloc();
  2162. if (!msg)
  2163. return -1;
  2164. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
  2165. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex);
  2166. if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info) == 0)
  2167. return 0;
  2168. msg = NULL;
  2169. nla_put_failure:
  2170. nlmsg_free(msg);
  2171. return -1;
  2172. }
  2173. static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
  2174. {
  2175. struct wiphy_info_data info;
  2176. if (wpa_driver_nl80211_get_info(drv, &info))
  2177. return -1;
  2178. if (info.error)
  2179. return -1;
  2180. drv->has_capability = 1;
  2181. /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
  2182. drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2183. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2184. WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2185. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
  2186. drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
  2187. WPA_DRIVER_CAPA_ENC_WEP104 |
  2188. WPA_DRIVER_CAPA_ENC_TKIP |
  2189. WPA_DRIVER_CAPA_ENC_CCMP;
  2190. drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
  2191. WPA_DRIVER_AUTH_SHARED |
  2192. WPA_DRIVER_AUTH_LEAP;
  2193. drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
  2194. drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
  2195. drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
  2196. if (!info.device_ap_sme) {
  2197. drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
  2198. /*
  2199. * No AP SME is currently assumed to also indicate no AP MLME
  2200. * in the driver/firmware.
  2201. */
  2202. drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
  2203. }
  2204. drv->device_ap_sme = info.device_ap_sme;
  2205. drv->poll_command_supported = info.poll_command_supported;
  2206. drv->data_tx_status = info.data_tx_status;
  2207. /*
  2208. * If poll command and tx status are supported, mac80211 is new enough
  2209. * to have everything we need to not need monitor interfaces.
  2210. */
  2211. drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
  2212. if (drv->device_ap_sme && drv->use_monitor) {
  2213. /*
  2214. * Non-mac80211 drivers may not support monitor interface.
  2215. * Make sure we do not get stuck with incorrect capability here
  2216. * by explicitly testing this.
  2217. */
  2218. if (!info.monitor_supported) {
  2219. wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
  2220. "with device_ap_sme since no monitor mode "
  2221. "support detected");
  2222. drv->use_monitor = 0;
  2223. }
  2224. }
  2225. /*
  2226. * If we aren't going to use monitor interfaces, but the
  2227. * driver doesn't support data TX status, we won't get TX
  2228. * status for EAPOL frames.
  2229. */
  2230. if (!drv->use_monitor && !info.data_tx_status)
  2231. drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
  2232. return 0;
  2233. }
  2234. #ifdef ANDROID
  2235. static int android_genl_ctrl_resolve(struct nl_handle *handle,
  2236. const char *name)
  2237. {
  2238. /*
  2239. * Android ICS has very minimal genl_ctrl_resolve() implementation, so
  2240. * need to work around that.
  2241. */
  2242. struct nl_cache *cache = NULL;
  2243. struct genl_family *nl80211 = NULL;
  2244. int id = -1;
  2245. if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
  2246. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  2247. "netlink cache");
  2248. goto fail;
  2249. }
  2250. nl80211 = genl_ctrl_search_by_name(cache, name);
  2251. if (nl80211 == NULL)
  2252. goto fail;
  2253. id = genl_family_get_id(nl80211);
  2254. fail:
  2255. if (nl80211)
  2256. genl_family_put(nl80211);
  2257. if (cache)
  2258. nl_cache_free(cache);
  2259. return id;
  2260. }
  2261. #define genl_ctrl_resolve android_genl_ctrl_resolve
  2262. #endif /* ANDROID */
  2263. static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
  2264. {
  2265. int ret;
  2266. global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  2267. if (global->nl_cb == NULL) {
  2268. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  2269. "callbacks");
  2270. return -1;
  2271. }
  2272. global->nl = nl_create_handle(global->nl_cb, "nl");
  2273. if (global->nl == NULL)
  2274. goto err;
  2275. global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
  2276. if (global->nl80211_id < 0) {
  2277. wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
  2278. "found");
  2279. goto err;
  2280. }
  2281. global->nl_event = nl_create_handle(global->nl_cb, "event");
  2282. if (global->nl_event == NULL)
  2283. goto err;
  2284. ret = nl_get_multicast_id(global, "nl80211", "scan");
  2285. if (ret >= 0)
  2286. ret = nl_socket_add_membership(global->nl_event, ret);
  2287. if (ret < 0) {
  2288. wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
  2289. "membership for scan events: %d (%s)",
  2290. ret, strerror(-ret));
  2291. goto err;
  2292. }
  2293. ret = nl_get_multicast_id(global, "nl80211", "mlme");
  2294. if (ret >= 0)
  2295. ret = nl_socket_add_membership(global->nl_event, ret);
  2296. if (ret < 0) {
  2297. wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
  2298. "membership for mlme events: %d (%s)",
  2299. ret, strerror(-ret));
  2300. goto err;
  2301. }
  2302. ret = nl_get_multicast_id(global, "nl80211", "regulatory");
  2303. if (ret >= 0)
  2304. ret = nl_socket_add_membership(global->nl_event, ret);
  2305. if (ret < 0) {
  2306. wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
  2307. "membership for regulatory events: %d (%s)",
  2308. ret, strerror(-ret));
  2309. /* Continue without regulatory events */
  2310. }
  2311. nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
  2312. no_seq_check, NULL);
  2313. nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
  2314. process_global_event, global);
  2315. eloop_register_read_sock(nl_socket_get_fd(global->nl_event),
  2316. wpa_driver_nl80211_event_receive,
  2317. global->nl_cb, global->nl_event);
  2318. return 0;
  2319. err:
  2320. nl_destroy_handles(&global->nl_event);
  2321. nl_destroy_handles(&global->nl);
  2322. nl_cb_put(global->nl_cb);
  2323. global->nl_cb = NULL;
  2324. return -1;
  2325. }
  2326. static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
  2327. {
  2328. drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  2329. if (!drv->nl_cb) {
  2330. wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
  2331. return -1;
  2332. }
  2333. nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
  2334. no_seq_check, NULL);
  2335. nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
  2336. process_drv_event, drv);
  2337. return 0;
  2338. }
  2339. static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
  2340. {
  2341. wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
  2342. /*
  2343. * This may be for any interface; use ifdown event to disable
  2344. * interface.
  2345. */
  2346. }
  2347. static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
  2348. {
  2349. struct wpa_driver_nl80211_data *drv = ctx;
  2350. wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
  2351. if (linux_set_iface_flags(drv->global->ioctl_sock,
  2352. drv->first_bss.ifname, 1)) {
  2353. wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
  2354. "after rfkill unblock");
  2355. return;
  2356. }
  2357. /* rtnetlink ifup handler will report interface as enabled */
  2358. }
  2359. static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv)
  2360. {
  2361. /* Find phy (radio) to which this interface belongs */
  2362. char buf[90], *pos;
  2363. int f, rv;
  2364. drv->phyname[0] = '\0';
  2365. snprintf(buf, sizeof(buf) - 1, "/sys/class/net/%s/phy80211/name",
  2366. drv->first_bss.ifname);
  2367. f = open(buf, O_RDONLY);
  2368. if (f < 0) {
  2369. wpa_printf(MSG_DEBUG, "Could not open file %s: %s",
  2370. buf, strerror(errno));
  2371. return;
  2372. }
  2373. rv = read(f, drv->phyname, sizeof(drv->phyname) - 1);
  2374. close(f);
  2375. if (rv < 0) {
  2376. wpa_printf(MSG_DEBUG, "Could not read file %s: %s",
  2377. buf, strerror(errno));
  2378. return;
  2379. }
  2380. drv->phyname[rv] = '\0';
  2381. pos = os_strchr(drv->phyname, '\n');
  2382. if (pos)
  2383. *pos = '\0';
  2384. wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
  2385. drv->first_bss.ifname, drv->phyname);
  2386. }
  2387. static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
  2388. void *eloop_ctx,
  2389. void *handle)
  2390. {
  2391. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  2392. u8 data[2048];
  2393. struct msghdr msg;
  2394. struct iovec entry;
  2395. u8 control[512];
  2396. struct cmsghdr *cmsg;
  2397. int res, found_ee = 0, found_wifi = 0, acked = 0;
  2398. union wpa_event_data event;
  2399. memset(&msg, 0, sizeof(msg));
  2400. msg.msg_iov = &entry;
  2401. msg.msg_iovlen = 1;
  2402. entry.iov_base = data;
  2403. entry.iov_len = sizeof(data);
  2404. msg.msg_control = &control;
  2405. msg.msg_controllen = sizeof(control);
  2406. res = recvmsg(sock, &msg, MSG_ERRQUEUE);
  2407. /* if error or not fitting 802.3 header, return */
  2408. if (res < 14)
  2409. return;
  2410. for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
  2411. {
  2412. if (cmsg->cmsg_level == SOL_SOCKET &&
  2413. cmsg->cmsg_type == SCM_WIFI_STATUS) {
  2414. int *ack;
  2415. found_wifi = 1;
  2416. ack = (void *)CMSG_DATA(cmsg);
  2417. acked = *ack;
  2418. }
  2419. if (cmsg->cmsg_level == SOL_PACKET &&
  2420. cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
  2421. struct sock_extended_err *err =
  2422. (struct sock_extended_err *)CMSG_DATA(cmsg);
  2423. if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
  2424. found_ee = 1;
  2425. }
  2426. }
  2427. if (!found_ee || !found_wifi)
  2428. return;
  2429. memset(&event, 0, sizeof(event));
  2430. event.eapol_tx_status.dst = data;
  2431. event.eapol_tx_status.data = data + 14;
  2432. event.eapol_tx_status.data_len = res - 14;
  2433. event.eapol_tx_status.ack = acked;
  2434. wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
  2435. }
  2436. static int nl80211_init_bss(struct i802_bss *bss)
  2437. {
  2438. bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  2439. if (!bss->nl_cb)
  2440. return -1;
  2441. nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
  2442. no_seq_check, NULL);
  2443. nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
  2444. process_bss_event, bss);
  2445. return 0;
  2446. }
  2447. static void nl80211_destroy_bss(struct i802_bss *bss)
  2448. {
  2449. nl_cb_put(bss->nl_cb);
  2450. bss->nl_cb = NULL;
  2451. }
  2452. /**
  2453. * wpa_driver_nl80211_init - Initialize nl80211 driver interface
  2454. * @ctx: context to be used when calling wpa_supplicant functions,
  2455. * e.g., wpa_supplicant_event()
  2456. * @ifname: interface name, e.g., wlan0
  2457. * @global_priv: private driver global data from global_init()
  2458. * Returns: Pointer to private data, %NULL on failure
  2459. */
  2460. static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
  2461. void *global_priv)
  2462. {
  2463. struct wpa_driver_nl80211_data *drv;
  2464. struct rfkill_config *rcfg;
  2465. struct i802_bss *bss;
  2466. if (global_priv == NULL)
  2467. return NULL;
  2468. drv = os_zalloc(sizeof(*drv));
  2469. if (drv == NULL)
  2470. return NULL;
  2471. drv->global = global_priv;
  2472. drv->ctx = ctx;
  2473. bss = &drv->first_bss;
  2474. bss->drv = drv;
  2475. os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
  2476. drv->monitor_ifidx = -1;
  2477. drv->monitor_sock = -1;
  2478. drv->eapol_tx_sock = -1;
  2479. drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
  2480. if (wpa_driver_nl80211_init_nl(drv)) {
  2481. os_free(drv);
  2482. return NULL;
  2483. }
  2484. if (nl80211_init_bss(bss))
  2485. goto failed;
  2486. nl80211_get_phy_name(drv);
  2487. rcfg = os_zalloc(sizeof(*rcfg));
  2488. if (rcfg == NULL)
  2489. goto failed;
  2490. rcfg->ctx = drv;
  2491. os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
  2492. rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
  2493. rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
  2494. drv->rfkill = rfkill_init(rcfg);
  2495. if (drv->rfkill == NULL) {
  2496. wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
  2497. os_free(rcfg);
  2498. }
  2499. if (wpa_driver_nl80211_finish_drv_init(drv))
  2500. goto failed;
  2501. drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
  2502. if (drv->eapol_tx_sock < 0)
  2503. goto failed;
  2504. if (drv->data_tx_status) {
  2505. int enabled = 1;
  2506. if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
  2507. &enabled, sizeof(enabled)) < 0) {
  2508. wpa_printf(MSG_DEBUG,
  2509. "nl80211: wifi status sockopt failed\n");
  2510. drv->data_tx_status = 0;
  2511. if (!drv->use_monitor)
  2512. drv->capa.flags &=
  2513. ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
  2514. } else {
  2515. eloop_register_read_sock(drv->eapol_tx_sock,
  2516. wpa_driver_nl80211_handle_eapol_tx_status,
  2517. drv, NULL);
  2518. }
  2519. }
  2520. if (drv->global) {
  2521. dl_list_add(&drv->global->interfaces, &drv->list);
  2522. drv->in_interface_list = 1;
  2523. }
  2524. return bss;
  2525. failed:
  2526. wpa_driver_nl80211_deinit(bss);
  2527. return NULL;
  2528. }
  2529. static int nl80211_register_frame(struct i802_bss *bss,
  2530. struct nl_handle *nl_handle,
  2531. u16 type, const u8 *match, size_t match_len)
  2532. {
  2533. struct wpa_driver_nl80211_data *drv = bss->drv;
  2534. struct nl_msg *msg;
  2535. int ret = -1;
  2536. msg = nlmsg_alloc();
  2537. if (!msg)
  2538. return -1;
  2539. wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p",
  2540. type, nl_handle);
  2541. wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
  2542. match, match_len);
  2543. nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
  2544. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  2545. NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
  2546. NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
  2547. ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
  2548. msg = NULL;
  2549. if (ret) {
  2550. wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
  2551. "failed (type=%u): ret=%d (%s)",
  2552. type, ret, strerror(-ret));
  2553. wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
  2554. match, match_len);
  2555. goto nla_put_failure;
  2556. }
  2557. ret = 0;
  2558. nla_put_failure:
  2559. nlmsg_free(msg);
  2560. return ret;
  2561. }
  2562. static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
  2563. {
  2564. struct wpa_driver_nl80211_data *drv = bss->drv;
  2565. if (bss->nl_mgmt) {
  2566. wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
  2567. "already on! (nl_mgmt=%p)", bss->nl_mgmt);
  2568. return -1;
  2569. }
  2570. bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
  2571. if (bss->nl_mgmt == NULL)
  2572. return -1;
  2573. eloop_register_read_sock(nl_socket_get_fd(bss->nl_mgmt),
  2574. wpa_driver_nl80211_event_receive, bss->nl_cb,
  2575. bss->nl_mgmt);
  2576. return 0;
  2577. }
  2578. static int nl80211_register_action_frame(struct i802_bss *bss,
  2579. const u8 *match, size_t match_len)
  2580. {
  2581. u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
  2582. return nl80211_register_frame(bss, bss->nl_mgmt,
  2583. type, match, match_len);
  2584. }
  2585. static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
  2586. {
  2587. struct wpa_driver_nl80211_data *drv = bss->drv;
  2588. if (nl80211_alloc_mgmt_handle(bss))
  2589. return -1;
  2590. wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
  2591. "handle %p", bss->nl_mgmt);
  2592. #if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
  2593. /* GAS Initial Request */
  2594. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
  2595. return -1;
  2596. /* GAS Initial Response */
  2597. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
  2598. return -1;
  2599. /* GAS Comeback Request */
  2600. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
  2601. return -1;
  2602. /* GAS Comeback Response */
  2603. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
  2604. return -1;
  2605. #endif /* CONFIG_P2P || CONFIG_INTERWORKING */
  2606. #ifdef CONFIG_P2P
  2607. /* P2P Public Action */
  2608. if (nl80211_register_action_frame(bss,
  2609. (u8 *) "\x04\x09\x50\x6f\x9a\x09",
  2610. 6) < 0)
  2611. return -1;
  2612. /* P2P Action */
  2613. if (nl80211_register_action_frame(bss,
  2614. (u8 *) "\x7f\x50\x6f\x9a\x09",
  2615. 5) < 0)
  2616. return -1;
  2617. #endif /* CONFIG_P2P */
  2618. #ifdef CONFIG_IEEE80211W
  2619. /* SA Query Response */
  2620. if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
  2621. return -1;
  2622. #endif /* CONFIG_IEEE80211W */
  2623. #ifdef CONFIG_TDLS
  2624. if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
  2625. /* TDLS Discovery Response */
  2626. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
  2627. 0)
  2628. return -1;
  2629. }
  2630. #endif /* CONFIG_TDLS */
  2631. /* FT Action frames */
  2632. if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
  2633. return -1;
  2634. else
  2635. drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
  2636. WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
  2637. /* WNM - BSS Transition Management Request */
  2638. if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
  2639. return -1;
  2640. return 0;
  2641. }
  2642. static int nl80211_register_spurious_class3(struct i802_bss *bss)
  2643. {
  2644. struct wpa_driver_nl80211_data *drv = bss->drv;
  2645. struct nl_msg *msg;
  2646. int ret = -1;
  2647. msg = nlmsg_alloc();
  2648. if (!msg)
  2649. return -1;
  2650. nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
  2651. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  2652. ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
  2653. msg = NULL;
  2654. if (ret) {
  2655. wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
  2656. "failed: ret=%d (%s)",
  2657. ret, strerror(-ret));
  2658. goto nla_put_failure;
  2659. }
  2660. ret = 0;
  2661. nla_put_failure:
  2662. nlmsg_free(msg);
  2663. return ret;
  2664. }
  2665. static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
  2666. {
  2667. static const int stypes[] = {
  2668. WLAN_FC_STYPE_AUTH,
  2669. WLAN_FC_STYPE_ASSOC_REQ,
  2670. WLAN_FC_STYPE_REASSOC_REQ,
  2671. WLAN_FC_STYPE_DISASSOC,
  2672. WLAN_FC_STYPE_DEAUTH,
  2673. WLAN_FC_STYPE_ACTION,
  2674. WLAN_FC_STYPE_PROBE_REQ,
  2675. /* Beacon doesn't work as mac80211 doesn't currently allow
  2676. * it, but it wouldn't really be the right thing anyway as
  2677. * it isn't per interface ... maybe just dump the scan
  2678. * results periodically for OLBC?
  2679. */
  2680. // WLAN_FC_STYPE_BEACON,
  2681. };
  2682. unsigned int i;
  2683. if (nl80211_alloc_mgmt_handle(bss))
  2684. return -1;
  2685. wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
  2686. "handle %p", bss->nl_mgmt);
  2687. for (i = 0; i < sizeof(stypes) / sizeof(stypes[0]); i++) {
  2688. if (nl80211_register_frame(bss, bss->nl_mgmt,
  2689. (WLAN_FC_TYPE_MGMT << 2) |
  2690. (stypes[i] << 4),
  2691. NULL, 0) < 0) {
  2692. goto out_err;
  2693. }
  2694. }
  2695. if (nl80211_register_spurious_class3(bss))
  2696. goto out_err;
  2697. if (nl80211_get_wiphy_data_ap(bss) == NULL)
  2698. goto out_err;
  2699. return 0;
  2700. out_err:
  2701. eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
  2702. nl_destroy_handles(&bss->nl_mgmt);
  2703. return -1;
  2704. }
  2705. static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
  2706. {
  2707. if (nl80211_alloc_mgmt_handle(bss))
  2708. return -1;
  2709. wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
  2710. "handle %p (device SME)", bss->nl_mgmt);
  2711. if (nl80211_register_frame(bss, bss->nl_mgmt,
  2712. (WLAN_FC_TYPE_MGMT << 2) |
  2713. (WLAN_FC_STYPE_ACTION << 4),
  2714. NULL, 0) < 0)
  2715. goto out_err;
  2716. return 0;
  2717. out_err:
  2718. eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
  2719. nl_destroy_handles(&bss->nl_mgmt);
  2720. return -1;
  2721. }
  2722. static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
  2723. {
  2724. if (bss->nl_mgmt == NULL)
  2725. return;
  2726. wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
  2727. "(%s)", bss->nl_mgmt, reason);
  2728. eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
  2729. nl_destroy_handles(&bss->nl_mgmt);
  2730. nl80211_put_wiphy_data_ap(bss);
  2731. }
  2732. static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
  2733. {
  2734. wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
  2735. }
  2736. static int
  2737. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
  2738. {
  2739. struct i802_bss *bss = &drv->first_bss;
  2740. int send_rfkill_event = 0;
  2741. drv->ifindex = if_nametoindex(bss->ifname);
  2742. drv->first_bss.ifindex = drv->ifindex;
  2743. #ifndef HOSTAPD
  2744. /*
  2745. * Make sure the interface starts up in station mode unless this is a
  2746. * dynamically added interface (e.g., P2P) that was already configured
  2747. * with proper iftype.
  2748. */
  2749. if (drv->ifindex != drv->global->if_add_ifindex &&
  2750. wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION) < 0) {
  2751. wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to "
  2752. "use managed mode");
  2753. return -1;
  2754. }
  2755. if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
  2756. if (rfkill_is_blocked(drv->rfkill)) {
  2757. wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
  2758. "interface '%s' due to rfkill",
  2759. bss->ifname);
  2760. drv->if_disabled = 1;
  2761. send_rfkill_event = 1;
  2762. } else {
  2763. wpa_printf(MSG_ERROR, "nl80211: Could not set "
  2764. "interface '%s' UP", bss->ifname);
  2765. return -1;
  2766. }
  2767. }
  2768. netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
  2769. 1, IF_OPER_DORMANT);
  2770. #endif /* HOSTAPD */
  2771. if (wpa_driver_nl80211_capa(drv))
  2772. return -1;
  2773. if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  2774. bss->addr))
  2775. return -1;
  2776. if (send_rfkill_event) {
  2777. eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
  2778. drv, drv->ctx);
  2779. }
  2780. return 0;
  2781. }
  2782. static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
  2783. {
  2784. struct nl_msg *msg;
  2785. msg = nlmsg_alloc();
  2786. if (!msg)
  2787. return -ENOMEM;
  2788. nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
  2789. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2790. return send_and_recv_msgs(drv, msg, NULL, NULL);
  2791. nla_put_failure:
  2792. nlmsg_free(msg);
  2793. return -ENOBUFS;
  2794. }
  2795. /**
  2796. * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
  2797. * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
  2798. *
  2799. * Shut down driver interface and processing of driver events. Free
  2800. * private data buffer if one was allocated in wpa_driver_nl80211_init().
  2801. */
  2802. static void wpa_driver_nl80211_deinit(void *priv)
  2803. {
  2804. struct i802_bss *bss = priv;
  2805. struct wpa_driver_nl80211_data *drv = bss->drv;
  2806. bss->in_deinit = 1;
  2807. if (drv->data_tx_status)
  2808. eloop_unregister_read_sock(drv->eapol_tx_sock);
  2809. if (drv->eapol_tx_sock >= 0)
  2810. close(drv->eapol_tx_sock);
  2811. if (bss->nl_preq)
  2812. wpa_driver_nl80211_probe_req_report(bss, 0);
  2813. if (bss->added_if_into_bridge) {
  2814. if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
  2815. bss->ifname) < 0)
  2816. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  2817. "interface %s from bridge %s: %s",
  2818. bss->ifname, bss->brname, strerror(errno));
  2819. }
  2820. if (bss->added_bridge) {
  2821. if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
  2822. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  2823. "bridge %s: %s",
  2824. bss->brname, strerror(errno));
  2825. }
  2826. nl80211_remove_monitor_interface(drv);
  2827. if (is_ap_interface(drv->nlmode))
  2828. wpa_driver_nl80211_del_beacon(drv);
  2829. #ifdef HOSTAPD
  2830. if (drv->last_freq_ht) {
  2831. /* Clear HT flags from the driver */
  2832. struct hostapd_freq_params freq;
  2833. os_memset(&freq, 0, sizeof(freq));
  2834. freq.freq = drv->last_freq;
  2835. i802_set_freq(priv, &freq);
  2836. }
  2837. if (drv->eapol_sock >= 0) {
  2838. eloop_unregister_read_sock(drv->eapol_sock);
  2839. close(drv->eapol_sock);
  2840. }
  2841. if (drv->if_indices != drv->default_if_indices)
  2842. os_free(drv->if_indices);
  2843. #endif /* HOSTAPD */
  2844. if (drv->disabled_11b_rates)
  2845. nl80211_disable_11b_rates(drv, drv->ifindex, 0);
  2846. netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
  2847. IF_OPER_UP);
  2848. rfkill_deinit(drv->rfkill);
  2849. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
  2850. (void) linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
  2851. wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION);
  2852. nl80211_mgmt_unsubscribe(bss, "deinit");
  2853. nl_cb_put(drv->nl_cb);
  2854. nl80211_destroy_bss(&drv->first_bss);
  2855. os_free(drv->filter_ssids);
  2856. os_free(drv->auth_ie);
  2857. if (drv->in_interface_list)
  2858. dl_list_del(&drv->list);
  2859. os_free(drv);
  2860. }
  2861. /**
  2862. * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
  2863. * @eloop_ctx: Driver private data
  2864. * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
  2865. *
  2866. * This function can be used as registered timeout when starting a scan to
  2867. * generate a scan completed event if the driver does not report this.
  2868. */
  2869. static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  2870. {
  2871. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  2872. if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
  2873. wpa_driver_nl80211_set_mode(&drv->first_bss,
  2874. drv->ap_scan_as_station);
  2875. drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
  2876. }
  2877. wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
  2878. wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
  2879. }
  2880. static struct nl_msg *
  2881. nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
  2882. struct wpa_driver_scan_params *params)
  2883. {
  2884. struct nl_msg *msg;
  2885. int err;
  2886. size_t i;
  2887. msg = nlmsg_alloc();
  2888. if (!msg)
  2889. return NULL;
  2890. nl80211_cmd(drv, msg, 0, cmd);
  2891. if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) < 0)
  2892. goto fail;
  2893. if (params->num_ssids) {
  2894. struct nl_msg *ssids = nlmsg_alloc();
  2895. if (ssids == NULL)
  2896. goto fail;
  2897. for (i = 0; i < params->num_ssids; i++) {
  2898. wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
  2899. params->ssids[i].ssid,
  2900. params->ssids[i].ssid_len);
  2901. if (nla_put(ssids, i + 1, params->ssids[i].ssid_len,
  2902. params->ssids[i].ssid) < 0) {
  2903. nlmsg_free(ssids);
  2904. goto fail;
  2905. }
  2906. }
  2907. err = nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
  2908. nlmsg_free(ssids);
  2909. if (err < 0)
  2910. goto fail;
  2911. }
  2912. if (params->extra_ies) {
  2913. wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
  2914. params->extra_ies, params->extra_ies_len);
  2915. if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
  2916. params->extra_ies) < 0)
  2917. goto fail;
  2918. }
  2919. if (params->freqs) {
  2920. struct nl_msg *freqs = nlmsg_alloc();
  2921. if (freqs == NULL)
  2922. goto fail;
  2923. for (i = 0; params->freqs[i]; i++) {
  2924. wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
  2925. "MHz", params->freqs[i]);
  2926. if (nla_put_u32(freqs, i + 1, params->freqs[i]) < 0) {
  2927. nlmsg_free(freqs);
  2928. goto fail;
  2929. }
  2930. }
  2931. err = nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES,
  2932. freqs);
  2933. nlmsg_free(freqs);
  2934. if (err < 0)
  2935. goto fail;
  2936. }
  2937. os_free(drv->filter_ssids);
  2938. drv->filter_ssids = params->filter_ssids;
  2939. params->filter_ssids = NULL;
  2940. drv->num_filter_ssids = params->num_filter_ssids;
  2941. return msg;
  2942. fail:
  2943. nlmsg_free(msg);
  2944. return NULL;
  2945. }
  2946. /**
  2947. * wpa_driver_nl80211_scan - Request the driver to initiate scan
  2948. * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
  2949. * @params: Scan parameters
  2950. * Returns: 0 on success, -1 on failure
  2951. */
  2952. static int wpa_driver_nl80211_scan(void *priv,
  2953. struct wpa_driver_scan_params *params)
  2954. {
  2955. struct i802_bss *bss = priv;
  2956. struct wpa_driver_nl80211_data *drv = bss->drv;
  2957. int ret = -1, timeout;
  2958. struct nl_msg *msg, *rates = NULL;
  2959. drv->scan_for_auth = 0;
  2960. msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params);
  2961. if (!msg)
  2962. return -1;
  2963. if (params->p2p_probe) {
  2964. wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
  2965. rates = nlmsg_alloc();
  2966. if (rates == NULL)
  2967. goto nla_put_failure;
  2968. /*
  2969. * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
  2970. * by masking out everything else apart from the OFDM rates 6,
  2971. * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
  2972. * rates are left enabled.
  2973. */
  2974. NLA_PUT(rates, NL80211_BAND_2GHZ, 8,
  2975. "\x0c\x12\x18\x24\x30\x48\x60\x6c");
  2976. if (nla_put_nested(msg, NL80211_ATTR_SCAN_SUPP_RATES, rates) <
  2977. 0)
  2978. goto nla_put_failure;
  2979. NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
  2980. }
  2981. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2982. msg = NULL;
  2983. if (ret) {
  2984. wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
  2985. "(%s)", ret, strerror(-ret));
  2986. #ifdef HOSTAPD
  2987. if (is_ap_interface(drv->nlmode)) {
  2988. /*
  2989. * mac80211 does not allow scan requests in AP mode, so
  2990. * try to do this in station mode.
  2991. */
  2992. if (wpa_driver_nl80211_set_mode(
  2993. bss, NL80211_IFTYPE_STATION))
  2994. goto nla_put_failure;
  2995. if (wpa_driver_nl80211_scan(drv, params)) {
  2996. wpa_driver_nl80211_set_mode(bss, drv->nlmode);
  2997. goto nla_put_failure;
  2998. }
  2999. /* Restore AP mode when processing scan results */
  3000. drv->ap_scan_as_station = drv->nlmode;
  3001. ret = 0;
  3002. } else
  3003. goto nla_put_failure;
  3004. #else /* HOSTAPD */
  3005. goto nla_put_failure;
  3006. #endif /* HOSTAPD */
  3007. }
  3008. /* Not all drivers generate "scan completed" wireless event, so try to
  3009. * read results after a timeout. */
  3010. timeout = 10;
  3011. if (drv->scan_complete_events) {
  3012. /*
  3013. * The driver seems to deliver events to notify when scan is
  3014. * complete, so use longer timeout to avoid race conditions
  3015. * with scanning and following association request.
  3016. */
  3017. timeout = 30;
  3018. }
  3019. wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
  3020. "seconds", ret, timeout);
  3021. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
  3022. eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
  3023. drv, drv->ctx);
  3024. nla_put_failure:
  3025. nlmsg_free(msg);
  3026. nlmsg_free(rates);
  3027. return ret;
  3028. }
  3029. /**
  3030. * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
  3031. * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
  3032. * @params: Scan parameters
  3033. * @interval: Interval between scan cycles in milliseconds
  3034. * Returns: 0 on success, -1 on failure or if not supported
  3035. */
  3036. static int wpa_driver_nl80211_sched_scan(void *priv,
  3037. struct wpa_driver_scan_params *params,
  3038. u32 interval)
  3039. {
  3040. struct i802_bss *bss = priv;
  3041. struct wpa_driver_nl80211_data *drv = bss->drv;
  3042. int ret = -1;
  3043. struct nl_msg *msg;
  3044. struct nl_msg *match_set_ssid = NULL, *match_sets = NULL;
  3045. struct nl_msg *match_set_rssi = NULL;
  3046. size_t i;
  3047. #ifdef ANDROID
  3048. if (!drv->capa.sched_scan_supported)
  3049. return android_pno_start(bss, params);
  3050. #endif /* ANDROID */
  3051. msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params);
  3052. if (!msg)
  3053. goto nla_put_failure;
  3054. NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
  3055. if ((drv->num_filter_ssids &&
  3056. (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
  3057. params->filter_rssi) {
  3058. match_sets = nlmsg_alloc();
  3059. if (match_sets == NULL)
  3060. goto nla_put_failure;
  3061. for (i = 0; i < drv->num_filter_ssids; i++) {
  3062. wpa_hexdump_ascii(MSG_MSGDUMP,
  3063. "nl80211: Sched scan filter SSID",
  3064. drv->filter_ssids[i].ssid,
  3065. drv->filter_ssids[i].ssid_len);
  3066. match_set_ssid = nlmsg_alloc();
  3067. if (match_set_ssid == NULL)
  3068. goto nla_put_failure;
  3069. NLA_PUT(match_set_ssid,
  3070. NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
  3071. drv->filter_ssids[i].ssid_len,
  3072. drv->filter_ssids[i].ssid);
  3073. if (nla_put_nested(match_sets, i + 1, match_set_ssid) <
  3074. 0)
  3075. goto nla_put_failure;
  3076. }
  3077. if (params->filter_rssi) {
  3078. match_set_rssi = nlmsg_alloc();
  3079. if (match_set_rssi == NULL)
  3080. goto nla_put_failure;
  3081. NLA_PUT_U32(match_set_rssi,
  3082. NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
  3083. params->filter_rssi);
  3084. wpa_printf(MSG_MSGDUMP,
  3085. "nl80211: Sched scan RSSI filter %d dBm",
  3086. params->filter_rssi);
  3087. if (nla_put_nested(match_sets, 0, match_set_rssi) < 0)
  3088. goto nla_put_failure;
  3089. }
  3090. if (nla_put_nested(msg, NL80211_ATTR_SCHED_SCAN_MATCH,
  3091. match_sets) < 0)
  3092. goto nla_put_failure;
  3093. }
  3094. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3095. /* TODO: if we get an error here, we should fall back to normal scan */
  3096. msg = NULL;
  3097. if (ret) {
  3098. wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
  3099. "ret=%d (%s)", ret, strerror(-ret));
  3100. goto nla_put_failure;
  3101. }
  3102. wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
  3103. "scan interval %d msec", ret, interval);
  3104. nla_put_failure:
  3105. nlmsg_free(match_set_ssid);
  3106. nlmsg_free(match_sets);
  3107. nlmsg_free(match_set_rssi);
  3108. nlmsg_free(msg);
  3109. return ret;
  3110. }
  3111. /**
  3112. * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
  3113. * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
  3114. * Returns: 0 on success, -1 on failure or if not supported
  3115. */
  3116. static int wpa_driver_nl80211_stop_sched_scan(void *priv)
  3117. {
  3118. struct i802_bss *bss = priv;
  3119. struct wpa_driver_nl80211_data *drv = bss->drv;
  3120. int ret = 0;
  3121. struct nl_msg *msg;
  3122. #ifdef ANDROID
  3123. if (!drv->capa.sched_scan_supported)
  3124. return android_pno_stop(bss);
  3125. #endif /* ANDROID */
  3126. msg = nlmsg_alloc();
  3127. if (!msg)
  3128. return -1;
  3129. nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
  3130. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3131. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3132. msg = NULL;
  3133. if (ret) {
  3134. wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
  3135. "ret=%d (%s)", ret, strerror(-ret));
  3136. goto nla_put_failure;
  3137. }
  3138. wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
  3139. nla_put_failure:
  3140. nlmsg_free(msg);
  3141. return ret;
  3142. }
  3143. static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
  3144. {
  3145. const u8 *end, *pos;
  3146. if (ies == NULL)
  3147. return NULL;
  3148. pos = ies;
  3149. end = ies + ies_len;
  3150. while (pos + 1 < end) {
  3151. if (pos + 2 + pos[1] > end)
  3152. break;
  3153. if (pos[0] == ie)
  3154. return pos;
  3155. pos += 2 + pos[1];
  3156. }
  3157. return NULL;
  3158. }
  3159. static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
  3160. const u8 *ie, size_t ie_len)
  3161. {
  3162. const u8 *ssid;
  3163. size_t i;
  3164. if (drv->filter_ssids == NULL)
  3165. return 0;
  3166. ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
  3167. if (ssid == NULL)
  3168. return 1;
  3169. for (i = 0; i < drv->num_filter_ssids; i++) {
  3170. if (ssid[1] == drv->filter_ssids[i].ssid_len &&
  3171. os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
  3172. 0)
  3173. return 0;
  3174. }
  3175. return 1;
  3176. }
  3177. static int bss_info_handler(struct nl_msg *msg, void *arg)
  3178. {
  3179. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  3180. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  3181. struct nlattr *bss[NL80211_BSS_MAX + 1];
  3182. static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
  3183. [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
  3184. [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
  3185. [NL80211_BSS_TSF] = { .type = NLA_U64 },
  3186. [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
  3187. [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
  3188. [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
  3189. [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
  3190. [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
  3191. [NL80211_BSS_STATUS] = { .type = NLA_U32 },
  3192. [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
  3193. [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
  3194. };
  3195. struct nl80211_bss_info_arg *_arg = arg;
  3196. struct wpa_scan_results *res = _arg->res;
  3197. struct wpa_scan_res **tmp;
  3198. struct wpa_scan_res *r;
  3199. const u8 *ie, *beacon_ie;
  3200. size_t ie_len, beacon_ie_len;
  3201. u8 *pos;
  3202. size_t i;
  3203. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  3204. genlmsg_attrlen(gnlh, 0), NULL);
  3205. if (!tb[NL80211_ATTR_BSS])
  3206. return NL_SKIP;
  3207. if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
  3208. bss_policy))
  3209. return NL_SKIP;
  3210. if (bss[NL80211_BSS_STATUS]) {
  3211. enum nl80211_bss_status status;
  3212. status = nla_get_u32(bss[NL80211_BSS_STATUS]);
  3213. if (status == NL80211_BSS_STATUS_ASSOCIATED &&
  3214. bss[NL80211_BSS_FREQUENCY]) {
  3215. _arg->assoc_freq =
  3216. nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
  3217. wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
  3218. _arg->assoc_freq);
  3219. }
  3220. if (status == NL80211_BSS_STATUS_ASSOCIATED &&
  3221. bss[NL80211_BSS_BSSID]) {
  3222. os_memcpy(_arg->assoc_bssid,
  3223. nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
  3224. wpa_printf(MSG_DEBUG, "nl80211: Associated with "
  3225. MACSTR, MAC2STR(_arg->assoc_bssid));
  3226. }
  3227. }
  3228. if (!res)
  3229. return NL_SKIP;
  3230. if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
  3231. ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  3232. ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  3233. } else {
  3234. ie = NULL;
  3235. ie_len = 0;
  3236. }
  3237. if (bss[NL80211_BSS_BEACON_IES]) {
  3238. beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
  3239. beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
  3240. } else {
  3241. beacon_ie = NULL;
  3242. beacon_ie_len = 0;
  3243. }
  3244. if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
  3245. ie ? ie_len : beacon_ie_len))
  3246. return NL_SKIP;
  3247. r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
  3248. if (r == NULL)
  3249. return NL_SKIP;
  3250. if (bss[NL80211_BSS_BSSID])
  3251. os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
  3252. ETH_ALEN);
  3253. if (bss[NL80211_BSS_FREQUENCY])
  3254. r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
  3255. if (bss[NL80211_BSS_BEACON_INTERVAL])
  3256. r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
  3257. if (bss[NL80211_BSS_CAPABILITY])
  3258. r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
  3259. r->flags |= WPA_SCAN_NOISE_INVALID;
  3260. if (bss[NL80211_BSS_SIGNAL_MBM]) {
  3261. r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
  3262. r->level /= 100; /* mBm to dBm */
  3263. r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
  3264. } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
  3265. r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
  3266. r->flags |= WPA_SCAN_QUAL_INVALID;
  3267. } else
  3268. r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
  3269. if (bss[NL80211_BSS_TSF])
  3270. r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
  3271. if (bss[NL80211_BSS_SEEN_MS_AGO])
  3272. r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
  3273. r->ie_len = ie_len;
  3274. pos = (u8 *) (r + 1);
  3275. if (ie) {
  3276. os_memcpy(pos, ie, ie_len);
  3277. pos += ie_len;
  3278. }
  3279. r->beacon_ie_len = beacon_ie_len;
  3280. if (beacon_ie)
  3281. os_memcpy(pos, beacon_ie, beacon_ie_len);
  3282. if (bss[NL80211_BSS_STATUS]) {
  3283. enum nl80211_bss_status status;
  3284. status = nla_get_u32(bss[NL80211_BSS_STATUS]);
  3285. switch (status) {
  3286. case NL80211_BSS_STATUS_AUTHENTICATED:
  3287. r->flags |= WPA_SCAN_AUTHENTICATED;
  3288. break;
  3289. case NL80211_BSS_STATUS_ASSOCIATED:
  3290. r->flags |= WPA_SCAN_ASSOCIATED;
  3291. break;
  3292. default:
  3293. break;
  3294. }
  3295. }
  3296. /*
  3297. * cfg80211 maintains separate BSS table entries for APs if the same
  3298. * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
  3299. * not use frequency as a separate key in the BSS table, so filter out
  3300. * duplicated entries. Prefer associated BSS entry in such a case in
  3301. * order to get the correct frequency into the BSS table.
  3302. */
  3303. for (i = 0; i < res->num; i++) {
  3304. const u8 *s1, *s2;
  3305. if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
  3306. continue;
  3307. s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
  3308. res->res[i]->ie_len, WLAN_EID_SSID);
  3309. s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
  3310. if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
  3311. os_memcmp(s1, s2, 2 + s1[1]) != 0)
  3312. continue;
  3313. /* Same BSSID,SSID was already included in scan results */
  3314. wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
  3315. "for " MACSTR, MAC2STR(r->bssid));
  3316. if ((r->flags & WPA_SCAN_ASSOCIATED) &&
  3317. !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
  3318. os_free(res->res[i]);
  3319. res->res[i] = r;
  3320. } else
  3321. os_free(r);
  3322. return NL_SKIP;
  3323. }
  3324. tmp = os_realloc_array(res->res, res->num + 1,
  3325. sizeof(struct wpa_scan_res *));
  3326. if (tmp == NULL) {
  3327. os_free(r);
  3328. return NL_SKIP;
  3329. }
  3330. tmp[res->num++] = r;
  3331. res->res = tmp;
  3332. return NL_SKIP;
  3333. }
  3334. static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
  3335. const u8 *addr)
  3336. {
  3337. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  3338. wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
  3339. "mismatch (" MACSTR ")", MAC2STR(addr));
  3340. wpa_driver_nl80211_mlme(drv, addr,
  3341. NL80211_CMD_DEAUTHENTICATE,
  3342. WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
  3343. }
  3344. }
  3345. static void wpa_driver_nl80211_check_bss_status(
  3346. struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
  3347. {
  3348. size_t i;
  3349. for (i = 0; i < res->num; i++) {
  3350. struct wpa_scan_res *r = res->res[i];
  3351. if (r->flags & WPA_SCAN_AUTHENTICATED) {
  3352. wpa_printf(MSG_DEBUG, "nl80211: Scan results "
  3353. "indicates BSS status with " MACSTR
  3354. " as authenticated",
  3355. MAC2STR(r->bssid));
  3356. if (is_sta_interface(drv->nlmode) &&
  3357. os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
  3358. os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
  3359. 0) {
  3360. wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
  3361. " in local state (auth=" MACSTR
  3362. " assoc=" MACSTR ")",
  3363. MAC2STR(drv->auth_bssid),
  3364. MAC2STR(drv->bssid));
  3365. clear_state_mismatch(drv, r->bssid);
  3366. }
  3367. }
  3368. if (r->flags & WPA_SCAN_ASSOCIATED) {
  3369. wpa_printf(MSG_DEBUG, "nl80211: Scan results "
  3370. "indicate BSS status with " MACSTR
  3371. " as associated",
  3372. MAC2STR(r->bssid));
  3373. if (is_sta_interface(drv->nlmode) &&
  3374. !drv->associated) {
  3375. wpa_printf(MSG_DEBUG, "nl80211: Local state "
  3376. "(not associated) does not match "
  3377. "with BSS state");
  3378. clear_state_mismatch(drv, r->bssid);
  3379. } else if (is_sta_interface(drv->nlmode) &&
  3380. os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
  3381. 0) {
  3382. wpa_printf(MSG_DEBUG, "nl80211: Local state "
  3383. "(associated with " MACSTR ") does "
  3384. "not match with BSS state",
  3385. MAC2STR(drv->bssid));
  3386. clear_state_mismatch(drv, r->bssid);
  3387. clear_state_mismatch(drv, drv->bssid);
  3388. }
  3389. }
  3390. }
  3391. }
  3392. static struct wpa_scan_results *
  3393. nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
  3394. {
  3395. struct nl_msg *msg;
  3396. struct wpa_scan_results *res;
  3397. int ret;
  3398. struct nl80211_bss_info_arg arg;
  3399. res = os_zalloc(sizeof(*res));
  3400. if (res == NULL)
  3401. return NULL;
  3402. msg = nlmsg_alloc();
  3403. if (!msg)
  3404. goto nla_put_failure;
  3405. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
  3406. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3407. arg.drv = drv;
  3408. arg.res = res;
  3409. ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
  3410. msg = NULL;
  3411. if (ret == 0) {
  3412. wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
  3413. "BSSes)", (unsigned long) res->num);
  3414. nl80211_get_noise_for_scan_results(drv, res);
  3415. return res;
  3416. }
  3417. wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
  3418. "(%s)", ret, strerror(-ret));
  3419. nla_put_failure:
  3420. nlmsg_free(msg);
  3421. wpa_scan_results_free(res);
  3422. return NULL;
  3423. }
  3424. /**
  3425. * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
  3426. * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
  3427. * Returns: Scan results on success, -1 on failure
  3428. */
  3429. static struct wpa_scan_results *
  3430. wpa_driver_nl80211_get_scan_results(void *priv)
  3431. {
  3432. struct i802_bss *bss = priv;
  3433. struct wpa_driver_nl80211_data *drv = bss->drv;
  3434. struct wpa_scan_results *res;
  3435. res = nl80211_get_scan_results(drv);
  3436. if (res)
  3437. wpa_driver_nl80211_check_bss_status(drv, res);
  3438. return res;
  3439. }
  3440. static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
  3441. {
  3442. struct wpa_scan_results *res;
  3443. size_t i;
  3444. res = nl80211_get_scan_results(drv);
  3445. if (res == NULL) {
  3446. wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
  3447. return;
  3448. }
  3449. wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
  3450. for (i = 0; i < res->num; i++) {
  3451. struct wpa_scan_res *r = res->res[i];
  3452. wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
  3453. (int) i, (int) res->num, MAC2STR(r->bssid),
  3454. r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
  3455. r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
  3456. }
  3457. wpa_scan_results_free(res);
  3458. }
  3459. static int wpa_driver_nl80211_set_key(const char *ifname, void *priv,
  3460. enum wpa_alg alg, const u8 *addr,
  3461. int key_idx, int set_tx,
  3462. const u8 *seq, size_t seq_len,
  3463. const u8 *key, size_t key_len)
  3464. {
  3465. struct i802_bss *bss = priv;
  3466. struct wpa_driver_nl80211_data *drv = bss->drv;
  3467. int ifindex = if_nametoindex(ifname);
  3468. struct nl_msg *msg;
  3469. int ret;
  3470. wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
  3471. "set_tx=%d seq_len=%lu key_len=%lu",
  3472. __func__, ifindex, alg, addr, key_idx, set_tx,
  3473. (unsigned long) seq_len, (unsigned long) key_len);
  3474. #ifdef CONFIG_TDLS
  3475. if (key_idx == -1)
  3476. key_idx = 0;
  3477. #endif /* CONFIG_TDLS */
  3478. msg = nlmsg_alloc();
  3479. if (!msg)
  3480. return -ENOMEM;
  3481. if (alg == WPA_ALG_NONE) {
  3482. nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
  3483. } else {
  3484. nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
  3485. NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
  3486. switch (alg) {
  3487. case WPA_ALG_WEP:
  3488. if (key_len == 5)
  3489. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  3490. WLAN_CIPHER_SUITE_WEP40);
  3491. else
  3492. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  3493. WLAN_CIPHER_SUITE_WEP104);
  3494. break;
  3495. case WPA_ALG_TKIP:
  3496. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  3497. WLAN_CIPHER_SUITE_TKIP);
  3498. break;
  3499. case WPA_ALG_CCMP:
  3500. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  3501. WLAN_CIPHER_SUITE_CCMP);
  3502. break;
  3503. case WPA_ALG_GCMP:
  3504. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  3505. WLAN_CIPHER_SUITE_GCMP);
  3506. break;
  3507. case WPA_ALG_IGTK:
  3508. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  3509. WLAN_CIPHER_SUITE_AES_CMAC);
  3510. break;
  3511. default:
  3512. wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
  3513. "algorithm %d", __func__, alg);
  3514. nlmsg_free(msg);
  3515. return -1;
  3516. }
  3517. }
  3518. if (seq && seq_len)
  3519. NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
  3520. if (addr && !is_broadcast_ether_addr(addr)) {
  3521. wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
  3522. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  3523. if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
  3524. wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
  3525. NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
  3526. NL80211_KEYTYPE_GROUP);
  3527. }
  3528. } else if (addr && is_broadcast_ether_addr(addr)) {
  3529. struct nl_msg *types;
  3530. int err;
  3531. wpa_printf(MSG_DEBUG, " broadcast key");
  3532. types = nlmsg_alloc();
  3533. if (!types)
  3534. goto nla_put_failure;
  3535. NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
  3536. err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
  3537. types);
  3538. nlmsg_free(types);
  3539. if (err)
  3540. goto nla_put_failure;
  3541. }
  3542. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
  3543. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  3544. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3545. if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
  3546. ret = 0;
  3547. if (ret)
  3548. wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
  3549. ret, strerror(-ret));
  3550. /*
  3551. * If we failed or don't need to set the default TX key (below),
  3552. * we're done here.
  3553. */
  3554. if (ret || !set_tx || alg == WPA_ALG_NONE)
  3555. return ret;
  3556. if (is_ap_interface(drv->nlmode) && addr &&
  3557. !is_broadcast_ether_addr(addr))
  3558. return ret;
  3559. msg = nlmsg_alloc();
  3560. if (!msg)
  3561. return -ENOMEM;
  3562. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
  3563. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
  3564. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  3565. if (alg == WPA_ALG_IGTK)
  3566. NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
  3567. else
  3568. NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
  3569. if (addr && is_broadcast_ether_addr(addr)) {
  3570. struct nl_msg *types;
  3571. int err;
  3572. types = nlmsg_alloc();
  3573. if (!types)
  3574. goto nla_put_failure;
  3575. NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
  3576. err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
  3577. types);
  3578. nlmsg_free(types);
  3579. if (err)
  3580. goto nla_put_failure;
  3581. } else if (addr) {
  3582. struct nl_msg *types;
  3583. int err;
  3584. types = nlmsg_alloc();
  3585. if (!types)
  3586. goto nla_put_failure;
  3587. NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_UNICAST);
  3588. err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
  3589. types);
  3590. nlmsg_free(types);
  3591. if (err)
  3592. goto nla_put_failure;
  3593. }
  3594. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3595. if (ret == -ENOENT)
  3596. ret = 0;
  3597. if (ret)
  3598. wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
  3599. "err=%d %s)", ret, strerror(-ret));
  3600. return ret;
  3601. nla_put_failure:
  3602. nlmsg_free(msg);
  3603. return -ENOBUFS;
  3604. }
  3605. static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
  3606. int key_idx, int defkey,
  3607. const u8 *seq, size_t seq_len,
  3608. const u8 *key, size_t key_len)
  3609. {
  3610. struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
  3611. if (!key_attr)
  3612. return -1;
  3613. if (defkey && alg == WPA_ALG_IGTK)
  3614. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
  3615. else if (defkey)
  3616. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
  3617. NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
  3618. switch (alg) {
  3619. case WPA_ALG_WEP:
  3620. if (key_len == 5)
  3621. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  3622. WLAN_CIPHER_SUITE_WEP40);
  3623. else
  3624. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  3625. WLAN_CIPHER_SUITE_WEP104);
  3626. break;
  3627. case WPA_ALG_TKIP:
  3628. NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
  3629. break;
  3630. case WPA_ALG_CCMP:
  3631. NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
  3632. break;
  3633. case WPA_ALG_GCMP:
  3634. NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_GCMP);
  3635. break;
  3636. case WPA_ALG_IGTK:
  3637. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  3638. WLAN_CIPHER_SUITE_AES_CMAC);
  3639. break;
  3640. default:
  3641. wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
  3642. "algorithm %d", __func__, alg);
  3643. return -1;
  3644. }
  3645. if (seq && seq_len)
  3646. NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
  3647. NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
  3648. nla_nest_end(msg, key_attr);
  3649. return 0;
  3650. nla_put_failure:
  3651. return -1;
  3652. }
  3653. static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
  3654. struct nl_msg *msg)
  3655. {
  3656. int i, privacy = 0;
  3657. struct nlattr *nl_keys, *nl_key;
  3658. for (i = 0; i < 4; i++) {
  3659. if (!params->wep_key[i])
  3660. continue;
  3661. privacy = 1;
  3662. break;
  3663. }
  3664. if (params->wps == WPS_MODE_PRIVACY)
  3665. privacy = 1;
  3666. if (params->pairwise_suite &&
  3667. params->pairwise_suite != WPA_CIPHER_NONE)
  3668. privacy = 1;
  3669. if (!privacy)
  3670. return 0;
  3671. NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
  3672. nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
  3673. if (!nl_keys)
  3674. goto nla_put_failure;
  3675. for (i = 0; i < 4; i++) {
  3676. if (!params->wep_key[i])
  3677. continue;
  3678. nl_key = nla_nest_start(msg, i);
  3679. if (!nl_key)
  3680. goto nla_put_failure;
  3681. NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
  3682. params->wep_key[i]);
  3683. if (params->wep_key_len[i] == 5)
  3684. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  3685. WLAN_CIPHER_SUITE_WEP40);
  3686. else
  3687. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  3688. WLAN_CIPHER_SUITE_WEP104);
  3689. NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
  3690. if (i == params->wep_tx_keyidx)
  3691. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
  3692. nla_nest_end(msg, nl_key);
  3693. }
  3694. nla_nest_end(msg, nl_keys);
  3695. return 0;
  3696. nla_put_failure:
  3697. return -ENOBUFS;
  3698. }
  3699. static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
  3700. const u8 *addr, int cmd, u16 reason_code,
  3701. int local_state_change)
  3702. {
  3703. int ret = -1;
  3704. struct nl_msg *msg;
  3705. msg = nlmsg_alloc();
  3706. if (!msg)
  3707. return -1;
  3708. nl80211_cmd(drv, msg, 0, cmd);
  3709. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3710. NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
  3711. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  3712. if (local_state_change)
  3713. NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
  3714. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3715. msg = NULL;
  3716. if (ret) {
  3717. wpa_dbg(drv->ctx, MSG_DEBUG,
  3718. "nl80211: MLME command failed: reason=%u ret=%d (%s)",
  3719. reason_code, ret, strerror(-ret));
  3720. goto nla_put_failure;
  3721. }
  3722. ret = 0;
  3723. nla_put_failure:
  3724. nlmsg_free(msg);
  3725. return ret;
  3726. }
  3727. static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
  3728. const u8 *addr, int reason_code)
  3729. {
  3730. wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
  3731. __func__, MAC2STR(addr), reason_code);
  3732. drv->associated = 0;
  3733. return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISCONNECT,
  3734. reason_code, 0);
  3735. }
  3736. static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
  3737. int reason_code)
  3738. {
  3739. struct i802_bss *bss = priv;
  3740. struct wpa_driver_nl80211_data *drv = bss->drv;
  3741. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
  3742. return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
  3743. wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
  3744. __func__, MAC2STR(addr), reason_code);
  3745. drv->associated = 0;
  3746. if (drv->nlmode == NL80211_IFTYPE_ADHOC)
  3747. return nl80211_leave_ibss(drv);
  3748. return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
  3749. reason_code, 0);
  3750. }
  3751. static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
  3752. int reason_code)
  3753. {
  3754. struct i802_bss *bss = priv;
  3755. struct wpa_driver_nl80211_data *drv = bss->drv;
  3756. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
  3757. return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
  3758. wpa_printf(MSG_DEBUG, "%s", __func__);
  3759. drv->associated = 0;
  3760. return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE,
  3761. reason_code, 0);
  3762. }
  3763. static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
  3764. struct wpa_driver_auth_params *params)
  3765. {
  3766. int i;
  3767. drv->auth_freq = params->freq;
  3768. drv->auth_alg = params->auth_alg;
  3769. drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
  3770. drv->auth_local_state_change = params->local_state_change;
  3771. drv->auth_p2p = params->p2p;
  3772. if (params->bssid)
  3773. os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
  3774. else
  3775. os_memset(drv->auth_bssid_, 0, ETH_ALEN);
  3776. if (params->ssid) {
  3777. os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
  3778. drv->auth_ssid_len = params->ssid_len;
  3779. } else
  3780. drv->auth_ssid_len = 0;
  3781. os_free(drv->auth_ie);
  3782. drv->auth_ie = NULL;
  3783. drv->auth_ie_len = 0;
  3784. if (params->ie) {
  3785. drv->auth_ie = os_malloc(params->ie_len);
  3786. if (drv->auth_ie) {
  3787. os_memcpy(drv->auth_ie, params->ie, params->ie_len);
  3788. drv->auth_ie_len = params->ie_len;
  3789. }
  3790. }
  3791. for (i = 0; i < 4; i++) {
  3792. if (params->wep_key[i] && params->wep_key_len[i] &&
  3793. params->wep_key_len[i] <= 16) {
  3794. os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
  3795. params->wep_key_len[i]);
  3796. drv->auth_wep_key_len[i] = params->wep_key_len[i];
  3797. } else
  3798. drv->auth_wep_key_len[i] = 0;
  3799. }
  3800. }
  3801. static int wpa_driver_nl80211_authenticate(
  3802. void *priv, struct wpa_driver_auth_params *params)
  3803. {
  3804. struct i802_bss *bss = priv;
  3805. struct wpa_driver_nl80211_data *drv = bss->drv;
  3806. int ret = -1, i;
  3807. struct nl_msg *msg;
  3808. enum nl80211_auth_type type;
  3809. enum nl80211_iftype nlmode;
  3810. int count = 0;
  3811. int is_retry;
  3812. is_retry = drv->retry_auth;
  3813. drv->retry_auth = 0;
  3814. drv->associated = 0;
  3815. os_memset(drv->auth_bssid, 0, ETH_ALEN);
  3816. /* FIX: IBSS mode */
  3817. nlmode = params->p2p ?
  3818. NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
  3819. if (drv->nlmode != nlmode &&
  3820. wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
  3821. return -1;
  3822. retry:
  3823. msg = nlmsg_alloc();
  3824. if (!msg)
  3825. return -1;
  3826. wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
  3827. drv->ifindex);
  3828. nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
  3829. for (i = 0; i < 4; i++) {
  3830. if (!params->wep_key[i])
  3831. continue;
  3832. wpa_driver_nl80211_set_key(bss->ifname, priv, WPA_ALG_WEP,
  3833. NULL, i,
  3834. i == params->wep_tx_keyidx, NULL, 0,
  3835. params->wep_key[i],
  3836. params->wep_key_len[i]);
  3837. if (params->wep_tx_keyidx != i)
  3838. continue;
  3839. if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
  3840. params->wep_key[i], params->wep_key_len[i])) {
  3841. nlmsg_free(msg);
  3842. return -1;
  3843. }
  3844. }
  3845. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3846. if (params->bssid) {
  3847. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  3848. MAC2STR(params->bssid));
  3849. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  3850. }
  3851. if (params->freq) {
  3852. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  3853. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  3854. }
  3855. if (params->ssid) {
  3856. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  3857. params->ssid, params->ssid_len);
  3858. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  3859. params->ssid);
  3860. }
  3861. wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
  3862. if (params->ie)
  3863. NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
  3864. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  3865. type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  3866. else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  3867. type = NL80211_AUTHTYPE_SHARED_KEY;
  3868. else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  3869. type = NL80211_AUTHTYPE_NETWORK_EAP;
  3870. else if (params->auth_alg & WPA_AUTH_ALG_FT)
  3871. type = NL80211_AUTHTYPE_FT;
  3872. else
  3873. goto nla_put_failure;
  3874. wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
  3875. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
  3876. if (params->local_state_change) {
  3877. wpa_printf(MSG_DEBUG, " * Local state change only");
  3878. NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
  3879. }
  3880. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3881. msg = NULL;
  3882. if (ret) {
  3883. wpa_dbg(drv->ctx, MSG_DEBUG,
  3884. "nl80211: MLME command failed (auth): ret=%d (%s)",
  3885. ret, strerror(-ret));
  3886. count++;
  3887. if (ret == -EALREADY && count == 1 && params->bssid &&
  3888. !params->local_state_change) {
  3889. /*
  3890. * mac80211 does not currently accept new
  3891. * authentication if we are already authenticated. As a
  3892. * workaround, force deauthentication and try again.
  3893. */
  3894. wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
  3895. "after forced deauthentication");
  3896. wpa_driver_nl80211_deauthenticate(
  3897. bss, params->bssid,
  3898. WLAN_REASON_PREV_AUTH_NOT_VALID);
  3899. nlmsg_free(msg);
  3900. goto retry;
  3901. }
  3902. if (ret == -ENOENT && params->freq && !is_retry) {
  3903. /*
  3904. * cfg80211 has likely expired the BSS entry even
  3905. * though it was previously available in our internal
  3906. * BSS table. To recover quickly, start a single
  3907. * channel scan on the specified channel.
  3908. */
  3909. struct wpa_driver_scan_params scan;
  3910. int freqs[2];
  3911. os_memset(&scan, 0, sizeof(scan));
  3912. scan.num_ssids = 1;
  3913. if (params->ssid) {
  3914. scan.ssids[0].ssid = params->ssid;
  3915. scan.ssids[0].ssid_len = params->ssid_len;
  3916. }
  3917. freqs[0] = params->freq;
  3918. freqs[1] = 0;
  3919. scan.freqs = freqs;
  3920. wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
  3921. "channel scan to refresh cfg80211 BSS "
  3922. "entry");
  3923. ret = wpa_driver_nl80211_scan(bss, &scan);
  3924. if (ret == 0) {
  3925. nl80211_copy_auth_params(drv, params);
  3926. drv->scan_for_auth = 1;
  3927. }
  3928. } else if (is_retry) {
  3929. /*
  3930. * Need to indicate this with an event since the return
  3931. * value from the retry is not delivered to core code.
  3932. */
  3933. union wpa_event_data event;
  3934. wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
  3935. "failed");
  3936. os_memset(&event, 0, sizeof(event));
  3937. os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
  3938. ETH_ALEN);
  3939. wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
  3940. &event);
  3941. }
  3942. goto nla_put_failure;
  3943. }
  3944. ret = 0;
  3945. wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
  3946. "successfully");
  3947. nla_put_failure:
  3948. nlmsg_free(msg);
  3949. return ret;
  3950. }
  3951. static int wpa_driver_nl80211_authenticate_retry(
  3952. struct wpa_driver_nl80211_data *drv)
  3953. {
  3954. struct wpa_driver_auth_params params;
  3955. struct i802_bss *bss = &drv->first_bss;
  3956. int i;
  3957. wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
  3958. os_memset(&params, 0, sizeof(params));
  3959. params.freq = drv->auth_freq;
  3960. params.auth_alg = drv->auth_alg;
  3961. params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
  3962. params.local_state_change = drv->auth_local_state_change;
  3963. params.p2p = drv->auth_p2p;
  3964. if (!is_zero_ether_addr(drv->auth_bssid_))
  3965. params.bssid = drv->auth_bssid_;
  3966. if (drv->auth_ssid_len) {
  3967. params.ssid = drv->auth_ssid;
  3968. params.ssid_len = drv->auth_ssid_len;
  3969. }
  3970. params.ie = drv->auth_ie;
  3971. params.ie_len = drv->auth_ie_len;
  3972. for (i = 0; i < 4; i++) {
  3973. if (drv->auth_wep_key_len[i]) {
  3974. params.wep_key[i] = drv->auth_wep_key[i];
  3975. params.wep_key_len[i] = drv->auth_wep_key_len[i];
  3976. }
  3977. }
  3978. drv->retry_auth = 1;
  3979. return wpa_driver_nl80211_authenticate(bss, &params);
  3980. }
  3981. struct phy_info_arg {
  3982. u16 *num_modes;
  3983. struct hostapd_hw_modes *modes;
  3984. };
  3985. static int phy_info_handler(struct nl_msg *msg, void *arg)
  3986. {
  3987. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  3988. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  3989. struct phy_info_arg *phy_info = arg;
  3990. struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
  3991. struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
  3992. static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
  3993. [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
  3994. [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
  3995. [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
  3996. [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
  3997. [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
  3998. [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
  3999. };
  4000. struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
  4001. static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
  4002. [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
  4003. [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
  4004. };
  4005. struct nlattr *nl_band;
  4006. struct nlattr *nl_freq;
  4007. struct nlattr *nl_rate;
  4008. int rem_band, rem_freq, rem_rate;
  4009. struct hostapd_hw_modes *mode;
  4010. int idx, mode_is_set;
  4011. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  4012. genlmsg_attrlen(gnlh, 0), NULL);
  4013. if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
  4014. return NL_SKIP;
  4015. nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
  4016. mode = os_realloc_array(phy_info->modes,
  4017. *phy_info->num_modes + 1,
  4018. sizeof(*mode));
  4019. if (!mode)
  4020. return NL_SKIP;
  4021. phy_info->modes = mode;
  4022. mode_is_set = 0;
  4023. mode = &phy_info->modes[*(phy_info->num_modes)];
  4024. memset(mode, 0, sizeof(*mode));
  4025. mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN;
  4026. *(phy_info->num_modes) += 1;
  4027. nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
  4028. nla_len(nl_band), NULL);
  4029. if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
  4030. mode->ht_capab = nla_get_u16(
  4031. tb_band[NL80211_BAND_ATTR_HT_CAPA]);
  4032. }
  4033. if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) {
  4034. mode->a_mpdu_params |= nla_get_u8(
  4035. tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) &
  4036. 0x03;
  4037. }
  4038. if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) {
  4039. mode->a_mpdu_params |= nla_get_u8(
  4040. tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) <<
  4041. 2;
  4042. }
  4043. if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] &&
  4044. nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) {
  4045. u8 *mcs;
  4046. mcs = nla_data(tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
  4047. os_memcpy(mode->mcs_set, mcs, 16);
  4048. }
  4049. if (tb_band[NL80211_BAND_ATTR_VHT_CAPA]) {
  4050. mode->vht_capab = nla_get_u32(
  4051. tb_band[NL80211_BAND_ATTR_VHT_CAPA]);
  4052. }
  4053. if (tb_band[NL80211_BAND_ATTR_VHT_MCS_SET] &&
  4054. nla_len(tb_band[NL80211_BAND_ATTR_VHT_MCS_SET])) {
  4055. u8 *mcs;
  4056. mcs = nla_data(tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
  4057. os_memcpy(mode->vht_mcs_set, mcs, 8);
  4058. }
  4059. nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
  4060. nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
  4061. nla_len(nl_freq), freq_policy);
  4062. if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
  4063. continue;
  4064. mode->num_channels++;
  4065. }
  4066. mode->channels = os_calloc(mode->num_channels,
  4067. sizeof(struct hostapd_channel_data));
  4068. if (!mode->channels)
  4069. return NL_SKIP;
  4070. idx = 0;
  4071. nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
  4072. nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
  4073. nla_len(nl_freq), freq_policy);
  4074. if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
  4075. continue;
  4076. mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
  4077. mode->channels[idx].flag = 0;
  4078. if (!mode_is_set) {
  4079. /* crude heuristic */
  4080. if (mode->channels[idx].freq < 4000)
  4081. mode->mode = HOSTAPD_MODE_IEEE80211B;
  4082. else
  4083. mode->mode = HOSTAPD_MODE_IEEE80211A;
  4084. mode_is_set = 1;
  4085. }
  4086. /* crude heuristic */
  4087. if (mode->channels[idx].freq < 4000)
  4088. if (mode->channels[idx].freq == 2484)
  4089. mode->channels[idx].chan = 14;
  4090. else
  4091. mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
  4092. else
  4093. mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
  4094. if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
  4095. mode->channels[idx].flag |=
  4096. HOSTAPD_CHAN_DISABLED;
  4097. if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
  4098. mode->channels[idx].flag |=
  4099. HOSTAPD_CHAN_PASSIVE_SCAN;
  4100. if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
  4101. mode->channels[idx].flag |=
  4102. HOSTAPD_CHAN_NO_IBSS;
  4103. if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
  4104. mode->channels[idx].flag |=
  4105. HOSTAPD_CHAN_RADAR;
  4106. if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
  4107. !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
  4108. mode->channels[idx].max_tx_power =
  4109. nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
  4110. idx++;
  4111. }
  4112. nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
  4113. nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
  4114. nla_len(nl_rate), rate_policy);
  4115. if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
  4116. continue;
  4117. mode->num_rates++;
  4118. }
  4119. mode->rates = os_calloc(mode->num_rates, sizeof(int));
  4120. if (!mode->rates)
  4121. return NL_SKIP;
  4122. idx = 0;
  4123. nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
  4124. nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
  4125. nla_len(nl_rate), rate_policy);
  4126. if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
  4127. continue;
  4128. mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
  4129. /* crude heuristic */
  4130. if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
  4131. mode->rates[idx] > 200)
  4132. mode->mode = HOSTAPD_MODE_IEEE80211G;
  4133. idx++;
  4134. }
  4135. }
  4136. return NL_SKIP;
  4137. }
  4138. static struct hostapd_hw_modes *
  4139. wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes)
  4140. {
  4141. u16 m;
  4142. struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
  4143. int i, mode11g_idx = -1;
  4144. /* If only 802.11g mode is included, use it to construct matching
  4145. * 802.11b mode data. */
  4146. for (m = 0; m < *num_modes; m++) {
  4147. if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
  4148. return modes; /* 802.11b already included */
  4149. if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
  4150. mode11g_idx = m;
  4151. }
  4152. if (mode11g_idx < 0)
  4153. return modes; /* 2.4 GHz band not supported at all */
  4154. nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
  4155. if (nmodes == NULL)
  4156. return modes; /* Could not add 802.11b mode */
  4157. mode = &nmodes[*num_modes];
  4158. os_memset(mode, 0, sizeof(*mode));
  4159. (*num_modes)++;
  4160. modes = nmodes;
  4161. mode->mode = HOSTAPD_MODE_IEEE80211B;
  4162. mode11g = &modes[mode11g_idx];
  4163. mode->num_channels = mode11g->num_channels;
  4164. mode->channels = os_malloc(mode11g->num_channels *
  4165. sizeof(struct hostapd_channel_data));
  4166. if (mode->channels == NULL) {
  4167. (*num_modes)--;
  4168. return modes; /* Could not add 802.11b mode */
  4169. }
  4170. os_memcpy(mode->channels, mode11g->channels,
  4171. mode11g->num_channels * sizeof(struct hostapd_channel_data));
  4172. mode->num_rates = 0;
  4173. mode->rates = os_malloc(4 * sizeof(int));
  4174. if (mode->rates == NULL) {
  4175. os_free(mode->channels);
  4176. (*num_modes)--;
  4177. return modes; /* Could not add 802.11b mode */
  4178. }
  4179. for (i = 0; i < mode11g->num_rates; i++) {
  4180. if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
  4181. mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
  4182. continue;
  4183. mode->rates[mode->num_rates] = mode11g->rates[i];
  4184. mode->num_rates++;
  4185. if (mode->num_rates == 4)
  4186. break;
  4187. }
  4188. if (mode->num_rates == 0) {
  4189. os_free(mode->channels);
  4190. os_free(mode->rates);
  4191. (*num_modes)--;
  4192. return modes; /* No 802.11b rates */
  4193. }
  4194. wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
  4195. "information");
  4196. return modes;
  4197. }
  4198. static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
  4199. int end)
  4200. {
  4201. int c;
  4202. for (c = 0; c < mode->num_channels; c++) {
  4203. struct hostapd_channel_data *chan = &mode->channels[c];
  4204. if (chan->freq - 10 >= start && chan->freq + 10 <= end)
  4205. chan->flag |= HOSTAPD_CHAN_HT40;
  4206. }
  4207. }
  4208. static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
  4209. int end)
  4210. {
  4211. int c;
  4212. for (c = 0; c < mode->num_channels; c++) {
  4213. struct hostapd_channel_data *chan = &mode->channels[c];
  4214. if (!(chan->flag & HOSTAPD_CHAN_HT40))
  4215. continue;
  4216. if (chan->freq - 30 >= start && chan->freq - 10 <= end)
  4217. chan->flag |= HOSTAPD_CHAN_HT40MINUS;
  4218. if (chan->freq + 10 >= start && chan->freq + 30 <= end)
  4219. chan->flag |= HOSTAPD_CHAN_HT40PLUS;
  4220. }
  4221. }
  4222. static void nl80211_reg_rule_ht40(struct nlattr *tb[],
  4223. struct phy_info_arg *results)
  4224. {
  4225. u32 start, end, max_bw;
  4226. u16 m;
  4227. if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
  4228. tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
  4229. tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
  4230. return;
  4231. start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
  4232. end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
  4233. max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
  4234. wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
  4235. start, end, max_bw);
  4236. if (max_bw < 40)
  4237. return;
  4238. for (m = 0; m < *results->num_modes; m++) {
  4239. if (!(results->modes[m].ht_capab &
  4240. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  4241. continue;
  4242. nl80211_set_ht40_mode(&results->modes[m], start, end);
  4243. }
  4244. }
  4245. static void nl80211_reg_rule_sec(struct nlattr *tb[],
  4246. struct phy_info_arg *results)
  4247. {
  4248. u32 start, end, max_bw;
  4249. u16 m;
  4250. if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
  4251. tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
  4252. tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
  4253. return;
  4254. start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
  4255. end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
  4256. max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
  4257. if (max_bw < 20)
  4258. return;
  4259. for (m = 0; m < *results->num_modes; m++) {
  4260. if (!(results->modes[m].ht_capab &
  4261. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  4262. continue;
  4263. nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
  4264. }
  4265. }
  4266. static int nl80211_get_reg(struct nl_msg *msg, void *arg)
  4267. {
  4268. struct phy_info_arg *results = arg;
  4269. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  4270. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  4271. struct nlattr *nl_rule;
  4272. struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
  4273. int rem_rule;
  4274. static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
  4275. [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
  4276. [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
  4277. [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
  4278. [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
  4279. [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
  4280. [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
  4281. };
  4282. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  4283. genlmsg_attrlen(gnlh, 0), NULL);
  4284. if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
  4285. !tb_msg[NL80211_ATTR_REG_RULES]) {
  4286. wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
  4287. "available");
  4288. return NL_SKIP;
  4289. }
  4290. wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
  4291. (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
  4292. nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
  4293. {
  4294. nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
  4295. nla_data(nl_rule), nla_len(nl_rule), reg_policy);
  4296. nl80211_reg_rule_ht40(tb_rule, results);
  4297. }
  4298. nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
  4299. {
  4300. nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
  4301. nla_data(nl_rule), nla_len(nl_rule), reg_policy);
  4302. nl80211_reg_rule_sec(tb_rule, results);
  4303. }
  4304. return NL_SKIP;
  4305. }
  4306. static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv,
  4307. struct phy_info_arg *results)
  4308. {
  4309. struct nl_msg *msg;
  4310. msg = nlmsg_alloc();
  4311. if (!msg)
  4312. return -ENOMEM;
  4313. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
  4314. return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
  4315. }
  4316. static struct hostapd_hw_modes *
  4317. wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
  4318. {
  4319. struct i802_bss *bss = priv;
  4320. struct wpa_driver_nl80211_data *drv = bss->drv;
  4321. struct nl_msg *msg;
  4322. struct phy_info_arg result = {
  4323. .num_modes = num_modes,
  4324. .modes = NULL,
  4325. };
  4326. *num_modes = 0;
  4327. *flags = 0;
  4328. msg = nlmsg_alloc();
  4329. if (!msg)
  4330. return NULL;
  4331. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
  4332. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  4333. if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
  4334. nl80211_set_ht40_flags(drv, &result);
  4335. return wpa_driver_nl80211_add_11b(result.modes, num_modes);
  4336. }
  4337. msg = NULL;
  4338. nla_put_failure:
  4339. nlmsg_free(msg);
  4340. return NULL;
  4341. }
  4342. static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
  4343. const void *data, size_t len,
  4344. int encrypt, int noack)
  4345. {
  4346. __u8 rtap_hdr[] = {
  4347. 0x00, 0x00, /* radiotap version */
  4348. 0x0e, 0x00, /* radiotap length */
  4349. 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
  4350. IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
  4351. 0x00, /* padding */
  4352. 0x00, 0x00, /* RX and TX flags to indicate that */
  4353. 0x00, 0x00, /* this is the injected frame directly */
  4354. };
  4355. struct iovec iov[2] = {
  4356. {
  4357. .iov_base = &rtap_hdr,
  4358. .iov_len = sizeof(rtap_hdr),
  4359. },
  4360. {
  4361. .iov_base = (void *) data,
  4362. .iov_len = len,
  4363. }
  4364. };
  4365. struct msghdr msg = {
  4366. .msg_name = NULL,
  4367. .msg_namelen = 0,
  4368. .msg_iov = iov,
  4369. .msg_iovlen = 2,
  4370. .msg_control = NULL,
  4371. .msg_controllen = 0,
  4372. .msg_flags = 0,
  4373. };
  4374. int res;
  4375. u16 txflags = 0;
  4376. if (encrypt)
  4377. rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
  4378. if (drv->monitor_sock < 0) {
  4379. wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
  4380. "for %s", __func__);
  4381. return -1;
  4382. }
  4383. if (noack)
  4384. txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
  4385. *(le16 *) &rtap_hdr[12] = host_to_le16(txflags);
  4386. res = sendmsg(drv->monitor_sock, &msg, 0);
  4387. if (res < 0) {
  4388. wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
  4389. return -1;
  4390. }
  4391. return 0;
  4392. }
  4393. static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
  4394. const void *data, size_t len,
  4395. int encrypt, int noack,
  4396. unsigned int freq, int no_cck,
  4397. int offchanok, unsigned int wait_time)
  4398. {
  4399. struct wpa_driver_nl80211_data *drv = bss->drv;
  4400. u64 cookie;
  4401. if (freq == 0)
  4402. freq = bss->freq;
  4403. if (drv->use_monitor)
  4404. return wpa_driver_nl80211_send_mntr(drv, data, len,
  4405. encrypt, noack);
  4406. return nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
  4407. &cookie, no_cck, noack, offchanok);
  4408. }
  4409. static int wpa_driver_nl80211_send_mlme_freq(struct i802_bss *bss,
  4410. const u8 *data,
  4411. size_t data_len, int noack,
  4412. unsigned int freq, int no_cck,
  4413. int offchanok,
  4414. unsigned int wait_time)
  4415. {
  4416. struct wpa_driver_nl80211_data *drv = bss->drv;
  4417. struct ieee80211_mgmt *mgmt;
  4418. int encrypt = 1;
  4419. u16 fc;
  4420. mgmt = (struct ieee80211_mgmt *) data;
  4421. fc = le_to_host16(mgmt->frame_control);
  4422. if (is_sta_interface(drv->nlmode) &&
  4423. WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  4424. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
  4425. /*
  4426. * The use of last_mgmt_freq is a bit of a hack,
  4427. * but it works due to the single-threaded nature
  4428. * of wpa_supplicant.
  4429. */
  4430. if (freq == 0)
  4431. freq = drv->last_mgmt_freq;
  4432. return nl80211_send_frame_cmd(bss, freq, 0,
  4433. data, data_len, NULL, 1, noack,
  4434. 1);
  4435. }
  4436. if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
  4437. if (freq == 0)
  4438. freq = bss->freq;
  4439. return nl80211_send_frame_cmd(bss, freq,
  4440. (int) freq == bss->freq ? 0 :
  4441. wait_time,
  4442. data, data_len,
  4443. &drv->send_action_cookie,
  4444. no_cck, noack, offchanok);
  4445. }
  4446. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  4447. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
  4448. /*
  4449. * Only one of the authentication frame types is encrypted.
  4450. * In order for static WEP encryption to work properly (i.e.,
  4451. * to not encrypt the frame), we need to tell mac80211 about
  4452. * the frames that must not be encrypted.
  4453. */
  4454. u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  4455. u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
  4456. if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
  4457. encrypt = 0;
  4458. }
  4459. return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
  4460. noack, freq, no_cck, offchanok,
  4461. wait_time);
  4462. }
  4463. static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
  4464. size_t data_len, int noack)
  4465. {
  4466. struct i802_bss *bss = priv;
  4467. return wpa_driver_nl80211_send_mlme_freq(bss, data, data_len, noack,
  4468. 0, 0, 0, 0);
  4469. }
  4470. static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
  4471. int slot, int ht_opmode, int ap_isolate,
  4472. int *basic_rates)
  4473. {
  4474. struct wpa_driver_nl80211_data *drv = bss->drv;
  4475. struct nl_msg *msg;
  4476. msg = nlmsg_alloc();
  4477. if (!msg)
  4478. return -ENOMEM;
  4479. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
  4480. if (cts >= 0)
  4481. NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
  4482. if (preamble >= 0)
  4483. NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
  4484. if (slot >= 0)
  4485. NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
  4486. if (ht_opmode >= 0)
  4487. NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
  4488. if (ap_isolate >= 0)
  4489. NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
  4490. if (basic_rates) {
  4491. u8 rates[NL80211_MAX_SUPP_RATES];
  4492. u8 rates_len = 0;
  4493. int i;
  4494. for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
  4495. i++)
  4496. rates[rates_len++] = basic_rates[i] / 5;
  4497. NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
  4498. }
  4499. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  4500. return send_and_recv_msgs(drv, msg, NULL, NULL);
  4501. nla_put_failure:
  4502. nlmsg_free(msg);
  4503. return -ENOBUFS;
  4504. }
  4505. static int wpa_driver_nl80211_set_ap(void *priv,
  4506. struct wpa_driver_ap_params *params)
  4507. {
  4508. struct i802_bss *bss = priv;
  4509. struct wpa_driver_nl80211_data *drv = bss->drv;
  4510. struct nl_msg *msg;
  4511. u8 cmd = NL80211_CMD_NEW_BEACON;
  4512. int ret;
  4513. int beacon_set;
  4514. int ifindex = if_nametoindex(bss->ifname);
  4515. int num_suites;
  4516. u32 suites[10];
  4517. u32 ver;
  4518. beacon_set = bss->beacon_set;
  4519. msg = nlmsg_alloc();
  4520. if (!msg)
  4521. return -ENOMEM;
  4522. wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
  4523. beacon_set);
  4524. if (beacon_set)
  4525. cmd = NL80211_CMD_SET_BEACON;
  4526. nl80211_cmd(drv, msg, 0, cmd);
  4527. NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
  4528. NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
  4529. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  4530. NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
  4531. NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
  4532. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  4533. params->ssid);
  4534. if (params->proberesp && params->proberesp_len)
  4535. NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
  4536. params->proberesp);
  4537. switch (params->hide_ssid) {
  4538. case NO_SSID_HIDING:
  4539. NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
  4540. NL80211_HIDDEN_SSID_NOT_IN_USE);
  4541. break;
  4542. case HIDDEN_SSID_ZERO_LEN:
  4543. NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
  4544. NL80211_HIDDEN_SSID_ZERO_LEN);
  4545. break;
  4546. case HIDDEN_SSID_ZERO_CONTENTS:
  4547. NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
  4548. NL80211_HIDDEN_SSID_ZERO_CONTENTS);
  4549. break;
  4550. }
  4551. if (params->privacy)
  4552. NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
  4553. if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
  4554. (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
  4555. /* Leave out the attribute */
  4556. } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
  4557. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
  4558. NL80211_AUTHTYPE_SHARED_KEY);
  4559. else
  4560. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
  4561. NL80211_AUTHTYPE_OPEN_SYSTEM);
  4562. ver = 0;
  4563. if (params->wpa_version & WPA_PROTO_WPA)
  4564. ver |= NL80211_WPA_VERSION_1;
  4565. if (params->wpa_version & WPA_PROTO_RSN)
  4566. ver |= NL80211_WPA_VERSION_2;
  4567. if (ver)
  4568. NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
  4569. num_suites = 0;
  4570. if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
  4571. suites[num_suites++] = WLAN_AKM_SUITE_8021X;
  4572. if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
  4573. suites[num_suites++] = WLAN_AKM_SUITE_PSK;
  4574. if (num_suites) {
  4575. NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
  4576. num_suites * sizeof(u32), suites);
  4577. }
  4578. if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
  4579. params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
  4580. NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
  4581. num_suites = 0;
  4582. if (params->pairwise_ciphers & WPA_CIPHER_CCMP)
  4583. suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
  4584. if (params->pairwise_ciphers & WPA_CIPHER_GCMP)
  4585. suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
  4586. if (params->pairwise_ciphers & WPA_CIPHER_TKIP)
  4587. suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
  4588. if (params->pairwise_ciphers & WPA_CIPHER_WEP104)
  4589. suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
  4590. if (params->pairwise_ciphers & WPA_CIPHER_WEP40)
  4591. suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
  4592. if (num_suites) {
  4593. NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
  4594. num_suites * sizeof(u32), suites);
  4595. }
  4596. switch (params->group_cipher) {
  4597. case WPA_CIPHER_CCMP:
  4598. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
  4599. WLAN_CIPHER_SUITE_CCMP);
  4600. break;
  4601. case WPA_CIPHER_GCMP:
  4602. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
  4603. WLAN_CIPHER_SUITE_GCMP);
  4604. break;
  4605. case WPA_CIPHER_TKIP:
  4606. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
  4607. WLAN_CIPHER_SUITE_TKIP);
  4608. break;
  4609. case WPA_CIPHER_WEP104:
  4610. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
  4611. WLAN_CIPHER_SUITE_WEP104);
  4612. break;
  4613. case WPA_CIPHER_WEP40:
  4614. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
  4615. WLAN_CIPHER_SUITE_WEP40);
  4616. break;
  4617. }
  4618. if (params->beacon_ies) {
  4619. NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
  4620. wpabuf_head(params->beacon_ies));
  4621. }
  4622. if (params->proberesp_ies) {
  4623. NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
  4624. wpabuf_len(params->proberesp_ies),
  4625. wpabuf_head(params->proberesp_ies));
  4626. }
  4627. if (params->assocresp_ies) {
  4628. NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
  4629. wpabuf_len(params->assocresp_ies),
  4630. wpabuf_head(params->assocresp_ies));
  4631. }
  4632. if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
  4633. NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
  4634. params->ap_max_inactivity);
  4635. }
  4636. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4637. if (ret) {
  4638. wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
  4639. ret, strerror(-ret));
  4640. } else {
  4641. bss->beacon_set = 1;
  4642. nl80211_set_bss(bss, params->cts_protect, params->preamble,
  4643. params->short_slot_time, params->ht_opmode,
  4644. params->isolate, params->basic_rates);
  4645. }
  4646. return ret;
  4647. nla_put_failure:
  4648. nlmsg_free(msg);
  4649. return -ENOBUFS;
  4650. }
  4651. static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
  4652. int freq, int ht_enabled,
  4653. int sec_channel_offset)
  4654. {
  4655. struct wpa_driver_nl80211_data *drv = bss->drv;
  4656. struct nl_msg *msg;
  4657. int ret;
  4658. wpa_printf(MSG_DEBUG, "nl80211: Set freq %d (ht_enabled=%d "
  4659. "sec_channel_offset=%d)",
  4660. freq, ht_enabled, sec_channel_offset);
  4661. msg = nlmsg_alloc();
  4662. if (!msg)
  4663. return -1;
  4664. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
  4665. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  4666. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
  4667. if (ht_enabled) {
  4668. switch (sec_channel_offset) {
  4669. case -1:
  4670. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  4671. NL80211_CHAN_HT40MINUS);
  4672. break;
  4673. case 1:
  4674. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  4675. NL80211_CHAN_HT40PLUS);
  4676. break;
  4677. default:
  4678. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  4679. NL80211_CHAN_HT20);
  4680. break;
  4681. }
  4682. }
  4683. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4684. msg = NULL;
  4685. if (ret == 0) {
  4686. bss->freq = freq;
  4687. return 0;
  4688. }
  4689. wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
  4690. "%d (%s)", freq, ret, strerror(-ret));
  4691. nla_put_failure:
  4692. nlmsg_free(msg);
  4693. return -1;
  4694. }
  4695. static u32 sta_flags_nl80211(int flags)
  4696. {
  4697. u32 f = 0;
  4698. if (flags & WPA_STA_AUTHORIZED)
  4699. f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
  4700. if (flags & WPA_STA_WMM)
  4701. f |= BIT(NL80211_STA_FLAG_WME);
  4702. if (flags & WPA_STA_SHORT_PREAMBLE)
  4703. f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
  4704. if (flags & WPA_STA_MFP)
  4705. f |= BIT(NL80211_STA_FLAG_MFP);
  4706. if (flags & WPA_STA_TDLS_PEER)
  4707. f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
  4708. return f;
  4709. }
  4710. static int wpa_driver_nl80211_sta_add(void *priv,
  4711. struct hostapd_sta_add_params *params)
  4712. {
  4713. struct i802_bss *bss = priv;
  4714. struct wpa_driver_nl80211_data *drv = bss->drv;
  4715. struct nl_msg *msg, *wme = NULL;
  4716. struct nl80211_sta_flag_update upd;
  4717. int ret = -ENOBUFS;
  4718. if ((params->flags & WPA_STA_TDLS_PEER) &&
  4719. !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
  4720. return -EOPNOTSUPP;
  4721. msg = nlmsg_alloc();
  4722. if (!msg)
  4723. return -ENOMEM;
  4724. nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
  4725. NL80211_CMD_NEW_STATION);
  4726. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  4727. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
  4728. NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
  4729. params->supp_rates);
  4730. if (!params->set) {
  4731. NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
  4732. NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
  4733. params->listen_interval);
  4734. }
  4735. if (params->ht_capabilities) {
  4736. NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
  4737. sizeof(*params->ht_capabilities),
  4738. params->ht_capabilities);
  4739. }
  4740. os_memset(&upd, 0, sizeof(upd));
  4741. upd.mask = sta_flags_nl80211(params->flags);
  4742. upd.set = upd.mask;
  4743. NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
  4744. if (params->flags & WPA_STA_WMM) {
  4745. wme = nlmsg_alloc();
  4746. if (!wme)
  4747. goto nla_put_failure;
  4748. NLA_PUT_U8(wme, NL80211_STA_WME_UAPSD_QUEUES,
  4749. params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
  4750. NLA_PUT_U8(wme, NL80211_STA_WME_MAX_SP,
  4751. (params->qosinfo > WMM_QOSINFO_STA_SP_SHIFT) &
  4752. WMM_QOSINFO_STA_SP_MASK);
  4753. if (nla_put_nested(msg, NL80211_ATTR_STA_WME, wme) < 0)
  4754. goto nla_put_failure;
  4755. }
  4756. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4757. msg = NULL;
  4758. if (ret)
  4759. wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
  4760. "result: %d (%s)", params->set ? "SET" : "NEW", ret,
  4761. strerror(-ret));
  4762. if (ret == -EEXIST)
  4763. ret = 0;
  4764. nla_put_failure:
  4765. nlmsg_free(wme);
  4766. nlmsg_free(msg);
  4767. return ret;
  4768. }
  4769. static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr)
  4770. {
  4771. struct i802_bss *bss = priv;
  4772. struct wpa_driver_nl80211_data *drv = bss->drv;
  4773. struct nl_msg *msg;
  4774. int ret;
  4775. msg = nlmsg_alloc();
  4776. if (!msg)
  4777. return -ENOMEM;
  4778. nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
  4779. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  4780. if_nametoindex(bss->ifname));
  4781. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  4782. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4783. if (ret == -ENOENT)
  4784. return 0;
  4785. return ret;
  4786. nla_put_failure:
  4787. nlmsg_free(msg);
  4788. return -ENOBUFS;
  4789. }
  4790. static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
  4791. int ifidx)
  4792. {
  4793. struct nl_msg *msg;
  4794. wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
  4795. /* stop listening for EAPOL on this interface */
  4796. del_ifidx(drv, ifidx);
  4797. msg = nlmsg_alloc();
  4798. if (!msg)
  4799. goto nla_put_failure;
  4800. nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
  4801. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
  4802. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  4803. return;
  4804. msg = NULL;
  4805. nla_put_failure:
  4806. nlmsg_free(msg);
  4807. wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
  4808. }
  4809. static const char * nl80211_iftype_str(enum nl80211_iftype mode)
  4810. {
  4811. switch (mode) {
  4812. case NL80211_IFTYPE_ADHOC:
  4813. return "ADHOC";
  4814. case NL80211_IFTYPE_STATION:
  4815. return "STATION";
  4816. case NL80211_IFTYPE_AP:
  4817. return "AP";
  4818. case NL80211_IFTYPE_MONITOR:
  4819. return "MONITOR";
  4820. case NL80211_IFTYPE_P2P_CLIENT:
  4821. return "P2P_CLIENT";
  4822. case NL80211_IFTYPE_P2P_GO:
  4823. return "P2P_GO";
  4824. default:
  4825. return "unknown";
  4826. }
  4827. }
  4828. static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
  4829. const char *ifname,
  4830. enum nl80211_iftype iftype,
  4831. const u8 *addr, int wds)
  4832. {
  4833. struct nl_msg *msg, *flags = NULL;
  4834. int ifidx;
  4835. int ret = -ENOBUFS;
  4836. wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
  4837. iftype, nl80211_iftype_str(iftype));
  4838. msg = nlmsg_alloc();
  4839. if (!msg)
  4840. return -1;
  4841. nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
  4842. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  4843. NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
  4844. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
  4845. if (iftype == NL80211_IFTYPE_MONITOR) {
  4846. int err;
  4847. flags = nlmsg_alloc();
  4848. if (!flags)
  4849. goto nla_put_failure;
  4850. NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
  4851. err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
  4852. nlmsg_free(flags);
  4853. if (err)
  4854. goto nla_put_failure;
  4855. } else if (wds) {
  4856. NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
  4857. }
  4858. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4859. msg = NULL;
  4860. if (ret) {
  4861. nla_put_failure:
  4862. nlmsg_free(msg);
  4863. wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
  4864. ifname, ret, strerror(-ret));
  4865. return ret;
  4866. }
  4867. ifidx = if_nametoindex(ifname);
  4868. wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
  4869. ifname, ifidx);
  4870. if (ifidx <= 0)
  4871. return -1;
  4872. /* start listening for EAPOL on this interface */
  4873. add_ifidx(drv, ifidx);
  4874. if (addr && iftype != NL80211_IFTYPE_MONITOR &&
  4875. linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
  4876. nl80211_remove_iface(drv, ifidx);
  4877. return -1;
  4878. }
  4879. return ifidx;
  4880. }
  4881. static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
  4882. const char *ifname, enum nl80211_iftype iftype,
  4883. const u8 *addr, int wds)
  4884. {
  4885. int ret;
  4886. ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
  4887. /* if error occurred and interface exists already */
  4888. if (ret == -ENFILE && if_nametoindex(ifname)) {
  4889. wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
  4890. /* Try to remove the interface that was already there. */
  4891. nl80211_remove_iface(drv, if_nametoindex(ifname));
  4892. /* Try to create the interface again */
  4893. ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
  4894. wds);
  4895. }
  4896. if (ret >= 0 && is_p2p_interface(iftype))
  4897. nl80211_disable_11b_rates(drv, ret, 1);
  4898. return ret;
  4899. }
  4900. static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
  4901. {
  4902. struct ieee80211_hdr *hdr;
  4903. u16 fc;
  4904. union wpa_event_data event;
  4905. hdr = (struct ieee80211_hdr *) buf;
  4906. fc = le_to_host16(hdr->frame_control);
  4907. os_memset(&event, 0, sizeof(event));
  4908. event.tx_status.type = WLAN_FC_GET_TYPE(fc);
  4909. event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
  4910. event.tx_status.dst = hdr->addr1;
  4911. event.tx_status.data = buf;
  4912. event.tx_status.data_len = len;
  4913. event.tx_status.ack = ok;
  4914. wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
  4915. }
  4916. static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
  4917. u8 *buf, size_t len)
  4918. {
  4919. struct ieee80211_hdr *hdr = (void *)buf;
  4920. u16 fc;
  4921. union wpa_event_data event;
  4922. if (len < sizeof(*hdr))
  4923. return;
  4924. fc = le_to_host16(hdr->frame_control);
  4925. os_memset(&event, 0, sizeof(event));
  4926. event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
  4927. event.rx_from_unknown.addr = hdr->addr2;
  4928. event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
  4929. (WLAN_FC_FROMDS | WLAN_FC_TODS);
  4930. wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
  4931. }
  4932. static void handle_frame(struct wpa_driver_nl80211_data *drv,
  4933. u8 *buf, size_t len, int datarate, int ssi_signal)
  4934. {
  4935. struct ieee80211_hdr *hdr;
  4936. u16 fc;
  4937. union wpa_event_data event;
  4938. hdr = (struct ieee80211_hdr *) buf;
  4939. fc = le_to_host16(hdr->frame_control);
  4940. switch (WLAN_FC_GET_TYPE(fc)) {
  4941. case WLAN_FC_TYPE_MGMT:
  4942. os_memset(&event, 0, sizeof(event));
  4943. event.rx_mgmt.frame = buf;
  4944. event.rx_mgmt.frame_len = len;
  4945. event.rx_mgmt.datarate = datarate;
  4946. event.rx_mgmt.ssi_signal = ssi_signal;
  4947. wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
  4948. break;
  4949. case WLAN_FC_TYPE_CTRL:
  4950. /* can only get here with PS-Poll frames */
  4951. wpa_printf(MSG_DEBUG, "CTRL");
  4952. from_unknown_sta(drv, buf, len);
  4953. break;
  4954. case WLAN_FC_TYPE_DATA:
  4955. from_unknown_sta(drv, buf, len);
  4956. break;
  4957. }
  4958. }
  4959. static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
  4960. {
  4961. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  4962. int len;
  4963. unsigned char buf[3000];
  4964. struct ieee80211_radiotap_iterator iter;
  4965. int ret;
  4966. int datarate = 0, ssi_signal = 0;
  4967. int injected = 0, failed = 0, rxflags = 0;
  4968. len = recv(sock, buf, sizeof(buf), 0);
  4969. if (len < 0) {
  4970. perror("recv");
  4971. return;
  4972. }
  4973. if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
  4974. printf("received invalid radiotap frame\n");
  4975. return;
  4976. }
  4977. while (1) {
  4978. ret = ieee80211_radiotap_iterator_next(&iter);
  4979. if (ret == -ENOENT)
  4980. break;
  4981. if (ret) {
  4982. printf("received invalid radiotap frame (%d)\n", ret);
  4983. return;
  4984. }
  4985. switch (iter.this_arg_index) {
  4986. case IEEE80211_RADIOTAP_FLAGS:
  4987. if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
  4988. len -= 4;
  4989. break;
  4990. case IEEE80211_RADIOTAP_RX_FLAGS:
  4991. rxflags = 1;
  4992. break;
  4993. case IEEE80211_RADIOTAP_TX_FLAGS:
  4994. injected = 1;
  4995. failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
  4996. IEEE80211_RADIOTAP_F_TX_FAIL;
  4997. break;
  4998. case IEEE80211_RADIOTAP_DATA_RETRIES:
  4999. break;
  5000. case IEEE80211_RADIOTAP_CHANNEL:
  5001. /* TODO: convert from freq/flags to channel number */
  5002. break;
  5003. case IEEE80211_RADIOTAP_RATE:
  5004. datarate = *iter.this_arg * 5;
  5005. break;
  5006. case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
  5007. ssi_signal = (s8) *iter.this_arg;
  5008. break;
  5009. }
  5010. }
  5011. if (rxflags && injected)
  5012. return;
  5013. if (!injected)
  5014. handle_frame(drv, buf + iter.max_length,
  5015. len - iter.max_length, datarate, ssi_signal);
  5016. else
  5017. handle_tx_callback(drv->ctx, buf + iter.max_length,
  5018. len - iter.max_length, !failed);
  5019. }
  5020. /*
  5021. * we post-process the filter code later and rewrite
  5022. * this to the offset to the last instruction
  5023. */
  5024. #define PASS 0xFF
  5025. #define FAIL 0xFE
  5026. static struct sock_filter msock_filter_insns[] = {
  5027. /*
  5028. * do a little-endian load of the radiotap length field
  5029. */
  5030. /* load lower byte into A */
  5031. BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
  5032. /* put it into X (== index register) */
  5033. BPF_STMT(BPF_MISC| BPF_TAX, 0),
  5034. /* load upper byte into A */
  5035. BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
  5036. /* left-shift it by 8 */
  5037. BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
  5038. /* or with X */
  5039. BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
  5040. /* put result into X */
  5041. BPF_STMT(BPF_MISC| BPF_TAX, 0),
  5042. /*
  5043. * Allow management frames through, this also gives us those
  5044. * management frames that we sent ourselves with status
  5045. */
  5046. /* load the lower byte of the IEEE 802.11 frame control field */
  5047. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  5048. /* mask off frame type and version */
  5049. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
  5050. /* accept frame if it's both 0, fall through otherwise */
  5051. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
  5052. /*
  5053. * TODO: add a bit to radiotap RX flags that indicates
  5054. * that the sending station is not associated, then
  5055. * add a filter here that filters on our DA and that flag
  5056. * to allow us to deauth frames to that bad station.
  5057. *
  5058. * For now allow all To DS data frames through.
  5059. */
  5060. /* load the IEEE 802.11 frame control field */
  5061. BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
  5062. /* mask off frame type, version and DS status */
  5063. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
  5064. /* accept frame if version 0, type 2 and To DS, fall through otherwise
  5065. */
  5066. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
  5067. #if 0
  5068. /*
  5069. * drop non-data frames
  5070. */
  5071. /* load the lower byte of the frame control field */
  5072. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  5073. /* mask off QoS bit */
  5074. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
  5075. /* drop non-data frames */
  5076. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
  5077. #endif
  5078. /* load the upper byte of the frame control field */
  5079. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
  5080. /* mask off toDS/fromDS */
  5081. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
  5082. /* accept WDS frames */
  5083. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
  5084. /*
  5085. * add header length to index
  5086. */
  5087. /* load the lower byte of the frame control field */
  5088. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  5089. /* mask off QoS bit */
  5090. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
  5091. /* right shift it by 6 to give 0 or 2 */
  5092. BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
  5093. /* add data frame header length */
  5094. BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
  5095. /* add index, was start of 802.11 header */
  5096. BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
  5097. /* move to index, now start of LL header */
  5098. BPF_STMT(BPF_MISC | BPF_TAX, 0),
  5099. /*
  5100. * Accept empty data frames, we use those for
  5101. * polling activity.
  5102. */
  5103. BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
  5104. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
  5105. /*
  5106. * Accept EAPOL frames
  5107. */
  5108. BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
  5109. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
  5110. BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
  5111. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
  5112. /* keep these last two statements or change the code below */
  5113. /* return 0 == "DROP" */
  5114. BPF_STMT(BPF_RET | BPF_K, 0),
  5115. /* return ~0 == "keep all" */
  5116. BPF_STMT(BPF_RET | BPF_K, ~0),
  5117. };
  5118. static struct sock_fprog msock_filter = {
  5119. .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
  5120. .filter = msock_filter_insns,
  5121. };
  5122. static int add_monitor_filter(int s)
  5123. {
  5124. int idx;
  5125. /* rewrite all PASS/FAIL jump offsets */
  5126. for (idx = 0; idx < msock_filter.len; idx++) {
  5127. struct sock_filter *insn = &msock_filter_insns[idx];
  5128. if (BPF_CLASS(insn->code) == BPF_JMP) {
  5129. if (insn->code == (BPF_JMP|BPF_JA)) {
  5130. if (insn->k == PASS)
  5131. insn->k = msock_filter.len - idx - 2;
  5132. else if (insn->k == FAIL)
  5133. insn->k = msock_filter.len - idx - 3;
  5134. }
  5135. if (insn->jt == PASS)
  5136. insn->jt = msock_filter.len - idx - 2;
  5137. else if (insn->jt == FAIL)
  5138. insn->jt = msock_filter.len - idx - 3;
  5139. if (insn->jf == PASS)
  5140. insn->jf = msock_filter.len - idx - 2;
  5141. else if (insn->jf == FAIL)
  5142. insn->jf = msock_filter.len - idx - 3;
  5143. }
  5144. }
  5145. if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
  5146. &msock_filter, sizeof(msock_filter))) {
  5147. perror("SO_ATTACH_FILTER");
  5148. return -1;
  5149. }
  5150. return 0;
  5151. }
  5152. static void nl80211_remove_monitor_interface(
  5153. struct wpa_driver_nl80211_data *drv)
  5154. {
  5155. drv->monitor_refcount--;
  5156. if (drv->monitor_refcount > 0)
  5157. return;
  5158. if (drv->monitor_ifidx >= 0) {
  5159. nl80211_remove_iface(drv, drv->monitor_ifidx);
  5160. drv->monitor_ifidx = -1;
  5161. }
  5162. if (drv->monitor_sock >= 0) {
  5163. eloop_unregister_read_sock(drv->monitor_sock);
  5164. close(drv->monitor_sock);
  5165. drv->monitor_sock = -1;
  5166. }
  5167. }
  5168. static int
  5169. nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
  5170. {
  5171. char buf[IFNAMSIZ];
  5172. struct sockaddr_ll ll;
  5173. int optval;
  5174. socklen_t optlen;
  5175. if (drv->monitor_ifidx >= 0) {
  5176. drv->monitor_refcount++;
  5177. return 0;
  5178. }
  5179. if (os_strncmp(drv->first_bss.ifname, "p2p-", 4) == 0) {
  5180. /*
  5181. * P2P interface name is of the format p2p-%s-%d. For monitor
  5182. * interface name corresponding to P2P GO, replace "p2p-" with
  5183. * "mon-" to retain the same interface name length and to
  5184. * indicate that it is a monitor interface.
  5185. */
  5186. snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss.ifname + 4);
  5187. } else {
  5188. /* Non-P2P interface with AP functionality. */
  5189. snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
  5190. }
  5191. buf[IFNAMSIZ - 1] = '\0';
  5192. drv->monitor_ifidx =
  5193. nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
  5194. 0);
  5195. if (drv->monitor_ifidx == -EOPNOTSUPP) {
  5196. /*
  5197. * This is backward compatibility for a few versions of
  5198. * the kernel only that didn't advertise the right
  5199. * attributes for the only driver that then supported
  5200. * AP mode w/o monitor -- ath6kl.
  5201. */
  5202. wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
  5203. "monitor interface type - try to run without it");
  5204. drv->device_ap_sme = 1;
  5205. }
  5206. if (drv->monitor_ifidx < 0)
  5207. return -1;
  5208. if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
  5209. goto error;
  5210. memset(&ll, 0, sizeof(ll));
  5211. ll.sll_family = AF_PACKET;
  5212. ll.sll_ifindex = drv->monitor_ifidx;
  5213. drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  5214. if (drv->monitor_sock < 0) {
  5215. perror("socket[PF_PACKET,SOCK_RAW]");
  5216. goto error;
  5217. }
  5218. if (add_monitor_filter(drv->monitor_sock)) {
  5219. wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
  5220. "interface; do filtering in user space");
  5221. /* This works, but will cost in performance. */
  5222. }
  5223. if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
  5224. perror("monitor socket bind");
  5225. goto error;
  5226. }
  5227. optlen = sizeof(optval);
  5228. optval = 20;
  5229. if (setsockopt
  5230. (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
  5231. perror("Failed to set socket priority");
  5232. goto error;
  5233. }
  5234. if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
  5235. drv, NULL)) {
  5236. printf("Could not register monitor read socket\n");
  5237. goto error;
  5238. }
  5239. return 0;
  5240. error:
  5241. nl80211_remove_monitor_interface(drv);
  5242. return -1;
  5243. }
  5244. static int nl80211_setup_ap(struct i802_bss *bss)
  5245. {
  5246. struct wpa_driver_nl80211_data *drv = bss->drv;
  5247. wpa_printf(MSG_DEBUG, "nl80211: Setup AP - device_ap_sme=%d "
  5248. "use_monitor=%d", drv->device_ap_sme, drv->use_monitor);
  5249. /*
  5250. * Disable Probe Request reporting unless we need it in this way for
  5251. * devices that include the AP SME, in the other case (unless using
  5252. * monitor iface) we'll get it through the nl_mgmt socket instead.
  5253. */
  5254. if (!drv->device_ap_sme)
  5255. wpa_driver_nl80211_probe_req_report(bss, 0);
  5256. if (!drv->device_ap_sme && !drv->use_monitor)
  5257. if (nl80211_mgmt_subscribe_ap(bss))
  5258. return -1;
  5259. if (drv->device_ap_sme && !drv->use_monitor)
  5260. if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
  5261. return -1;
  5262. if (!drv->device_ap_sme && drv->use_monitor &&
  5263. nl80211_create_monitor_interface(drv) &&
  5264. !drv->device_ap_sme)
  5265. return -1;
  5266. if (drv->device_ap_sme &&
  5267. wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
  5268. wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
  5269. "Probe Request frame reporting in AP mode");
  5270. /* Try to survive without this */
  5271. }
  5272. return 0;
  5273. }
  5274. static void nl80211_teardown_ap(struct i802_bss *bss)
  5275. {
  5276. struct wpa_driver_nl80211_data *drv = bss->drv;
  5277. if (drv->device_ap_sme) {
  5278. wpa_driver_nl80211_probe_req_report(bss, 0);
  5279. if (!drv->use_monitor)
  5280. nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
  5281. } else if (drv->use_monitor)
  5282. nl80211_remove_monitor_interface(drv);
  5283. else
  5284. nl80211_mgmt_unsubscribe(bss, "AP teardown");
  5285. bss->beacon_set = 0;
  5286. }
  5287. static int nl80211_send_eapol_data(struct i802_bss *bss,
  5288. const u8 *addr, const u8 *data,
  5289. size_t data_len)
  5290. {
  5291. struct sockaddr_ll ll;
  5292. int ret;
  5293. if (bss->drv->eapol_tx_sock < 0) {
  5294. wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
  5295. return -1;
  5296. }
  5297. os_memset(&ll, 0, sizeof(ll));
  5298. ll.sll_family = AF_PACKET;
  5299. ll.sll_ifindex = bss->ifindex;
  5300. ll.sll_protocol = htons(ETH_P_PAE);
  5301. ll.sll_halen = ETH_ALEN;
  5302. os_memcpy(ll.sll_addr, addr, ETH_ALEN);
  5303. ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
  5304. (struct sockaddr *) &ll, sizeof(ll));
  5305. if (ret < 0)
  5306. wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
  5307. strerror(errno));
  5308. return ret;
  5309. }
  5310. static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  5311. static int wpa_driver_nl80211_hapd_send_eapol(
  5312. void *priv, const u8 *addr, const u8 *data,
  5313. size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
  5314. {
  5315. struct i802_bss *bss = priv;
  5316. struct wpa_driver_nl80211_data *drv = bss->drv;
  5317. struct ieee80211_hdr *hdr;
  5318. size_t len;
  5319. u8 *pos;
  5320. int res;
  5321. int qos = flags & WPA_STA_WMM;
  5322. if (drv->device_ap_sme || !drv->use_monitor)
  5323. return nl80211_send_eapol_data(bss, addr, data, data_len);
  5324. len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
  5325. data_len;
  5326. hdr = os_zalloc(len);
  5327. if (hdr == NULL) {
  5328. printf("malloc() failed for i802_send_data(len=%lu)\n",
  5329. (unsigned long) len);
  5330. return -1;
  5331. }
  5332. hdr->frame_control =
  5333. IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
  5334. hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
  5335. if (encrypt)
  5336. hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  5337. if (qos) {
  5338. hdr->frame_control |=
  5339. host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
  5340. }
  5341. memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
  5342. memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
  5343. memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
  5344. pos = (u8 *) (hdr + 1);
  5345. if (qos) {
  5346. /* Set highest priority in QoS header */
  5347. pos[0] = 7;
  5348. pos[1] = 0;
  5349. pos += 2;
  5350. }
  5351. memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
  5352. pos += sizeof(rfc1042_header);
  5353. WPA_PUT_BE16(pos, ETH_P_PAE);
  5354. pos += 2;
  5355. memcpy(pos, data, data_len);
  5356. res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
  5357. 0, 0, 0, 0);
  5358. if (res < 0) {
  5359. wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
  5360. "failed: %d (%s)",
  5361. (unsigned long) len, errno, strerror(errno));
  5362. }
  5363. os_free(hdr);
  5364. return res;
  5365. }
  5366. static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
  5367. int total_flags,
  5368. int flags_or, int flags_and)
  5369. {
  5370. struct i802_bss *bss = priv;
  5371. struct wpa_driver_nl80211_data *drv = bss->drv;
  5372. struct nl_msg *msg, *flags = NULL;
  5373. struct nl80211_sta_flag_update upd;
  5374. msg = nlmsg_alloc();
  5375. if (!msg)
  5376. return -ENOMEM;
  5377. flags = nlmsg_alloc();
  5378. if (!flags) {
  5379. nlmsg_free(msg);
  5380. return -ENOMEM;
  5381. }
  5382. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
  5383. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  5384. if_nametoindex(bss->ifname));
  5385. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  5386. /*
  5387. * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
  5388. * can be removed eventually.
  5389. */
  5390. if (total_flags & WPA_STA_AUTHORIZED)
  5391. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
  5392. if (total_flags & WPA_STA_WMM)
  5393. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
  5394. if (total_flags & WPA_STA_SHORT_PREAMBLE)
  5395. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
  5396. if (total_flags & WPA_STA_MFP)
  5397. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
  5398. if (total_flags & WPA_STA_TDLS_PEER)
  5399. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_TDLS_PEER);
  5400. if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
  5401. goto nla_put_failure;
  5402. os_memset(&upd, 0, sizeof(upd));
  5403. upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
  5404. upd.set = sta_flags_nl80211(flags_or);
  5405. NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
  5406. nlmsg_free(flags);
  5407. return send_and_recv_msgs(drv, msg, NULL, NULL);
  5408. nla_put_failure:
  5409. nlmsg_free(msg);
  5410. nlmsg_free(flags);
  5411. return -ENOBUFS;
  5412. }
  5413. static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
  5414. struct wpa_driver_associate_params *params)
  5415. {
  5416. enum nl80211_iftype nlmode;
  5417. if (params->p2p) {
  5418. wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
  5419. "group (GO)");
  5420. nlmode = NL80211_IFTYPE_P2P_GO;
  5421. } else
  5422. nlmode = NL80211_IFTYPE_AP;
  5423. if (wpa_driver_nl80211_set_mode(&drv->first_bss, nlmode) ||
  5424. wpa_driver_nl80211_set_freq(&drv->first_bss, params->freq, 0, 0)) {
  5425. nl80211_remove_monitor_interface(drv);
  5426. return -1;
  5427. }
  5428. return 0;
  5429. }
  5430. static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
  5431. {
  5432. struct nl_msg *msg;
  5433. int ret = -1;
  5434. msg = nlmsg_alloc();
  5435. if (!msg)
  5436. return -1;
  5437. nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
  5438. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  5439. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5440. msg = NULL;
  5441. if (ret) {
  5442. wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
  5443. "(%s)", ret, strerror(-ret));
  5444. goto nla_put_failure;
  5445. }
  5446. ret = 0;
  5447. wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
  5448. nla_put_failure:
  5449. nlmsg_free(msg);
  5450. return ret;
  5451. }
  5452. static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
  5453. struct wpa_driver_associate_params *params)
  5454. {
  5455. struct nl_msg *msg;
  5456. int ret = -1;
  5457. int count = 0;
  5458. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
  5459. if (wpa_driver_nl80211_set_mode(&drv->first_bss,
  5460. NL80211_IFTYPE_ADHOC)) {
  5461. wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
  5462. "IBSS mode");
  5463. return -1;
  5464. }
  5465. retry:
  5466. msg = nlmsg_alloc();
  5467. if (!msg)
  5468. return -1;
  5469. nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
  5470. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  5471. if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
  5472. goto nla_put_failure;
  5473. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  5474. params->ssid, params->ssid_len);
  5475. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  5476. params->ssid);
  5477. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  5478. drv->ssid_len = params->ssid_len;
  5479. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  5480. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  5481. ret = nl80211_set_conn_keys(params, msg);
  5482. if (ret)
  5483. goto nla_put_failure;
  5484. if (params->bssid && params->fixed_bssid) {
  5485. wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
  5486. MAC2STR(params->bssid));
  5487. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  5488. }
  5489. if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
  5490. params->key_mgmt_suite == KEY_MGMT_PSK ||
  5491. params->key_mgmt_suite == KEY_MGMT_802_1X_SHA256 ||
  5492. params->key_mgmt_suite == KEY_MGMT_PSK_SHA256) {
  5493. wpa_printf(MSG_DEBUG, " * control port");
  5494. NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
  5495. }
  5496. if (params->wpa_ie) {
  5497. wpa_hexdump(MSG_DEBUG,
  5498. " * Extra IEs for Beacon/Probe Response frames",
  5499. params->wpa_ie, params->wpa_ie_len);
  5500. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  5501. params->wpa_ie);
  5502. }
  5503. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5504. msg = NULL;
  5505. if (ret) {
  5506. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
  5507. ret, strerror(-ret));
  5508. count++;
  5509. if (ret == -EALREADY && count == 1) {
  5510. wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
  5511. "forced leave");
  5512. nl80211_leave_ibss(drv);
  5513. nlmsg_free(msg);
  5514. goto retry;
  5515. }
  5516. goto nla_put_failure;
  5517. }
  5518. ret = 0;
  5519. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
  5520. nla_put_failure:
  5521. nlmsg_free(msg);
  5522. return ret;
  5523. }
  5524. static unsigned int nl80211_get_assoc_bssid(struct wpa_driver_nl80211_data *drv,
  5525. u8 *bssid)
  5526. {
  5527. struct nl_msg *msg;
  5528. int ret;
  5529. struct nl80211_bss_info_arg arg;
  5530. os_memset(&arg, 0, sizeof(arg));
  5531. msg = nlmsg_alloc();
  5532. if (!msg)
  5533. goto nla_put_failure;
  5534. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
  5535. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  5536. arg.drv = drv;
  5537. ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
  5538. msg = NULL;
  5539. if (ret == 0) {
  5540. if (is_zero_ether_addr(arg.assoc_bssid))
  5541. return -ENOTCONN;
  5542. os_memcpy(bssid, arg.assoc_bssid, ETH_ALEN);
  5543. return 0;
  5544. }
  5545. wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
  5546. "(%s)", ret, strerror(-ret));
  5547. nla_put_failure:
  5548. nlmsg_free(msg);
  5549. return drv->assoc_freq;
  5550. }
  5551. static int nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
  5552. const u8 *bssid)
  5553. {
  5554. u8 addr[ETH_ALEN];
  5555. if (bssid == NULL) {
  5556. int res = nl80211_get_assoc_bssid(drv, addr);
  5557. if (res)
  5558. return res;
  5559. bssid = addr;
  5560. }
  5561. return wpa_driver_nl80211_disconnect(drv, bssid,
  5562. WLAN_REASON_PREV_AUTH_NOT_VALID);
  5563. }
  5564. static int wpa_driver_nl80211_connect(
  5565. struct wpa_driver_nl80211_data *drv,
  5566. struct wpa_driver_associate_params *params)
  5567. {
  5568. struct nl_msg *msg;
  5569. enum nl80211_auth_type type;
  5570. int ret = 0;
  5571. int algs;
  5572. msg = nlmsg_alloc();
  5573. if (!msg)
  5574. return -1;
  5575. wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
  5576. nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
  5577. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  5578. if (params->bssid) {
  5579. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  5580. MAC2STR(params->bssid));
  5581. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  5582. }
  5583. if (params->freq) {
  5584. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  5585. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  5586. }
  5587. if (params->bg_scan_period >= 0) {
  5588. wpa_printf(MSG_DEBUG, " * bg scan period=%d",
  5589. params->bg_scan_period);
  5590. NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
  5591. params->bg_scan_period);
  5592. }
  5593. if (params->ssid) {
  5594. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  5595. params->ssid, params->ssid_len);
  5596. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  5597. params->ssid);
  5598. if (params->ssid_len > sizeof(drv->ssid))
  5599. goto nla_put_failure;
  5600. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  5601. drv->ssid_len = params->ssid_len;
  5602. }
  5603. wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
  5604. if (params->wpa_ie)
  5605. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  5606. params->wpa_ie);
  5607. algs = 0;
  5608. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  5609. algs++;
  5610. if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  5611. algs++;
  5612. if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  5613. algs++;
  5614. if (algs > 1) {
  5615. wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
  5616. "selection");
  5617. goto skip_auth_type;
  5618. }
  5619. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  5620. type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  5621. else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  5622. type = NL80211_AUTHTYPE_SHARED_KEY;
  5623. else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  5624. type = NL80211_AUTHTYPE_NETWORK_EAP;
  5625. else if (params->auth_alg & WPA_AUTH_ALG_FT)
  5626. type = NL80211_AUTHTYPE_FT;
  5627. else
  5628. goto nla_put_failure;
  5629. wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
  5630. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
  5631. skip_auth_type:
  5632. if (params->wpa_proto) {
  5633. enum nl80211_wpa_versions ver = 0;
  5634. if (params->wpa_proto & WPA_PROTO_WPA)
  5635. ver |= NL80211_WPA_VERSION_1;
  5636. if (params->wpa_proto & WPA_PROTO_RSN)
  5637. ver |= NL80211_WPA_VERSION_2;
  5638. wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
  5639. NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
  5640. }
  5641. if (params->pairwise_suite != CIPHER_NONE) {
  5642. int cipher;
  5643. switch (params->pairwise_suite) {
  5644. case CIPHER_WEP40:
  5645. cipher = WLAN_CIPHER_SUITE_WEP40;
  5646. break;
  5647. case CIPHER_WEP104:
  5648. cipher = WLAN_CIPHER_SUITE_WEP104;
  5649. break;
  5650. case CIPHER_CCMP:
  5651. cipher = WLAN_CIPHER_SUITE_CCMP;
  5652. break;
  5653. case CIPHER_GCMP:
  5654. cipher = WLAN_CIPHER_SUITE_GCMP;
  5655. break;
  5656. case CIPHER_TKIP:
  5657. default:
  5658. cipher = WLAN_CIPHER_SUITE_TKIP;
  5659. break;
  5660. }
  5661. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
  5662. }
  5663. if (params->group_suite != CIPHER_NONE) {
  5664. int cipher;
  5665. switch (params->group_suite) {
  5666. case CIPHER_WEP40:
  5667. cipher = WLAN_CIPHER_SUITE_WEP40;
  5668. break;
  5669. case CIPHER_WEP104:
  5670. cipher = WLAN_CIPHER_SUITE_WEP104;
  5671. break;
  5672. case CIPHER_CCMP:
  5673. cipher = WLAN_CIPHER_SUITE_CCMP;
  5674. break;
  5675. case CIPHER_GCMP:
  5676. cipher = WLAN_CIPHER_SUITE_GCMP;
  5677. break;
  5678. case CIPHER_TKIP:
  5679. default:
  5680. cipher = WLAN_CIPHER_SUITE_TKIP;
  5681. break;
  5682. }
  5683. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
  5684. }
  5685. if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
  5686. params->key_mgmt_suite == KEY_MGMT_PSK) {
  5687. int mgmt = WLAN_AKM_SUITE_PSK;
  5688. switch (params->key_mgmt_suite) {
  5689. case KEY_MGMT_802_1X:
  5690. mgmt = WLAN_AKM_SUITE_8021X;
  5691. break;
  5692. case KEY_MGMT_PSK:
  5693. default:
  5694. mgmt = WLAN_AKM_SUITE_PSK;
  5695. break;
  5696. }
  5697. NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
  5698. }
  5699. if (params->disable_ht)
  5700. NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
  5701. if (params->htcaps && params->htcaps_mask) {
  5702. int sz = sizeof(struct ieee80211_ht_capabilities);
  5703. NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
  5704. NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
  5705. params->htcaps_mask);
  5706. }
  5707. ret = nl80211_set_conn_keys(params, msg);
  5708. if (ret)
  5709. goto nla_put_failure;
  5710. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5711. msg = NULL;
  5712. if (ret) {
  5713. wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
  5714. "(%s)", ret, strerror(-ret));
  5715. /*
  5716. * cfg80211 does not currently accept new connection if we are
  5717. * already connected. As a workaround, force disconnection and
  5718. * try again once the driver indicates it completed
  5719. * disconnection.
  5720. */
  5721. if (ret == -EALREADY)
  5722. nl80211_disconnect(drv, params->bssid);
  5723. goto nla_put_failure;
  5724. }
  5725. ret = 0;
  5726. wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
  5727. nla_put_failure:
  5728. nlmsg_free(msg);
  5729. return ret;
  5730. }
  5731. static int wpa_driver_nl80211_associate(
  5732. void *priv, struct wpa_driver_associate_params *params)
  5733. {
  5734. struct i802_bss *bss = priv;
  5735. struct wpa_driver_nl80211_data *drv = bss->drv;
  5736. int ret = -1;
  5737. struct nl_msg *msg;
  5738. if (params->mode == IEEE80211_MODE_AP)
  5739. return wpa_driver_nl80211_ap(drv, params);
  5740. if (params->mode == IEEE80211_MODE_IBSS)
  5741. return wpa_driver_nl80211_ibss(drv, params);
  5742. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
  5743. enum nl80211_iftype nlmode = params->p2p ?
  5744. NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
  5745. if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
  5746. return -1;
  5747. return wpa_driver_nl80211_connect(drv, params);
  5748. }
  5749. drv->associated = 0;
  5750. msg = nlmsg_alloc();
  5751. if (!msg)
  5752. return -1;
  5753. wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
  5754. drv->ifindex);
  5755. nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
  5756. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  5757. if (params->bssid) {
  5758. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  5759. MAC2STR(params->bssid));
  5760. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  5761. }
  5762. if (params->freq) {
  5763. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  5764. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  5765. drv->assoc_freq = params->freq;
  5766. } else
  5767. drv->assoc_freq = 0;
  5768. if (params->bg_scan_period >= 0) {
  5769. wpa_printf(MSG_DEBUG, " * bg scan period=%d",
  5770. params->bg_scan_period);
  5771. NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
  5772. params->bg_scan_period);
  5773. }
  5774. if (params->ssid) {
  5775. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  5776. params->ssid, params->ssid_len);
  5777. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  5778. params->ssid);
  5779. if (params->ssid_len > sizeof(drv->ssid))
  5780. goto nla_put_failure;
  5781. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  5782. drv->ssid_len = params->ssid_len;
  5783. }
  5784. wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
  5785. if (params->wpa_ie)
  5786. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  5787. params->wpa_ie);
  5788. if (params->pairwise_suite != CIPHER_NONE) {
  5789. int cipher;
  5790. switch (params->pairwise_suite) {
  5791. case CIPHER_WEP40:
  5792. cipher = WLAN_CIPHER_SUITE_WEP40;
  5793. break;
  5794. case CIPHER_WEP104:
  5795. cipher = WLAN_CIPHER_SUITE_WEP104;
  5796. break;
  5797. case CIPHER_CCMP:
  5798. cipher = WLAN_CIPHER_SUITE_CCMP;
  5799. break;
  5800. case CIPHER_GCMP:
  5801. cipher = WLAN_CIPHER_SUITE_GCMP;
  5802. break;
  5803. case CIPHER_TKIP:
  5804. default:
  5805. cipher = WLAN_CIPHER_SUITE_TKIP;
  5806. break;
  5807. }
  5808. wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
  5809. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
  5810. }
  5811. if (params->group_suite != CIPHER_NONE) {
  5812. int cipher;
  5813. switch (params->group_suite) {
  5814. case CIPHER_WEP40:
  5815. cipher = WLAN_CIPHER_SUITE_WEP40;
  5816. break;
  5817. case CIPHER_WEP104:
  5818. cipher = WLAN_CIPHER_SUITE_WEP104;
  5819. break;
  5820. case CIPHER_CCMP:
  5821. cipher = WLAN_CIPHER_SUITE_CCMP;
  5822. break;
  5823. case CIPHER_GCMP:
  5824. cipher = WLAN_CIPHER_SUITE_GCMP;
  5825. break;
  5826. case CIPHER_TKIP:
  5827. default:
  5828. cipher = WLAN_CIPHER_SUITE_TKIP;
  5829. break;
  5830. }
  5831. wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
  5832. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
  5833. }
  5834. #ifdef CONFIG_IEEE80211W
  5835. if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
  5836. NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
  5837. #endif /* CONFIG_IEEE80211W */
  5838. NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
  5839. if (params->prev_bssid) {
  5840. wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
  5841. MAC2STR(params->prev_bssid));
  5842. NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
  5843. params->prev_bssid);
  5844. }
  5845. if (params->disable_ht)
  5846. NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
  5847. if (params->htcaps && params->htcaps_mask) {
  5848. int sz = sizeof(struct ieee80211_ht_capabilities);
  5849. NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
  5850. NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
  5851. params->htcaps_mask);
  5852. }
  5853. if (params->p2p)
  5854. wpa_printf(MSG_DEBUG, " * P2P group");
  5855. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5856. msg = NULL;
  5857. if (ret) {
  5858. wpa_dbg(drv->ctx, MSG_DEBUG,
  5859. "nl80211: MLME command failed (assoc): ret=%d (%s)",
  5860. ret, strerror(-ret));
  5861. nl80211_dump_scan(drv);
  5862. goto nla_put_failure;
  5863. }
  5864. ret = 0;
  5865. wpa_printf(MSG_DEBUG, "nl80211: Association request send "
  5866. "successfully");
  5867. nla_put_failure:
  5868. nlmsg_free(msg);
  5869. return ret;
  5870. }
  5871. static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
  5872. int ifindex, enum nl80211_iftype mode)
  5873. {
  5874. struct nl_msg *msg;
  5875. int ret = -ENOBUFS;
  5876. wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
  5877. ifindex, mode, nl80211_iftype_str(mode));
  5878. msg = nlmsg_alloc();
  5879. if (!msg)
  5880. return -ENOMEM;
  5881. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
  5882. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  5883. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
  5884. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5885. msg = NULL;
  5886. if (!ret)
  5887. return 0;
  5888. nla_put_failure:
  5889. nlmsg_free(msg);
  5890. wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
  5891. " %d (%s)", ifindex, mode, ret, strerror(-ret));
  5892. return ret;
  5893. }
  5894. static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
  5895. enum nl80211_iftype nlmode)
  5896. {
  5897. struct wpa_driver_nl80211_data *drv = bss->drv;
  5898. int ret = -1;
  5899. int i;
  5900. int was_ap = is_ap_interface(drv->nlmode);
  5901. int res;
  5902. res = nl80211_set_mode(drv, drv->ifindex, nlmode);
  5903. if (res == 0) {
  5904. drv->nlmode = nlmode;
  5905. ret = 0;
  5906. goto done;
  5907. }
  5908. if (res == -ENODEV)
  5909. return -1;
  5910. if (nlmode == drv->nlmode) {
  5911. wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
  5912. "requested mode - ignore error");
  5913. ret = 0;
  5914. goto done; /* Already in the requested mode */
  5915. }
  5916. /* mac80211 doesn't allow mode changes while the device is up, so
  5917. * take the device down, try to set the mode again, and bring the
  5918. * device back up.
  5919. */
  5920. wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
  5921. "interface down");
  5922. for (i = 0; i < 10; i++) {
  5923. res = linux_set_iface_flags(drv->global->ioctl_sock,
  5924. bss->ifname, 0);
  5925. if (res == -EACCES || res == -ENODEV)
  5926. break;
  5927. if (res == 0) {
  5928. /* Try to set the mode again while the interface is
  5929. * down */
  5930. ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
  5931. if (ret == -EACCES)
  5932. break;
  5933. res = linux_set_iface_flags(drv->global->ioctl_sock,
  5934. bss->ifname, 1);
  5935. if (res && !ret)
  5936. ret = -1;
  5937. else if (ret != -EBUSY)
  5938. break;
  5939. } else
  5940. wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
  5941. "interface down");
  5942. os_sleep(0, 100000);
  5943. }
  5944. if (!ret) {
  5945. wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
  5946. "interface is down");
  5947. drv->nlmode = nlmode;
  5948. drv->ignore_if_down_event = 1;
  5949. }
  5950. done:
  5951. if (ret) {
  5952. wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
  5953. "from %d failed", nlmode, drv->nlmode);
  5954. return ret;
  5955. }
  5956. if (is_p2p_interface(nlmode))
  5957. nl80211_disable_11b_rates(drv, drv->ifindex, 1);
  5958. else if (drv->disabled_11b_rates)
  5959. nl80211_disable_11b_rates(drv, drv->ifindex, 0);
  5960. if (is_ap_interface(nlmode)) {
  5961. nl80211_mgmt_unsubscribe(bss, "start AP");
  5962. /* Setup additional AP mode functionality if needed */
  5963. if (nl80211_setup_ap(bss))
  5964. return -1;
  5965. } else if (was_ap) {
  5966. /* Remove additional AP mode functionality */
  5967. nl80211_teardown_ap(bss);
  5968. } else {
  5969. nl80211_mgmt_unsubscribe(bss, "mode change");
  5970. }
  5971. if (!bss->in_deinit && !is_ap_interface(nlmode) &&
  5972. nl80211_mgmt_subscribe_non_ap(bss) < 0)
  5973. wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
  5974. "frame processing - ignore for now");
  5975. return 0;
  5976. }
  5977. static int wpa_driver_nl80211_get_capa(void *priv,
  5978. struct wpa_driver_capa *capa)
  5979. {
  5980. struct i802_bss *bss = priv;
  5981. struct wpa_driver_nl80211_data *drv = bss->drv;
  5982. if (!drv->has_capability)
  5983. return -1;
  5984. os_memcpy(capa, &drv->capa, sizeof(*capa));
  5985. return 0;
  5986. }
  5987. static int wpa_driver_nl80211_set_operstate(void *priv, int state)
  5988. {
  5989. struct i802_bss *bss = priv;
  5990. struct wpa_driver_nl80211_data *drv = bss->drv;
  5991. wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
  5992. __func__, drv->operstate, state, state ? "UP" : "DORMANT");
  5993. drv->operstate = state;
  5994. return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
  5995. state ? IF_OPER_UP : IF_OPER_DORMANT);
  5996. }
  5997. static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
  5998. {
  5999. struct i802_bss *bss = priv;
  6000. struct wpa_driver_nl80211_data *drv = bss->drv;
  6001. struct nl_msg *msg;
  6002. struct nl80211_sta_flag_update upd;
  6003. msg = nlmsg_alloc();
  6004. if (!msg)
  6005. return -ENOMEM;
  6006. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
  6007. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  6008. if_nametoindex(bss->ifname));
  6009. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
  6010. os_memset(&upd, 0, sizeof(upd));
  6011. upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
  6012. if (authorized)
  6013. upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
  6014. NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
  6015. return send_and_recv_msgs(drv, msg, NULL, NULL);
  6016. nla_put_failure:
  6017. nlmsg_free(msg);
  6018. return -ENOBUFS;
  6019. }
  6020. /* Set kernel driver on given frequency (MHz) */
  6021. static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
  6022. {
  6023. struct i802_bss *bss = priv;
  6024. return wpa_driver_nl80211_set_freq(bss, freq->freq, freq->ht_enabled,
  6025. freq->sec_channel_offset);
  6026. }
  6027. #if defined(HOSTAPD) || defined(CONFIG_AP)
  6028. static inline int min_int(int a, int b)
  6029. {
  6030. if (a < b)
  6031. return a;
  6032. return b;
  6033. }
  6034. static int get_key_handler(struct nl_msg *msg, void *arg)
  6035. {
  6036. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  6037. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  6038. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  6039. genlmsg_attrlen(gnlh, 0), NULL);
  6040. /*
  6041. * TODO: validate the key index and mac address!
  6042. * Otherwise, there's a race condition as soon as
  6043. * the kernel starts sending key notifications.
  6044. */
  6045. if (tb[NL80211_ATTR_KEY_SEQ])
  6046. memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
  6047. min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
  6048. return NL_SKIP;
  6049. }
  6050. static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
  6051. int idx, u8 *seq)
  6052. {
  6053. struct i802_bss *bss = priv;
  6054. struct wpa_driver_nl80211_data *drv = bss->drv;
  6055. struct nl_msg *msg;
  6056. msg = nlmsg_alloc();
  6057. if (!msg)
  6058. return -ENOMEM;
  6059. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
  6060. if (addr)
  6061. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  6062. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
  6063. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
  6064. memset(seq, 0, 6);
  6065. return send_and_recv_msgs(drv, msg, get_key_handler, seq);
  6066. nla_put_failure:
  6067. nlmsg_free(msg);
  6068. return -ENOBUFS;
  6069. }
  6070. static int i802_set_rts(void *priv, int rts)
  6071. {
  6072. struct i802_bss *bss = priv;
  6073. struct wpa_driver_nl80211_data *drv = bss->drv;
  6074. struct nl_msg *msg;
  6075. int ret = -ENOBUFS;
  6076. u32 val;
  6077. msg = nlmsg_alloc();
  6078. if (!msg)
  6079. return -ENOMEM;
  6080. if (rts >= 2347)
  6081. val = (u32) -1;
  6082. else
  6083. val = rts;
  6084. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
  6085. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  6086. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
  6087. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6088. msg = NULL;
  6089. if (!ret)
  6090. return 0;
  6091. nla_put_failure:
  6092. nlmsg_free(msg);
  6093. wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
  6094. "%d (%s)", rts, ret, strerror(-ret));
  6095. return ret;
  6096. }
  6097. static int i802_set_frag(void *priv, int frag)
  6098. {
  6099. struct i802_bss *bss = priv;
  6100. struct wpa_driver_nl80211_data *drv = bss->drv;
  6101. struct nl_msg *msg;
  6102. int ret = -ENOBUFS;
  6103. u32 val;
  6104. msg = nlmsg_alloc();
  6105. if (!msg)
  6106. return -ENOMEM;
  6107. if (frag >= 2346)
  6108. val = (u32) -1;
  6109. else
  6110. val = frag;
  6111. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
  6112. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  6113. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
  6114. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6115. msg = NULL;
  6116. if (!ret)
  6117. return 0;
  6118. nla_put_failure:
  6119. nlmsg_free(msg);
  6120. wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
  6121. "%d: %d (%s)", frag, ret, strerror(-ret));
  6122. return ret;
  6123. }
  6124. static int i802_flush(void *priv)
  6125. {
  6126. struct i802_bss *bss = priv;
  6127. struct wpa_driver_nl80211_data *drv = bss->drv;
  6128. struct nl_msg *msg;
  6129. int res;
  6130. msg = nlmsg_alloc();
  6131. if (!msg)
  6132. return -1;
  6133. nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
  6134. /*
  6135. * XXX: FIX! this needs to flush all VLANs too
  6136. */
  6137. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  6138. if_nametoindex(bss->ifname));
  6139. res = send_and_recv_msgs(drv, msg, NULL, NULL);
  6140. if (res) {
  6141. wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
  6142. "(%s)", res, strerror(-res));
  6143. }
  6144. return res;
  6145. nla_put_failure:
  6146. nlmsg_free(msg);
  6147. return -ENOBUFS;
  6148. }
  6149. #endif /* HOSTAPD || CONFIG_AP */
  6150. static int get_sta_handler(struct nl_msg *msg, void *arg)
  6151. {
  6152. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  6153. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  6154. struct hostap_sta_driver_data *data = arg;
  6155. struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
  6156. static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
  6157. [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
  6158. [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
  6159. [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
  6160. [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
  6161. [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
  6162. [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
  6163. };
  6164. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  6165. genlmsg_attrlen(gnlh, 0), NULL);
  6166. /*
  6167. * TODO: validate the interface and mac address!
  6168. * Otherwise, there's a race condition as soon as
  6169. * the kernel starts sending station notifications.
  6170. */
  6171. if (!tb[NL80211_ATTR_STA_INFO]) {
  6172. wpa_printf(MSG_DEBUG, "sta stats missing!");
  6173. return NL_SKIP;
  6174. }
  6175. if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
  6176. tb[NL80211_ATTR_STA_INFO],
  6177. stats_policy)) {
  6178. wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
  6179. return NL_SKIP;
  6180. }
  6181. if (stats[NL80211_STA_INFO_INACTIVE_TIME])
  6182. data->inactive_msec =
  6183. nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
  6184. if (stats[NL80211_STA_INFO_RX_BYTES])
  6185. data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
  6186. if (stats[NL80211_STA_INFO_TX_BYTES])
  6187. data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
  6188. if (stats[NL80211_STA_INFO_RX_PACKETS])
  6189. data->rx_packets =
  6190. nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
  6191. if (stats[NL80211_STA_INFO_TX_PACKETS])
  6192. data->tx_packets =
  6193. nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
  6194. if (stats[NL80211_STA_INFO_TX_FAILED])
  6195. data->tx_retry_failed =
  6196. nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
  6197. return NL_SKIP;
  6198. }
  6199. static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
  6200. const u8 *addr)
  6201. {
  6202. struct i802_bss *bss = priv;
  6203. struct wpa_driver_nl80211_data *drv = bss->drv;
  6204. struct nl_msg *msg;
  6205. os_memset(data, 0, sizeof(*data));
  6206. msg = nlmsg_alloc();
  6207. if (!msg)
  6208. return -ENOMEM;
  6209. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
  6210. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  6211. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  6212. return send_and_recv_msgs(drv, msg, get_sta_handler, data);
  6213. nla_put_failure:
  6214. nlmsg_free(msg);
  6215. return -ENOBUFS;
  6216. }
  6217. #if defined(HOSTAPD) || defined(CONFIG_AP)
  6218. static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
  6219. int cw_min, int cw_max, int burst_time)
  6220. {
  6221. struct i802_bss *bss = priv;
  6222. struct wpa_driver_nl80211_data *drv = bss->drv;
  6223. struct nl_msg *msg;
  6224. struct nlattr *txq, *params;
  6225. msg = nlmsg_alloc();
  6226. if (!msg)
  6227. return -1;
  6228. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
  6229. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  6230. txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
  6231. if (!txq)
  6232. goto nla_put_failure;
  6233. /* We are only sending parameters for a single TXQ at a time */
  6234. params = nla_nest_start(msg, 1);
  6235. if (!params)
  6236. goto nla_put_failure;
  6237. switch (queue) {
  6238. case 0:
  6239. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
  6240. break;
  6241. case 1:
  6242. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
  6243. break;
  6244. case 2:
  6245. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
  6246. break;
  6247. case 3:
  6248. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
  6249. break;
  6250. }
  6251. /* Burst time is configured in units of 0.1 msec and TXOP parameter in
  6252. * 32 usec, so need to convert the value here. */
  6253. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
  6254. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
  6255. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
  6256. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
  6257. nla_nest_end(msg, params);
  6258. nla_nest_end(msg, txq);
  6259. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  6260. return 0;
  6261. msg = NULL;
  6262. nla_put_failure:
  6263. nlmsg_free(msg);
  6264. return -1;
  6265. }
  6266. static int i802_set_sta_vlan(void *priv, const u8 *addr,
  6267. const char *ifname, int vlan_id)
  6268. {
  6269. struct i802_bss *bss = priv;
  6270. struct wpa_driver_nl80211_data *drv = bss->drv;
  6271. struct nl_msg *msg;
  6272. int ret = -ENOBUFS;
  6273. msg = nlmsg_alloc();
  6274. if (!msg)
  6275. return -ENOMEM;
  6276. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
  6277. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  6278. if_nametoindex(bss->ifname));
  6279. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  6280. NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
  6281. if_nametoindex(ifname));
  6282. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6283. msg = NULL;
  6284. if (ret < 0) {
  6285. wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
  6286. MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
  6287. MAC2STR(addr), ifname, vlan_id, ret,
  6288. strerror(-ret));
  6289. }
  6290. nla_put_failure:
  6291. nlmsg_free(msg);
  6292. return ret;
  6293. }
  6294. static int i802_get_inact_sec(void *priv, const u8 *addr)
  6295. {
  6296. struct hostap_sta_driver_data data;
  6297. int ret;
  6298. data.inactive_msec = (unsigned long) -1;
  6299. ret = i802_read_sta_data(priv, &data, addr);
  6300. if (ret || data.inactive_msec == (unsigned long) -1)
  6301. return -1;
  6302. return data.inactive_msec / 1000;
  6303. }
  6304. static int i802_sta_clear_stats(void *priv, const u8 *addr)
  6305. {
  6306. #if 0
  6307. /* TODO */
  6308. #endif
  6309. return 0;
  6310. }
  6311. static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
  6312. int reason)
  6313. {
  6314. struct i802_bss *bss = priv;
  6315. struct wpa_driver_nl80211_data *drv = bss->drv;
  6316. struct ieee80211_mgmt mgmt;
  6317. if (drv->device_ap_sme)
  6318. return wpa_driver_nl80211_sta_remove(bss, addr);
  6319. memset(&mgmt, 0, sizeof(mgmt));
  6320. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  6321. WLAN_FC_STYPE_DEAUTH);
  6322. memcpy(mgmt.da, addr, ETH_ALEN);
  6323. memcpy(mgmt.sa, own_addr, ETH_ALEN);
  6324. memcpy(mgmt.bssid, own_addr, ETH_ALEN);
  6325. mgmt.u.deauth.reason_code = host_to_le16(reason);
  6326. return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
  6327. IEEE80211_HDRLEN +
  6328. sizeof(mgmt.u.deauth), 0);
  6329. }
  6330. static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
  6331. int reason)
  6332. {
  6333. struct i802_bss *bss = priv;
  6334. struct wpa_driver_nl80211_data *drv = bss->drv;
  6335. struct ieee80211_mgmt mgmt;
  6336. if (drv->device_ap_sme)
  6337. return wpa_driver_nl80211_sta_remove(bss, addr);
  6338. memset(&mgmt, 0, sizeof(mgmt));
  6339. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  6340. WLAN_FC_STYPE_DISASSOC);
  6341. memcpy(mgmt.da, addr, ETH_ALEN);
  6342. memcpy(mgmt.sa, own_addr, ETH_ALEN);
  6343. memcpy(mgmt.bssid, own_addr, ETH_ALEN);
  6344. mgmt.u.disassoc.reason_code = host_to_le16(reason);
  6345. return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
  6346. IEEE80211_HDRLEN +
  6347. sizeof(mgmt.u.disassoc), 0);
  6348. }
  6349. #endif /* HOSTAPD || CONFIG_AP */
  6350. #ifdef HOSTAPD
  6351. static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  6352. {
  6353. int i;
  6354. int *old;
  6355. wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
  6356. ifidx);
  6357. for (i = 0; i < drv->num_if_indices; i++) {
  6358. if (drv->if_indices[i] == 0) {
  6359. drv->if_indices[i] = ifidx;
  6360. return;
  6361. }
  6362. }
  6363. if (drv->if_indices != drv->default_if_indices)
  6364. old = drv->if_indices;
  6365. else
  6366. old = NULL;
  6367. drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
  6368. sizeof(int));
  6369. if (!drv->if_indices) {
  6370. if (!old)
  6371. drv->if_indices = drv->default_if_indices;
  6372. else
  6373. drv->if_indices = old;
  6374. wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
  6375. "interfaces");
  6376. wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
  6377. return;
  6378. } else if (!old)
  6379. os_memcpy(drv->if_indices, drv->default_if_indices,
  6380. sizeof(drv->default_if_indices));
  6381. drv->if_indices[drv->num_if_indices] = ifidx;
  6382. drv->num_if_indices++;
  6383. }
  6384. static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  6385. {
  6386. int i;
  6387. for (i = 0; i < drv->num_if_indices; i++) {
  6388. if (drv->if_indices[i] == ifidx) {
  6389. drv->if_indices[i] = 0;
  6390. break;
  6391. }
  6392. }
  6393. }
  6394. static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  6395. {
  6396. int i;
  6397. for (i = 0; i < drv->num_if_indices; i++)
  6398. if (drv->if_indices[i] == ifidx)
  6399. return 1;
  6400. return 0;
  6401. }
  6402. static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
  6403. const char *bridge_ifname)
  6404. {
  6405. struct i802_bss *bss = priv;
  6406. struct wpa_driver_nl80211_data *drv = bss->drv;
  6407. char name[IFNAMSIZ + 1];
  6408. os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
  6409. wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
  6410. " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
  6411. if (val) {
  6412. if (!if_nametoindex(name)) {
  6413. if (nl80211_create_iface(drv, name,
  6414. NL80211_IFTYPE_AP_VLAN,
  6415. NULL, 1) < 0)
  6416. return -1;
  6417. if (bridge_ifname &&
  6418. linux_br_add_if(drv->global->ioctl_sock,
  6419. bridge_ifname, name) < 0)
  6420. return -1;
  6421. }
  6422. if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
  6423. wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
  6424. "interface %s up", name);
  6425. }
  6426. return i802_set_sta_vlan(priv, addr, name, 0);
  6427. } else {
  6428. if (bridge_ifname)
  6429. linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
  6430. name);
  6431. i802_set_sta_vlan(priv, addr, bss->ifname, 0);
  6432. return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
  6433. name);
  6434. }
  6435. }
  6436. static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
  6437. {
  6438. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  6439. struct sockaddr_ll lladdr;
  6440. unsigned char buf[3000];
  6441. int len;
  6442. socklen_t fromlen = sizeof(lladdr);
  6443. len = recvfrom(sock, buf, sizeof(buf), 0,
  6444. (struct sockaddr *)&lladdr, &fromlen);
  6445. if (len < 0) {
  6446. perror("recv");
  6447. return;
  6448. }
  6449. if (have_ifidx(drv, lladdr.sll_ifindex))
  6450. drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
  6451. }
  6452. static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
  6453. struct i802_bss *bss,
  6454. const char *brname, const char *ifname)
  6455. {
  6456. int ifindex;
  6457. char in_br[IFNAMSIZ];
  6458. os_strlcpy(bss->brname, brname, IFNAMSIZ);
  6459. ifindex = if_nametoindex(brname);
  6460. if (ifindex == 0) {
  6461. /*
  6462. * Bridge was configured, but the bridge device does
  6463. * not exist. Try to add it now.
  6464. */
  6465. if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
  6466. wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
  6467. "bridge interface %s: %s",
  6468. brname, strerror(errno));
  6469. return -1;
  6470. }
  6471. bss->added_bridge = 1;
  6472. add_ifidx(drv, if_nametoindex(brname));
  6473. }
  6474. if (linux_br_get(in_br, ifname) == 0) {
  6475. if (os_strcmp(in_br, brname) == 0)
  6476. return 0; /* already in the bridge */
  6477. wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
  6478. "bridge %s", ifname, in_br);
  6479. if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
  6480. 0) {
  6481. wpa_printf(MSG_ERROR, "nl80211: Failed to "
  6482. "remove interface %s from bridge "
  6483. "%s: %s",
  6484. ifname, brname, strerror(errno));
  6485. return -1;
  6486. }
  6487. }
  6488. wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
  6489. ifname, brname);
  6490. if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
  6491. wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
  6492. "into bridge %s: %s",
  6493. ifname, brname, strerror(errno));
  6494. return -1;
  6495. }
  6496. bss->added_if_into_bridge = 1;
  6497. return 0;
  6498. }
  6499. static void *i802_init(struct hostapd_data *hapd,
  6500. struct wpa_init_params *params)
  6501. {
  6502. struct wpa_driver_nl80211_data *drv;
  6503. struct i802_bss *bss;
  6504. size_t i;
  6505. char brname[IFNAMSIZ];
  6506. int ifindex, br_ifindex;
  6507. int br_added = 0;
  6508. bss = wpa_driver_nl80211_init(hapd, params->ifname,
  6509. params->global_priv);
  6510. if (bss == NULL)
  6511. return NULL;
  6512. drv = bss->drv;
  6513. drv->nlmode = NL80211_IFTYPE_AP;
  6514. drv->eapol_sock = -1;
  6515. if (linux_br_get(brname, params->ifname) == 0) {
  6516. wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
  6517. params->ifname, brname);
  6518. br_ifindex = if_nametoindex(brname);
  6519. } else {
  6520. brname[0] = '\0';
  6521. br_ifindex = 0;
  6522. }
  6523. drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
  6524. drv->if_indices = drv->default_if_indices;
  6525. for (i = 0; i < params->num_bridge; i++) {
  6526. if (params->bridge[i]) {
  6527. ifindex = if_nametoindex(params->bridge[i]);
  6528. if (ifindex)
  6529. add_ifidx(drv, ifindex);
  6530. if (ifindex == br_ifindex)
  6531. br_added = 1;
  6532. }
  6533. }
  6534. if (!br_added && br_ifindex &&
  6535. (params->num_bridge == 0 || !params->bridge[0]))
  6536. add_ifidx(drv, br_ifindex);
  6537. /* start listening for EAPOL on the default AP interface */
  6538. add_ifidx(drv, drv->ifindex);
  6539. if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0))
  6540. goto failed;
  6541. if (params->bssid) {
  6542. if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  6543. params->bssid))
  6544. goto failed;
  6545. }
  6546. if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) {
  6547. wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
  6548. "into AP mode", bss->ifname);
  6549. goto failed;
  6550. }
  6551. if (params->num_bridge && params->bridge[0] &&
  6552. i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
  6553. goto failed;
  6554. if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1))
  6555. goto failed;
  6556. drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
  6557. if (drv->eapol_sock < 0) {
  6558. perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
  6559. goto failed;
  6560. }
  6561. if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
  6562. {
  6563. printf("Could not register read socket for eapol\n");
  6564. goto failed;
  6565. }
  6566. if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  6567. params->own_addr))
  6568. goto failed;
  6569. memcpy(bss->addr, params->own_addr, ETH_ALEN);
  6570. return bss;
  6571. failed:
  6572. wpa_driver_nl80211_deinit(bss);
  6573. return NULL;
  6574. }
  6575. static void i802_deinit(void *priv)
  6576. {
  6577. wpa_driver_nl80211_deinit(priv);
  6578. }
  6579. #endif /* HOSTAPD */
  6580. static enum nl80211_iftype wpa_driver_nl80211_if_type(
  6581. enum wpa_driver_if_type type)
  6582. {
  6583. switch (type) {
  6584. case WPA_IF_STATION:
  6585. return NL80211_IFTYPE_STATION;
  6586. case WPA_IF_P2P_CLIENT:
  6587. case WPA_IF_P2P_GROUP:
  6588. return NL80211_IFTYPE_P2P_CLIENT;
  6589. case WPA_IF_AP_VLAN:
  6590. return NL80211_IFTYPE_AP_VLAN;
  6591. case WPA_IF_AP_BSS:
  6592. return NL80211_IFTYPE_AP;
  6593. case WPA_IF_P2P_GO:
  6594. return NL80211_IFTYPE_P2P_GO;
  6595. }
  6596. return -1;
  6597. }
  6598. #ifdef CONFIG_P2P
  6599. static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
  6600. {
  6601. struct wpa_driver_nl80211_data *drv;
  6602. dl_list_for_each(drv, &global->interfaces,
  6603. struct wpa_driver_nl80211_data, list) {
  6604. if (os_memcmp(addr, drv->first_bss.addr, ETH_ALEN) == 0)
  6605. return 1;
  6606. }
  6607. return 0;
  6608. }
  6609. static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
  6610. u8 *new_addr)
  6611. {
  6612. unsigned int idx;
  6613. if (!drv->global)
  6614. return -1;
  6615. os_memcpy(new_addr, drv->first_bss.addr, ETH_ALEN);
  6616. for (idx = 0; idx < 64; idx++) {
  6617. new_addr[0] = drv->first_bss.addr[0] | 0x02;
  6618. new_addr[0] ^= idx << 2;
  6619. if (!nl80211_addr_in_use(drv->global, new_addr))
  6620. break;
  6621. }
  6622. if (idx == 64)
  6623. return -1;
  6624. wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
  6625. MACSTR, MAC2STR(new_addr));
  6626. return 0;
  6627. }
  6628. #endif /* CONFIG_P2P */
  6629. static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
  6630. const char *ifname, const u8 *addr,
  6631. void *bss_ctx, void **drv_priv,
  6632. char *force_ifname, u8 *if_addr,
  6633. const char *bridge)
  6634. {
  6635. struct i802_bss *bss = priv;
  6636. struct wpa_driver_nl80211_data *drv = bss->drv;
  6637. int ifidx;
  6638. #ifdef HOSTAPD
  6639. struct i802_bss *new_bss = NULL;
  6640. if (type == WPA_IF_AP_BSS) {
  6641. new_bss = os_zalloc(sizeof(*new_bss));
  6642. if (new_bss == NULL)
  6643. return -1;
  6644. }
  6645. #endif /* HOSTAPD */
  6646. if (addr)
  6647. os_memcpy(if_addr, addr, ETH_ALEN);
  6648. ifidx = nl80211_create_iface(drv, ifname,
  6649. wpa_driver_nl80211_if_type(type), addr,
  6650. 0);
  6651. if (ifidx < 0) {
  6652. #ifdef HOSTAPD
  6653. os_free(new_bss);
  6654. #endif /* HOSTAPD */
  6655. return -1;
  6656. }
  6657. if (!addr &&
  6658. linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  6659. if_addr) < 0) {
  6660. nl80211_remove_iface(drv, ifidx);
  6661. return -1;
  6662. }
  6663. #ifdef CONFIG_P2P
  6664. if (!addr &&
  6665. (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
  6666. type == WPA_IF_P2P_GO)) {
  6667. /* Enforce unique P2P Interface Address */
  6668. u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN];
  6669. if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  6670. own_addr) < 0 ||
  6671. linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
  6672. new_addr) < 0) {
  6673. nl80211_remove_iface(drv, ifidx);
  6674. return -1;
  6675. }
  6676. if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) {
  6677. wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
  6678. "for P2P group interface");
  6679. if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
  6680. nl80211_remove_iface(drv, ifidx);
  6681. return -1;
  6682. }
  6683. if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
  6684. new_addr) < 0) {
  6685. nl80211_remove_iface(drv, ifidx);
  6686. return -1;
  6687. }
  6688. }
  6689. os_memcpy(if_addr, new_addr, ETH_ALEN);
  6690. }
  6691. #endif /* CONFIG_P2P */
  6692. #ifdef HOSTAPD
  6693. if (bridge &&
  6694. i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
  6695. wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
  6696. "interface %s to a bridge %s", ifname, bridge);
  6697. nl80211_remove_iface(drv, ifidx);
  6698. os_free(new_bss);
  6699. return -1;
  6700. }
  6701. if (type == WPA_IF_AP_BSS) {
  6702. if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
  6703. {
  6704. nl80211_remove_iface(drv, ifidx);
  6705. os_free(new_bss);
  6706. return -1;
  6707. }
  6708. os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
  6709. os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
  6710. new_bss->ifindex = ifidx;
  6711. new_bss->drv = drv;
  6712. new_bss->next = drv->first_bss.next;
  6713. new_bss->freq = drv->first_bss.freq;
  6714. drv->first_bss.next = new_bss;
  6715. if (drv_priv)
  6716. *drv_priv = new_bss;
  6717. nl80211_init_bss(new_bss);
  6718. /* Subscribe management frames for this WPA_IF_AP_BSS */
  6719. if (nl80211_setup_ap(new_bss))
  6720. return -1;
  6721. }
  6722. #endif /* HOSTAPD */
  6723. if (drv->global)
  6724. drv->global->if_add_ifindex = ifidx;
  6725. return 0;
  6726. }
  6727. static int wpa_driver_nl80211_if_remove(void *priv,
  6728. enum wpa_driver_if_type type,
  6729. const char *ifname)
  6730. {
  6731. struct i802_bss *bss = priv;
  6732. struct wpa_driver_nl80211_data *drv = bss->drv;
  6733. int ifindex = if_nametoindex(ifname);
  6734. wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
  6735. __func__, type, ifname, ifindex);
  6736. if (ifindex <= 0)
  6737. return -1;
  6738. nl80211_remove_iface(drv, ifindex);
  6739. #ifdef HOSTAPD
  6740. if (type != WPA_IF_AP_BSS)
  6741. return 0;
  6742. if (bss->added_if_into_bridge) {
  6743. if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
  6744. bss->ifname) < 0)
  6745. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  6746. "interface %s from bridge %s: %s",
  6747. bss->ifname, bss->brname, strerror(errno));
  6748. }
  6749. if (bss->added_bridge) {
  6750. if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
  6751. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  6752. "bridge %s: %s",
  6753. bss->brname, strerror(errno));
  6754. }
  6755. if (bss != &drv->first_bss) {
  6756. struct i802_bss *tbss;
  6757. for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
  6758. if (tbss->next == bss) {
  6759. tbss->next = bss->next;
  6760. /* Unsubscribe management frames */
  6761. nl80211_teardown_ap(bss);
  6762. nl80211_destroy_bss(bss);
  6763. os_free(bss);
  6764. bss = NULL;
  6765. break;
  6766. }
  6767. }
  6768. if (bss)
  6769. wpa_printf(MSG_INFO, "nl80211: %s - could not find "
  6770. "BSS %p in the list", __func__, bss);
  6771. }
  6772. #endif /* HOSTAPD */
  6773. return 0;
  6774. }
  6775. static int cookie_handler(struct nl_msg *msg, void *arg)
  6776. {
  6777. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  6778. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  6779. u64 *cookie = arg;
  6780. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  6781. genlmsg_attrlen(gnlh, 0), NULL);
  6782. if (tb[NL80211_ATTR_COOKIE])
  6783. *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
  6784. return NL_SKIP;
  6785. }
  6786. static int nl80211_send_frame_cmd(struct i802_bss *bss,
  6787. unsigned int freq, unsigned int wait,
  6788. const u8 *buf, size_t buf_len,
  6789. u64 *cookie_out, int no_cck, int no_ack,
  6790. int offchanok)
  6791. {
  6792. struct wpa_driver_nl80211_data *drv = bss->drv;
  6793. struct nl_msg *msg;
  6794. u64 cookie;
  6795. int ret = -1;
  6796. msg = nlmsg_alloc();
  6797. if (!msg)
  6798. return -1;
  6799. wpa_printf(MSG_DEBUG, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
  6800. "no_ack=%d offchanok=%d",
  6801. freq, wait, no_cck, no_ack, offchanok);
  6802. nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
  6803. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  6804. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
  6805. if (wait)
  6806. NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
  6807. if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
  6808. NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
  6809. if (no_cck)
  6810. NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
  6811. if (no_ack)
  6812. NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
  6813. NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
  6814. cookie = 0;
  6815. ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
  6816. msg = NULL;
  6817. if (ret) {
  6818. wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
  6819. "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
  6820. freq, wait);
  6821. goto nla_put_failure;
  6822. }
  6823. wpa_printf(MSG_DEBUG, "nl80211: Frame TX command accepted%s; "
  6824. "cookie 0x%llx", no_ack ? " (no ACK)" : "",
  6825. (long long unsigned int) cookie);
  6826. if (cookie_out)
  6827. *cookie_out = no_ack ? (u64) -1 : cookie;
  6828. nla_put_failure:
  6829. nlmsg_free(msg);
  6830. return ret;
  6831. }
  6832. static int wpa_driver_nl80211_send_action(void *priv, unsigned int freq,
  6833. unsigned int wait_time,
  6834. const u8 *dst, const u8 *src,
  6835. const u8 *bssid,
  6836. const u8 *data, size_t data_len,
  6837. int no_cck)
  6838. {
  6839. struct i802_bss *bss = priv;
  6840. struct wpa_driver_nl80211_data *drv = bss->drv;
  6841. int ret = -1;
  6842. u8 *buf;
  6843. struct ieee80211_hdr *hdr;
  6844. wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
  6845. "freq=%u MHz wait=%d ms no_cck=%d)",
  6846. drv->ifindex, freq, wait_time, no_cck);
  6847. buf = os_zalloc(24 + data_len);
  6848. if (buf == NULL)
  6849. return ret;
  6850. os_memcpy(buf + 24, data, data_len);
  6851. hdr = (struct ieee80211_hdr *) buf;
  6852. hdr->frame_control =
  6853. IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
  6854. os_memcpy(hdr->addr1, dst, ETH_ALEN);
  6855. os_memcpy(hdr->addr2, src, ETH_ALEN);
  6856. os_memcpy(hdr->addr3, bssid, ETH_ALEN);
  6857. if (is_ap_interface(drv->nlmode))
  6858. ret = wpa_driver_nl80211_send_mlme_freq(priv, buf,
  6859. 24 + data_len,
  6860. 0, freq, no_cck, 1,
  6861. wait_time);
  6862. else
  6863. ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
  6864. 24 + data_len,
  6865. &drv->send_action_cookie,
  6866. no_cck, 0, 1);
  6867. os_free(buf);
  6868. return ret;
  6869. }
  6870. static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
  6871. {
  6872. struct i802_bss *bss = priv;
  6873. struct wpa_driver_nl80211_data *drv = bss->drv;
  6874. struct nl_msg *msg;
  6875. int ret;
  6876. msg = nlmsg_alloc();
  6877. if (!msg)
  6878. return;
  6879. nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
  6880. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  6881. NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
  6882. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6883. msg = NULL;
  6884. if (ret)
  6885. wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
  6886. "(%s)", ret, strerror(-ret));
  6887. nla_put_failure:
  6888. nlmsg_free(msg);
  6889. }
  6890. static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
  6891. unsigned int duration)
  6892. {
  6893. struct i802_bss *bss = priv;
  6894. struct wpa_driver_nl80211_data *drv = bss->drv;
  6895. struct nl_msg *msg;
  6896. int ret;
  6897. u64 cookie;
  6898. msg = nlmsg_alloc();
  6899. if (!msg)
  6900. return -1;
  6901. nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
  6902. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  6903. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
  6904. NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
  6905. cookie = 0;
  6906. ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
  6907. msg = NULL;
  6908. if (ret == 0) {
  6909. wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
  6910. "0x%llx for freq=%u MHz duration=%u",
  6911. (long long unsigned int) cookie, freq, duration);
  6912. drv->remain_on_chan_cookie = cookie;
  6913. drv->pending_remain_on_chan = 1;
  6914. return 0;
  6915. }
  6916. wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
  6917. "(freq=%d duration=%u): %d (%s)",
  6918. freq, duration, ret, strerror(-ret));
  6919. nla_put_failure:
  6920. nlmsg_free(msg);
  6921. return -1;
  6922. }
  6923. static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
  6924. {
  6925. struct i802_bss *bss = priv;
  6926. struct wpa_driver_nl80211_data *drv = bss->drv;
  6927. struct nl_msg *msg;
  6928. int ret;
  6929. if (!drv->pending_remain_on_chan) {
  6930. wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
  6931. "to cancel");
  6932. return -1;
  6933. }
  6934. wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
  6935. "0x%llx",
  6936. (long long unsigned int) drv->remain_on_chan_cookie);
  6937. msg = nlmsg_alloc();
  6938. if (!msg)
  6939. return -1;
  6940. nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
  6941. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  6942. NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
  6943. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6944. msg = NULL;
  6945. if (ret == 0)
  6946. return 0;
  6947. wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
  6948. "%d (%s)", ret, strerror(-ret));
  6949. nla_put_failure:
  6950. nlmsg_free(msg);
  6951. return -1;
  6952. }
  6953. static int wpa_driver_nl80211_probe_req_report(void *priv, int report)
  6954. {
  6955. struct i802_bss *bss = priv;
  6956. struct wpa_driver_nl80211_data *drv = bss->drv;
  6957. if (!report) {
  6958. if (bss->nl_preq && drv->device_ap_sme &&
  6959. is_ap_interface(drv->nlmode)) {
  6960. /*
  6961. * Do not disable Probe Request reporting that was
  6962. * enabled in nl80211_setup_ap().
  6963. */
  6964. wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
  6965. "Probe Request reporting nl_preq=%p while "
  6966. "in AP mode", bss->nl_preq);
  6967. } else if (bss->nl_preq) {
  6968. wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
  6969. "reporting nl_preq=%p", bss->nl_preq);
  6970. eloop_unregister_read_sock(
  6971. nl_socket_get_fd(bss->nl_preq));
  6972. nl_destroy_handles(&bss->nl_preq);
  6973. }
  6974. return 0;
  6975. }
  6976. if (bss->nl_preq) {
  6977. wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
  6978. "already on! nl_preq=%p", bss->nl_preq);
  6979. return 0;
  6980. }
  6981. bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
  6982. if (bss->nl_preq == NULL)
  6983. return -1;
  6984. wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
  6985. "reporting nl_preq=%p", bss->nl_preq);
  6986. if (nl80211_register_frame(bss, bss->nl_preq,
  6987. (WLAN_FC_TYPE_MGMT << 2) |
  6988. (WLAN_FC_STYPE_PROBE_REQ << 4),
  6989. NULL, 0) < 0)
  6990. goto out_err;
  6991. eloop_register_read_sock(nl_socket_get_fd(bss->nl_preq),
  6992. wpa_driver_nl80211_event_receive, bss->nl_cb,
  6993. bss->nl_preq);
  6994. return 0;
  6995. out_err:
  6996. nl_destroy_handles(&bss->nl_preq);
  6997. return -1;
  6998. }
  6999. static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
  7000. int ifindex, int disabled)
  7001. {
  7002. struct nl_msg *msg;
  7003. struct nlattr *bands, *band;
  7004. int ret;
  7005. msg = nlmsg_alloc();
  7006. if (!msg)
  7007. return -1;
  7008. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
  7009. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  7010. bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
  7011. if (!bands)
  7012. goto nla_put_failure;
  7013. /*
  7014. * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
  7015. * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
  7016. * rates. All 5 GHz rates are left enabled.
  7017. */
  7018. band = nla_nest_start(msg, NL80211_BAND_2GHZ);
  7019. if (!band)
  7020. goto nla_put_failure;
  7021. if (disabled) {
  7022. NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
  7023. "\x0c\x12\x18\x24\x30\x48\x60\x6c");
  7024. }
  7025. nla_nest_end(msg, band);
  7026. nla_nest_end(msg, bands);
  7027. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7028. msg = NULL;
  7029. if (ret) {
  7030. wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
  7031. "(%s)", ret, strerror(-ret));
  7032. } else
  7033. drv->disabled_11b_rates = disabled;
  7034. return ret;
  7035. nla_put_failure:
  7036. nlmsg_free(msg);
  7037. return -1;
  7038. }
  7039. static int wpa_driver_nl80211_deinit_ap(void *priv)
  7040. {
  7041. struct i802_bss *bss = priv;
  7042. struct wpa_driver_nl80211_data *drv = bss->drv;
  7043. if (!is_ap_interface(drv->nlmode))
  7044. return -1;
  7045. wpa_driver_nl80211_del_beacon(drv);
  7046. return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
  7047. }
  7048. static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
  7049. {
  7050. struct i802_bss *bss = priv;
  7051. struct wpa_driver_nl80211_data *drv = bss->drv;
  7052. if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
  7053. return -1;
  7054. return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
  7055. }
  7056. static void wpa_driver_nl80211_resume(void *priv)
  7057. {
  7058. struct i802_bss *bss = priv;
  7059. struct wpa_driver_nl80211_data *drv = bss->drv;
  7060. if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
  7061. wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on "
  7062. "resume event");
  7063. }
  7064. }
  7065. static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
  7066. const u8 *ies, size_t ies_len)
  7067. {
  7068. struct i802_bss *bss = priv;
  7069. struct wpa_driver_nl80211_data *drv = bss->drv;
  7070. int ret;
  7071. u8 *data, *pos;
  7072. size_t data_len;
  7073. const u8 *own_addr = bss->addr;
  7074. if (action != 1) {
  7075. wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
  7076. "action %d", action);
  7077. return -1;
  7078. }
  7079. /*
  7080. * Action frame payload:
  7081. * Category[1] = 6 (Fast BSS Transition)
  7082. * Action[1] = 1 (Fast BSS Transition Request)
  7083. * STA Address
  7084. * Target AP Address
  7085. * FT IEs
  7086. */
  7087. data_len = 2 + 2 * ETH_ALEN + ies_len;
  7088. data = os_malloc(data_len);
  7089. if (data == NULL)
  7090. return -1;
  7091. pos = data;
  7092. *pos++ = 0x06; /* FT Action category */
  7093. *pos++ = action;
  7094. os_memcpy(pos, own_addr, ETH_ALEN);
  7095. pos += ETH_ALEN;
  7096. os_memcpy(pos, target_ap, ETH_ALEN);
  7097. pos += ETH_ALEN;
  7098. os_memcpy(pos, ies, ies_len);
  7099. ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
  7100. drv->bssid, own_addr, drv->bssid,
  7101. data, data_len, 0);
  7102. os_free(data);
  7103. return ret;
  7104. }
  7105. static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
  7106. {
  7107. struct i802_bss *bss = priv;
  7108. struct wpa_driver_nl80211_data *drv = bss->drv;
  7109. struct nl_msg *msg, *cqm = NULL;
  7110. int ret = -1;
  7111. wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
  7112. "hysteresis=%d", threshold, hysteresis);
  7113. msg = nlmsg_alloc();
  7114. if (!msg)
  7115. return -1;
  7116. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
  7117. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  7118. cqm = nlmsg_alloc();
  7119. if (cqm == NULL)
  7120. goto nla_put_failure;
  7121. NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
  7122. NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
  7123. if (nla_put_nested(msg, NL80211_ATTR_CQM, cqm) < 0)
  7124. goto nla_put_failure;
  7125. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7126. msg = NULL;
  7127. nla_put_failure:
  7128. nlmsg_free(cqm);
  7129. nlmsg_free(msg);
  7130. return ret;
  7131. }
  7132. static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
  7133. {
  7134. struct i802_bss *bss = priv;
  7135. struct wpa_driver_nl80211_data *drv = bss->drv;
  7136. int res;
  7137. os_memset(si, 0, sizeof(*si));
  7138. res = nl80211_get_link_signal(drv, si);
  7139. if (res != 0)
  7140. return res;
  7141. return nl80211_get_link_noise(drv, si);
  7142. }
  7143. static int wpa_driver_nl80211_shared_freq(void *priv)
  7144. {
  7145. struct i802_bss *bss = priv;
  7146. struct wpa_driver_nl80211_data *drv = bss->drv;
  7147. struct wpa_driver_nl80211_data *driver;
  7148. int freq = 0;
  7149. /*
  7150. * If the same PHY is in connected state with some other interface,
  7151. * then retrieve the assoc freq.
  7152. */
  7153. wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
  7154. drv->phyname);
  7155. dl_list_for_each(driver, &drv->global->interfaces,
  7156. struct wpa_driver_nl80211_data, list) {
  7157. if (drv == driver ||
  7158. os_strcmp(drv->phyname, driver->phyname) != 0 ||
  7159. !driver->associated)
  7160. continue;
  7161. wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
  7162. MACSTR,
  7163. driver->phyname, driver->first_bss.ifname,
  7164. MAC2STR(driver->first_bss.addr));
  7165. if (is_ap_interface(driver->nlmode))
  7166. freq = driver->first_bss.freq;
  7167. else
  7168. freq = nl80211_get_assoc_freq(driver);
  7169. wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
  7170. drv->phyname, freq);
  7171. }
  7172. if (!freq)
  7173. wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
  7174. "PHY (%s) in associated state", drv->phyname);
  7175. return freq;
  7176. }
  7177. static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
  7178. int encrypt)
  7179. {
  7180. struct i802_bss *bss = priv;
  7181. return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
  7182. 0, 0, 0, 0);
  7183. }
  7184. static int nl80211_set_param(void *priv, const char *param)
  7185. {
  7186. wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
  7187. if (param == NULL)
  7188. return 0;
  7189. #ifdef CONFIG_P2P
  7190. if (os_strstr(param, "use_p2p_group_interface=1")) {
  7191. struct i802_bss *bss = priv;
  7192. struct wpa_driver_nl80211_data *drv = bss->drv;
  7193. wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
  7194. "interface");
  7195. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
  7196. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
  7197. }
  7198. #endif /* CONFIG_P2P */
  7199. return 0;
  7200. }
  7201. static void * nl80211_global_init(void)
  7202. {
  7203. struct nl80211_global *global;
  7204. struct netlink_config *cfg;
  7205. global = os_zalloc(sizeof(*global));
  7206. if (global == NULL)
  7207. return NULL;
  7208. global->ioctl_sock = -1;
  7209. dl_list_init(&global->interfaces);
  7210. global->if_add_ifindex = -1;
  7211. cfg = os_zalloc(sizeof(*cfg));
  7212. if (cfg == NULL)
  7213. goto err;
  7214. cfg->ctx = global;
  7215. cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
  7216. cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
  7217. global->netlink = netlink_init(cfg);
  7218. if (global->netlink == NULL) {
  7219. os_free(cfg);
  7220. goto err;
  7221. }
  7222. if (wpa_driver_nl80211_init_nl_global(global) < 0)
  7223. goto err;
  7224. global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
  7225. if (global->ioctl_sock < 0) {
  7226. perror("socket(PF_INET,SOCK_DGRAM)");
  7227. goto err;
  7228. }
  7229. return global;
  7230. err:
  7231. nl80211_global_deinit(global);
  7232. return NULL;
  7233. }
  7234. static void nl80211_global_deinit(void *priv)
  7235. {
  7236. struct nl80211_global *global = priv;
  7237. if (global == NULL)
  7238. return;
  7239. if (!dl_list_empty(&global->interfaces)) {
  7240. wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
  7241. "nl80211_global_deinit",
  7242. dl_list_len(&global->interfaces));
  7243. }
  7244. if (global->netlink)
  7245. netlink_deinit(global->netlink);
  7246. nl_destroy_handles(&global->nl);
  7247. if (global->nl_event) {
  7248. eloop_unregister_read_sock(
  7249. nl_socket_get_fd(global->nl_event));
  7250. nl_destroy_handles(&global->nl_event);
  7251. }
  7252. nl_cb_put(global->nl_cb);
  7253. if (global->ioctl_sock >= 0)
  7254. close(global->ioctl_sock);
  7255. os_free(global);
  7256. }
  7257. static const char * nl80211_get_radio_name(void *priv)
  7258. {
  7259. struct i802_bss *bss = priv;
  7260. struct wpa_driver_nl80211_data *drv = bss->drv;
  7261. return drv->phyname;
  7262. }
  7263. static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
  7264. const u8 *pmkid)
  7265. {
  7266. struct nl_msg *msg;
  7267. msg = nlmsg_alloc();
  7268. if (!msg)
  7269. return -ENOMEM;
  7270. nl80211_cmd(bss->drv, msg, 0, cmd);
  7271. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  7272. if (pmkid)
  7273. NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
  7274. if (bssid)
  7275. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
  7276. return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  7277. nla_put_failure:
  7278. nlmsg_free(msg);
  7279. return -ENOBUFS;
  7280. }
  7281. static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
  7282. {
  7283. struct i802_bss *bss = priv;
  7284. wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
  7285. return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
  7286. }
  7287. static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
  7288. {
  7289. struct i802_bss *bss = priv;
  7290. wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
  7291. MAC2STR(bssid));
  7292. return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
  7293. }
  7294. static int nl80211_flush_pmkid(void *priv)
  7295. {
  7296. struct i802_bss *bss = priv;
  7297. wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
  7298. return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
  7299. }
  7300. static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
  7301. const u8 *replay_ctr)
  7302. {
  7303. struct i802_bss *bss = priv;
  7304. struct wpa_driver_nl80211_data *drv = bss->drv;
  7305. struct nlattr *replay_nested;
  7306. struct nl_msg *msg;
  7307. msg = nlmsg_alloc();
  7308. if (!msg)
  7309. return;
  7310. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
  7311. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  7312. replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
  7313. if (!replay_nested)
  7314. goto nla_put_failure;
  7315. NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
  7316. NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
  7317. NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
  7318. replay_ctr);
  7319. nla_nest_end(msg, replay_nested);
  7320. send_and_recv_msgs(drv, msg, NULL, NULL);
  7321. return;
  7322. nla_put_failure:
  7323. nlmsg_free(msg);
  7324. }
  7325. static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
  7326. const u8 *addr, int qos)
  7327. {
  7328. /* send data frame to poll STA and check whether
  7329. * this frame is ACKed */
  7330. struct {
  7331. struct ieee80211_hdr hdr;
  7332. u16 qos_ctl;
  7333. } STRUCT_PACKED nulldata;
  7334. size_t size;
  7335. /* Send data frame to poll STA and check whether this frame is ACKed */
  7336. os_memset(&nulldata, 0, sizeof(nulldata));
  7337. if (qos) {
  7338. nulldata.hdr.frame_control =
  7339. IEEE80211_FC(WLAN_FC_TYPE_DATA,
  7340. WLAN_FC_STYPE_QOS_NULL);
  7341. size = sizeof(nulldata);
  7342. } else {
  7343. nulldata.hdr.frame_control =
  7344. IEEE80211_FC(WLAN_FC_TYPE_DATA,
  7345. WLAN_FC_STYPE_NULLFUNC);
  7346. size = sizeof(struct ieee80211_hdr);
  7347. }
  7348. nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
  7349. os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
  7350. os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
  7351. os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
  7352. if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0) < 0)
  7353. wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
  7354. "send poll frame");
  7355. }
  7356. static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
  7357. int qos)
  7358. {
  7359. struct i802_bss *bss = priv;
  7360. struct wpa_driver_nl80211_data *drv = bss->drv;
  7361. struct nl_msg *msg;
  7362. if (!drv->poll_command_supported) {
  7363. nl80211_send_null_frame(bss, own_addr, addr, qos);
  7364. return;
  7365. }
  7366. msg = nlmsg_alloc();
  7367. if (!msg)
  7368. return;
  7369. nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
  7370. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  7371. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  7372. send_and_recv_msgs(drv, msg, NULL, NULL);
  7373. return;
  7374. nla_put_failure:
  7375. nlmsg_free(msg);
  7376. }
  7377. static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
  7378. {
  7379. struct nl_msg *msg;
  7380. msg = nlmsg_alloc();
  7381. if (!msg)
  7382. return -ENOMEM;
  7383. nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
  7384. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  7385. NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
  7386. enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
  7387. return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  7388. nla_put_failure:
  7389. nlmsg_free(msg);
  7390. return -ENOBUFS;
  7391. }
  7392. static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
  7393. int ctwindow)
  7394. {
  7395. struct i802_bss *bss = priv;
  7396. wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
  7397. "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
  7398. if (opp_ps != -1 || ctwindow != -1)
  7399. return -1; /* Not yet supported */
  7400. if (legacy_ps == -1)
  7401. return 0;
  7402. if (legacy_ps != 0 && legacy_ps != 1)
  7403. return -1; /* Not yet supported */
  7404. return nl80211_set_power_save(bss, legacy_ps);
  7405. }
  7406. #ifdef CONFIG_TDLS
  7407. static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
  7408. u8 dialog_token, u16 status_code,
  7409. const u8 *buf, size_t len)
  7410. {
  7411. struct i802_bss *bss = priv;
  7412. struct wpa_driver_nl80211_data *drv = bss->drv;
  7413. struct nl_msg *msg;
  7414. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
  7415. return -EOPNOTSUPP;
  7416. if (!dst)
  7417. return -EINVAL;
  7418. msg = nlmsg_alloc();
  7419. if (!msg)
  7420. return -ENOMEM;
  7421. nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
  7422. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  7423. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
  7424. NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
  7425. NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
  7426. NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
  7427. NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
  7428. return send_and_recv_msgs(drv, msg, NULL, NULL);
  7429. nla_put_failure:
  7430. nlmsg_free(msg);
  7431. return -ENOBUFS;
  7432. }
  7433. static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
  7434. {
  7435. struct i802_bss *bss = priv;
  7436. struct wpa_driver_nl80211_data *drv = bss->drv;
  7437. struct nl_msg *msg;
  7438. enum nl80211_tdls_operation nl80211_oper;
  7439. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
  7440. return -EOPNOTSUPP;
  7441. switch (oper) {
  7442. case TDLS_DISCOVERY_REQ:
  7443. nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
  7444. break;
  7445. case TDLS_SETUP:
  7446. nl80211_oper = NL80211_TDLS_SETUP;
  7447. break;
  7448. case TDLS_TEARDOWN:
  7449. nl80211_oper = NL80211_TDLS_TEARDOWN;
  7450. break;
  7451. case TDLS_ENABLE_LINK:
  7452. nl80211_oper = NL80211_TDLS_ENABLE_LINK;
  7453. break;
  7454. case TDLS_DISABLE_LINK:
  7455. nl80211_oper = NL80211_TDLS_DISABLE_LINK;
  7456. break;
  7457. case TDLS_ENABLE:
  7458. return 0;
  7459. case TDLS_DISABLE:
  7460. return 0;
  7461. default:
  7462. return -EINVAL;
  7463. }
  7464. msg = nlmsg_alloc();
  7465. if (!msg)
  7466. return -ENOMEM;
  7467. nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
  7468. NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
  7469. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  7470. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
  7471. return send_and_recv_msgs(drv, msg, NULL, NULL);
  7472. nla_put_failure:
  7473. nlmsg_free(msg);
  7474. return -ENOBUFS;
  7475. }
  7476. #endif /* CONFIG TDLS */
  7477. #ifdef ANDROID
  7478. typedef struct android_wifi_priv_cmd {
  7479. char *buf;
  7480. int used_len;
  7481. int total_len;
  7482. } android_wifi_priv_cmd;
  7483. static int drv_errors = 0;
  7484. static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
  7485. {
  7486. drv_errors++;
  7487. if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
  7488. drv_errors = 0;
  7489. wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
  7490. }
  7491. }
  7492. static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
  7493. {
  7494. struct wpa_driver_nl80211_data *drv = bss->drv;
  7495. struct ifreq ifr;
  7496. android_wifi_priv_cmd priv_cmd;
  7497. char buf[MAX_DRV_CMD_SIZE];
  7498. int ret;
  7499. os_memset(&ifr, 0, sizeof(ifr));
  7500. os_memset(&priv_cmd, 0, sizeof(priv_cmd));
  7501. os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
  7502. os_memset(buf, 0, sizeof(buf));
  7503. os_strlcpy(buf, cmd, sizeof(buf));
  7504. priv_cmd.buf = buf;
  7505. priv_cmd.used_len = sizeof(buf);
  7506. priv_cmd.total_len = sizeof(buf);
  7507. ifr.ifr_data = &priv_cmd;
  7508. ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
  7509. if (ret < 0) {
  7510. wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
  7511. __func__);
  7512. wpa_driver_send_hang_msg(drv);
  7513. return ret;
  7514. }
  7515. drv_errors = 0;
  7516. return 0;
  7517. }
  7518. static int android_pno_start(struct i802_bss *bss,
  7519. struct wpa_driver_scan_params *params)
  7520. {
  7521. struct wpa_driver_nl80211_data *drv = bss->drv;
  7522. struct ifreq ifr;
  7523. android_wifi_priv_cmd priv_cmd;
  7524. int ret = 0, i = 0, bp;
  7525. char buf[WEXT_PNO_MAX_COMMAND_SIZE];
  7526. bp = WEXT_PNOSETUP_HEADER_SIZE;
  7527. os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
  7528. buf[bp++] = WEXT_PNO_TLV_PREFIX;
  7529. buf[bp++] = WEXT_PNO_TLV_VERSION;
  7530. buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
  7531. buf[bp++] = WEXT_PNO_TLV_RESERVED;
  7532. while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
  7533. /* Check that there is enough space needed for 1 more SSID, the
  7534. * other sections and null termination */
  7535. if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
  7536. WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
  7537. break;
  7538. wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
  7539. params->ssids[i].ssid,
  7540. params->ssids[i].ssid_len);
  7541. buf[bp++] = WEXT_PNO_SSID_SECTION;
  7542. buf[bp++] = params->ssids[i].ssid_len;
  7543. os_memcpy(&buf[bp], params->ssids[i].ssid,
  7544. params->ssids[i].ssid_len);
  7545. bp += params->ssids[i].ssid_len;
  7546. i++;
  7547. }
  7548. buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
  7549. os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
  7550. WEXT_PNO_SCAN_INTERVAL);
  7551. bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
  7552. buf[bp++] = WEXT_PNO_REPEAT_SECTION;
  7553. os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
  7554. WEXT_PNO_REPEAT);
  7555. bp += WEXT_PNO_REPEAT_LENGTH;
  7556. buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
  7557. os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
  7558. WEXT_PNO_MAX_REPEAT);
  7559. bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
  7560. memset(&ifr, 0, sizeof(ifr));
  7561. memset(&priv_cmd, 0, sizeof(priv_cmd));
  7562. os_strncpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
  7563. priv_cmd.buf = buf;
  7564. priv_cmd.used_len = bp;
  7565. priv_cmd.total_len = bp;
  7566. ifr.ifr_data = &priv_cmd;
  7567. ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
  7568. if (ret < 0) {
  7569. wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
  7570. ret);
  7571. wpa_driver_send_hang_msg(drv);
  7572. return ret;
  7573. }
  7574. drv_errors = 0;
  7575. return android_priv_cmd(bss, "PNOFORCE 1");
  7576. }
  7577. static int android_pno_stop(struct i802_bss *bss)
  7578. {
  7579. return android_priv_cmd(bss, "PNOFORCE 0");
  7580. }
  7581. #endif /* ANDROID */
  7582. const struct wpa_driver_ops wpa_driver_nl80211_ops = {
  7583. .name = "nl80211",
  7584. .desc = "Linux nl80211/cfg80211",
  7585. .get_bssid = wpa_driver_nl80211_get_bssid,
  7586. .get_ssid = wpa_driver_nl80211_get_ssid,
  7587. .set_key = wpa_driver_nl80211_set_key,
  7588. .scan2 = wpa_driver_nl80211_scan,
  7589. .sched_scan = wpa_driver_nl80211_sched_scan,
  7590. .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
  7591. .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
  7592. .deauthenticate = wpa_driver_nl80211_deauthenticate,
  7593. .disassociate = wpa_driver_nl80211_disassociate,
  7594. .authenticate = wpa_driver_nl80211_authenticate,
  7595. .associate = wpa_driver_nl80211_associate,
  7596. .global_init = nl80211_global_init,
  7597. .global_deinit = nl80211_global_deinit,
  7598. .init2 = wpa_driver_nl80211_init,
  7599. .deinit = wpa_driver_nl80211_deinit,
  7600. .get_capa = wpa_driver_nl80211_get_capa,
  7601. .set_operstate = wpa_driver_nl80211_set_operstate,
  7602. .set_supp_port = wpa_driver_nl80211_set_supp_port,
  7603. .set_country = wpa_driver_nl80211_set_country,
  7604. .set_ap = wpa_driver_nl80211_set_ap,
  7605. .if_add = wpa_driver_nl80211_if_add,
  7606. .if_remove = wpa_driver_nl80211_if_remove,
  7607. .send_mlme = wpa_driver_nl80211_send_mlme,
  7608. .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
  7609. .sta_add = wpa_driver_nl80211_sta_add,
  7610. .sta_remove = wpa_driver_nl80211_sta_remove,
  7611. .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
  7612. .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
  7613. #ifdef HOSTAPD
  7614. .hapd_init = i802_init,
  7615. .hapd_deinit = i802_deinit,
  7616. .set_wds_sta = i802_set_wds_sta,
  7617. #endif /* HOSTAPD */
  7618. #if defined(HOSTAPD) || defined(CONFIG_AP)
  7619. .get_seqnum = i802_get_seqnum,
  7620. .flush = i802_flush,
  7621. .get_inact_sec = i802_get_inact_sec,
  7622. .sta_clear_stats = i802_sta_clear_stats,
  7623. .set_rts = i802_set_rts,
  7624. .set_frag = i802_set_frag,
  7625. .set_tx_queue_params = i802_set_tx_queue_params,
  7626. .set_sta_vlan = i802_set_sta_vlan,
  7627. .sta_deauth = i802_sta_deauth,
  7628. .sta_disassoc = i802_sta_disassoc,
  7629. #endif /* HOSTAPD || CONFIG_AP */
  7630. .read_sta_data = i802_read_sta_data,
  7631. .set_freq = i802_set_freq,
  7632. .send_action = wpa_driver_nl80211_send_action,
  7633. .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
  7634. .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
  7635. .cancel_remain_on_channel =
  7636. wpa_driver_nl80211_cancel_remain_on_channel,
  7637. .probe_req_report = wpa_driver_nl80211_probe_req_report,
  7638. .deinit_ap = wpa_driver_nl80211_deinit_ap,
  7639. .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
  7640. .resume = wpa_driver_nl80211_resume,
  7641. .send_ft_action = nl80211_send_ft_action,
  7642. .signal_monitor = nl80211_signal_monitor,
  7643. .signal_poll = nl80211_signal_poll,
  7644. .send_frame = nl80211_send_frame,
  7645. .shared_freq = wpa_driver_nl80211_shared_freq,
  7646. .set_param = nl80211_set_param,
  7647. .get_radio_name = nl80211_get_radio_name,
  7648. .add_pmkid = nl80211_add_pmkid,
  7649. .remove_pmkid = nl80211_remove_pmkid,
  7650. .flush_pmkid = nl80211_flush_pmkid,
  7651. .set_rekey_info = nl80211_set_rekey_info,
  7652. .poll_client = nl80211_poll_client,
  7653. .set_p2p_powersave = nl80211_set_p2p_powersave,
  7654. #ifdef CONFIG_TDLS
  7655. .send_tdls_mgmt = nl80211_send_tdls_mgmt,
  7656. .tdls_oper = nl80211_tdls_oper,
  7657. #endif /* CONFIG_TDLS */
  7658. };