driver_nl80211.c 166 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455
  1. /*
  2. * Driver interaction with Linux nl80211/cfg80211
  3. * Copyright (c) 2002-2010, 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 program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * Alternatively, this software may be distributed under the terms of BSD
  14. * license.
  15. *
  16. * See README and COPYING for more details.
  17. */
  18. #include "includes.h"
  19. #include <sys/ioctl.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #include <net/if.h>
  24. #include <netlink/genl/genl.h>
  25. #include <netlink/genl/family.h>
  26. #include <netlink/genl/ctrl.h>
  27. #include <linux/rtnetlink.h>
  28. #include <netpacket/packet.h>
  29. #include <linux/filter.h>
  30. #include "nl80211_copy.h"
  31. #include "common.h"
  32. #include "eloop.h"
  33. #include "utils/list.h"
  34. #include "common/ieee802_11_defs.h"
  35. #include "netlink.h"
  36. #include "linux_ioctl.h"
  37. #include "radiotap.h"
  38. #include "radiotap_iter.h"
  39. #include "rfkill.h"
  40. #include "driver.h"
  41. #ifdef CONFIG_LIBNL20
  42. /* libnl 2.0 compatibility code */
  43. #define nl_handle nl_sock
  44. #define nl80211_handle_alloc nl_socket_alloc_cb
  45. #define nl80211_handle_destroy nl_socket_free
  46. #else
  47. /*
  48. * libnl 1.1 has a bug, it tries to allocate socket numbers densely
  49. * but when you free a socket again it will mess up its bitmap and
  50. * and use the wrong number the next time it needs a socket ID.
  51. * Therefore, we wrap the handle alloc/destroy and add our own pid
  52. * accounting.
  53. */
  54. static uint32_t port_bitmap[32] = { 0 };
  55. static struct nl_handle *nl80211_handle_alloc(void *cb)
  56. {
  57. struct nl_handle *handle;
  58. uint32_t pid = getpid() & 0x3FFFFF;
  59. int i;
  60. handle = nl_handle_alloc_cb(cb);
  61. for (i = 0; i < 1024; i++) {
  62. if (port_bitmap[i / 32] & (1 << (i % 32)))
  63. continue;
  64. port_bitmap[i / 32] |= 1 << (i % 32);
  65. pid += i << 22;
  66. break;
  67. }
  68. nl_socket_set_local_port(handle, pid);
  69. return handle;
  70. }
  71. static void nl80211_handle_destroy(struct nl_handle *handle)
  72. {
  73. uint32_t port = nl_socket_get_local_port(handle);
  74. port >>= 22;
  75. port_bitmap[port / 32] &= ~(1 << (port % 32));
  76. nl_handle_destroy(handle);
  77. }
  78. #endif /* CONFIG_LIBNL20 */
  79. #ifndef IFF_LOWER_UP
  80. #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
  81. #endif
  82. #ifndef IFF_DORMANT
  83. #define IFF_DORMANT 0x20000 /* driver signals dormant */
  84. #endif
  85. #ifndef IF_OPER_DORMANT
  86. #define IF_OPER_DORMANT 5
  87. #endif
  88. #ifndef IF_OPER_UP
  89. #define IF_OPER_UP 6
  90. #endif
  91. struct nl80211_global {
  92. struct dl_list interfaces;
  93. };
  94. struct i802_bss {
  95. struct wpa_driver_nl80211_data *drv;
  96. struct i802_bss *next;
  97. int ifindex;
  98. char ifname[IFNAMSIZ + 1];
  99. unsigned int beacon_set:1;
  100. };
  101. struct wpa_driver_nl80211_data {
  102. struct nl80211_global *global;
  103. struct dl_list list;
  104. u8 addr[ETH_ALEN];
  105. char phyname[32];
  106. void *ctx;
  107. struct netlink_data *netlink;
  108. int ioctl_sock; /* socket for ioctl() use */
  109. char brname[IFNAMSIZ];
  110. int ifindex;
  111. int if_removed;
  112. int if_disabled;
  113. struct rfkill_data *rfkill;
  114. struct wpa_driver_capa capa;
  115. int has_capability;
  116. int operstate;
  117. int scan_complete_events;
  118. struct nl_handle *nl_handle;
  119. struct nl_handle *nl_handle_event;
  120. struct nl_handle *nl_handle_preq;
  121. struct nl_cache *nl_cache;
  122. struct nl_cache *nl_cache_event;
  123. struct nl_cache *nl_cache_preq;
  124. struct nl_cb *nl_cb;
  125. struct genl_family *nl80211;
  126. u8 auth_bssid[ETH_ALEN];
  127. u8 bssid[ETH_ALEN];
  128. int associated;
  129. u8 ssid[32];
  130. size_t ssid_len;
  131. int nlmode;
  132. int ap_scan_as_station;
  133. unsigned int assoc_freq;
  134. int monitor_sock;
  135. int monitor_ifidx;
  136. int disable_11b_rates;
  137. unsigned int pending_remain_on_chan:1;
  138. unsigned int added_bridge:1;
  139. unsigned int added_if_into_bridge:1;
  140. u64 remain_on_chan_cookie;
  141. u64 send_action_cookie;
  142. unsigned int last_mgmt_freq;
  143. struct wpa_driver_scan_filter *filter_ssids;
  144. size_t num_filter_ssids;
  145. struct i802_bss first_bss;
  146. #ifdef HOSTAPD
  147. int eapol_sock; /* socket for EAPOL frames */
  148. int default_if_indices[16];
  149. int *if_indices;
  150. int num_if_indices;
  151. int last_freq;
  152. int last_freq_ht;
  153. #endif /* HOSTAPD */
  154. };
  155. static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
  156. void *timeout_ctx);
  157. static int wpa_driver_nl80211_set_mode(void *priv, int mode);
  158. static int
  159. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
  160. static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
  161. const u8 *addr, int cmd, u16 reason_code,
  162. int local_state_change);
  163. static void nl80211_remove_monitor_interface(
  164. struct wpa_driver_nl80211_data *drv);
  165. static int nl80211_send_frame_cmd(struct wpa_driver_nl80211_data *drv,
  166. unsigned int freq, unsigned int wait,
  167. const u8 *buf, size_t buf_len, u64 *cookie);
  168. static int wpa_driver_nl80211_probe_req_report(void *priv, int report);
  169. #ifdef HOSTAPD
  170. static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  171. static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  172. static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  173. static int wpa_driver_nl80211_if_remove(void *priv,
  174. enum wpa_driver_if_type type,
  175. const char *ifname);
  176. #else /* HOSTAPD */
  177. static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  178. {
  179. return 0;
  180. }
  181. #endif /* HOSTAPD */
  182. static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
  183. static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
  184. int ifindex, int disabled);
  185. /* nl80211 code */
  186. static int ack_handler(struct nl_msg *msg, void *arg)
  187. {
  188. int *err = arg;
  189. *err = 0;
  190. return NL_STOP;
  191. }
  192. static int finish_handler(struct nl_msg *msg, void *arg)
  193. {
  194. int *ret = arg;
  195. *ret = 0;
  196. return NL_SKIP;
  197. }
  198. static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
  199. void *arg)
  200. {
  201. int *ret = arg;
  202. *ret = err->error;
  203. return NL_SKIP;
  204. }
  205. static int no_seq_check(struct nl_msg *msg, void *arg)
  206. {
  207. return NL_OK;
  208. }
  209. static int send_and_recv(struct wpa_driver_nl80211_data *drv,
  210. struct nl_handle *nl_handle, struct nl_msg *msg,
  211. int (*valid_handler)(struct nl_msg *, void *),
  212. void *valid_data)
  213. {
  214. struct nl_cb *cb;
  215. int err = -ENOMEM;
  216. cb = nl_cb_clone(drv->nl_cb);
  217. if (!cb)
  218. goto out;
  219. err = nl_send_auto_complete(nl_handle, msg);
  220. if (err < 0)
  221. goto out;
  222. err = 1;
  223. nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
  224. nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
  225. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
  226. if (valid_handler)
  227. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
  228. valid_handler, valid_data);
  229. while (err > 0)
  230. nl_recvmsgs(nl_handle, cb);
  231. out:
  232. nl_cb_put(cb);
  233. nlmsg_free(msg);
  234. return err;
  235. }
  236. static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
  237. struct nl_msg *msg,
  238. int (*valid_handler)(struct nl_msg *, void *),
  239. void *valid_data)
  240. {
  241. return send_and_recv(drv, drv->nl_handle, msg, valid_handler,
  242. valid_data);
  243. }
  244. struct family_data {
  245. const char *group;
  246. int id;
  247. };
  248. static int family_handler(struct nl_msg *msg, void *arg)
  249. {
  250. struct family_data *res = arg;
  251. struct nlattr *tb[CTRL_ATTR_MAX + 1];
  252. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  253. struct nlattr *mcgrp;
  254. int i;
  255. nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  256. genlmsg_attrlen(gnlh, 0), NULL);
  257. if (!tb[CTRL_ATTR_MCAST_GROUPS])
  258. return NL_SKIP;
  259. nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
  260. struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
  261. nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
  262. nla_len(mcgrp), NULL);
  263. if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
  264. !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
  265. os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
  266. res->group,
  267. nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
  268. continue;
  269. res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
  270. break;
  271. };
  272. return NL_SKIP;
  273. }
  274. static int nl_get_multicast_id(struct wpa_driver_nl80211_data *drv,
  275. const char *family, const char *group)
  276. {
  277. struct nl_msg *msg;
  278. int ret = -1;
  279. struct family_data res = { group, -ENOENT };
  280. msg = nlmsg_alloc();
  281. if (!msg)
  282. return -ENOMEM;
  283. genlmsg_put(msg, 0, 0, genl_ctrl_resolve(drv->nl_handle, "nlctrl"),
  284. 0, 0, CTRL_CMD_GETFAMILY, 0);
  285. NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
  286. ret = send_and_recv_msgs(drv, msg, family_handler, &res);
  287. msg = NULL;
  288. if (ret == 0)
  289. ret = res.id;
  290. nla_put_failure:
  291. nlmsg_free(msg);
  292. return ret;
  293. }
  294. static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
  295. {
  296. struct i802_bss *bss = priv;
  297. struct wpa_driver_nl80211_data *drv = bss->drv;
  298. if (!drv->associated)
  299. return -1;
  300. os_memcpy(bssid, drv->bssid, ETH_ALEN);
  301. return 0;
  302. }
  303. static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
  304. {
  305. struct i802_bss *bss = priv;
  306. struct wpa_driver_nl80211_data *drv = bss->drv;
  307. if (!drv->associated)
  308. return -1;
  309. os_memcpy(ssid, drv->ssid, drv->ssid_len);
  310. return drv->ssid_len;
  311. }
  312. static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
  313. char *buf, size_t len, int del)
  314. {
  315. union wpa_event_data event;
  316. os_memset(&event, 0, sizeof(event));
  317. if (len > sizeof(event.interface_status.ifname))
  318. len = sizeof(event.interface_status.ifname) - 1;
  319. os_memcpy(event.interface_status.ifname, buf, len);
  320. event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
  321. EVENT_INTERFACE_ADDED;
  322. wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
  323. del ? "DEL" : "NEW",
  324. event.interface_status.ifname,
  325. del ? "removed" : "added");
  326. if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) {
  327. if (del)
  328. drv->if_removed = 1;
  329. else
  330. drv->if_removed = 0;
  331. }
  332. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
  333. }
  334. static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
  335. u8 *buf, size_t len)
  336. {
  337. int attrlen, rta_len;
  338. struct rtattr *attr;
  339. attrlen = len;
  340. attr = (struct rtattr *) buf;
  341. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  342. while (RTA_OK(attr, attrlen)) {
  343. if (attr->rta_type == IFLA_IFNAME) {
  344. if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname)
  345. == 0)
  346. return 1;
  347. else
  348. break;
  349. }
  350. attr = RTA_NEXT(attr, attrlen);
  351. }
  352. return 0;
  353. }
  354. static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
  355. int ifindex, u8 *buf, size_t len)
  356. {
  357. if (drv->ifindex == ifindex)
  358. return 1;
  359. if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
  360. drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname);
  361. wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
  362. "interface");
  363. wpa_driver_nl80211_finish_drv_init(drv);
  364. return 1;
  365. }
  366. return 0;
  367. }
  368. static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
  369. struct ifinfomsg *ifi,
  370. u8 *buf, size_t len)
  371. {
  372. struct wpa_driver_nl80211_data *drv = ctx;
  373. int attrlen, rta_len;
  374. struct rtattr *attr;
  375. u32 brid = 0;
  376. if (!wpa_driver_nl80211_own_ifindex(drv, ifi->ifi_index, buf, len) &&
  377. !have_ifidx(drv, ifi->ifi_index)) {
  378. wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
  379. "ifindex %d", ifi->ifi_index);
  380. return;
  381. }
  382. wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
  383. "(%s%s%s%s)",
  384. drv->operstate, ifi->ifi_flags,
  385. (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
  386. (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
  387. (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
  388. (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
  389. if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
  390. wpa_printf(MSG_DEBUG, "nl80211: Interface down");
  391. drv->if_disabled = 1;
  392. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_DISABLED, NULL);
  393. }
  394. if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
  395. wpa_printf(MSG_DEBUG, "nl80211: Interface up");
  396. drv->if_disabled = 0;
  397. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
  398. }
  399. /*
  400. * Some drivers send the association event before the operup event--in
  401. * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
  402. * fails. This will hit us when wpa_supplicant does not need to do
  403. * IEEE 802.1X authentication
  404. */
  405. if (drv->operstate == 1 &&
  406. (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
  407. !(ifi->ifi_flags & IFF_RUNNING))
  408. netlink_send_oper_ifla(drv->netlink, drv->ifindex,
  409. -1, IF_OPER_UP);
  410. attrlen = len;
  411. attr = (struct rtattr *) buf;
  412. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  413. while (RTA_OK(attr, attrlen)) {
  414. if (attr->rta_type == IFLA_IFNAME) {
  415. wpa_driver_nl80211_event_link(
  416. drv,
  417. ((char *) attr) + rta_len,
  418. attr->rta_len - rta_len, 0);
  419. } else if (attr->rta_type == IFLA_MASTER)
  420. brid = nla_get_u32((struct nlattr *) attr);
  421. attr = RTA_NEXT(attr, attrlen);
  422. }
  423. #ifdef HOSTAPD
  424. if (ifi->ifi_family == AF_BRIDGE && brid) {
  425. /* device has been added to bridge */
  426. char namebuf[IFNAMSIZ];
  427. if_indextoname(brid, namebuf);
  428. wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
  429. brid, namebuf);
  430. add_ifidx(drv, brid);
  431. }
  432. #endif /* HOSTAPD */
  433. }
  434. static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
  435. struct ifinfomsg *ifi,
  436. u8 *buf, size_t len)
  437. {
  438. struct wpa_driver_nl80211_data *drv = ctx;
  439. int attrlen, rta_len;
  440. struct rtattr *attr;
  441. u32 brid = 0;
  442. attrlen = len;
  443. attr = (struct rtattr *) buf;
  444. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  445. while (RTA_OK(attr, attrlen)) {
  446. if (attr->rta_type == IFLA_IFNAME) {
  447. wpa_driver_nl80211_event_link(
  448. drv,
  449. ((char *) attr) + rta_len,
  450. attr->rta_len - rta_len, 1);
  451. } else if (attr->rta_type == IFLA_MASTER)
  452. brid = nla_get_u32((struct nlattr *) attr);
  453. attr = RTA_NEXT(attr, attrlen);
  454. }
  455. #ifdef HOSTAPD
  456. if (ifi->ifi_family == AF_BRIDGE && brid) {
  457. /* device has been removed from bridge */
  458. char namebuf[IFNAMSIZ];
  459. if_indextoname(brid, namebuf);
  460. wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
  461. "%s", brid, namebuf);
  462. del_ifidx(drv, brid);
  463. }
  464. #endif /* HOSTAPD */
  465. }
  466. static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
  467. const u8 *frame, size_t len)
  468. {
  469. const struct ieee80211_mgmt *mgmt;
  470. union wpa_event_data event;
  471. mgmt = (const struct ieee80211_mgmt *) frame;
  472. if (len < 24 + sizeof(mgmt->u.auth)) {
  473. wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
  474. "frame");
  475. return;
  476. }
  477. os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
  478. os_memset(&event, 0, sizeof(event));
  479. os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
  480. event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
  481. event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
  482. if (len > 24 + sizeof(mgmt->u.auth)) {
  483. event.auth.ies = mgmt->u.auth.variable;
  484. event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
  485. }
  486. wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
  487. }
  488. static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
  489. const u8 *frame, size_t len)
  490. {
  491. const struct ieee80211_mgmt *mgmt;
  492. union wpa_event_data event;
  493. u16 status;
  494. mgmt = (const struct ieee80211_mgmt *) frame;
  495. if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
  496. wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
  497. "frame");
  498. return;
  499. }
  500. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  501. if (status != WLAN_STATUS_SUCCESS) {
  502. os_memset(&event, 0, sizeof(event));
  503. event.assoc_reject.bssid = mgmt->bssid;
  504. if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
  505. event.assoc_reject.resp_ies =
  506. (u8 *) mgmt->u.assoc_resp.variable;
  507. event.assoc_reject.resp_ies_len =
  508. len - 24 - sizeof(mgmt->u.assoc_resp);
  509. }
  510. event.assoc_reject.status_code = status;
  511. wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
  512. return;
  513. }
  514. drv->associated = 1;
  515. os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
  516. os_memset(&event, 0, sizeof(event));
  517. if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
  518. event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
  519. event.assoc_info.resp_ies_len =
  520. len - 24 - sizeof(mgmt->u.assoc_resp);
  521. }
  522. event.assoc_info.freq = drv->assoc_freq;
  523. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
  524. }
  525. static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
  526. enum nl80211_commands cmd, struct nlattr *status,
  527. struct nlattr *addr, struct nlattr *req_ie,
  528. struct nlattr *resp_ie)
  529. {
  530. union wpa_event_data event;
  531. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  532. /*
  533. * Avoid reporting two association events that would confuse
  534. * the core code.
  535. */
  536. wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
  537. "when using userspace SME", cmd);
  538. return;
  539. }
  540. os_memset(&event, 0, sizeof(event));
  541. if (cmd == NL80211_CMD_CONNECT &&
  542. nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
  543. if (addr)
  544. event.assoc_reject.bssid = nla_data(addr);
  545. if (resp_ie) {
  546. event.assoc_reject.resp_ies = nla_data(resp_ie);
  547. event.assoc_reject.resp_ies_len = nla_len(resp_ie);
  548. }
  549. event.assoc_reject.status_code = nla_get_u16(status);
  550. wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
  551. return;
  552. }
  553. drv->associated = 1;
  554. if (addr)
  555. os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
  556. if (req_ie) {
  557. event.assoc_info.req_ies = nla_data(req_ie);
  558. event.assoc_info.req_ies_len = nla_len(req_ie);
  559. }
  560. if (resp_ie) {
  561. event.assoc_info.resp_ies = nla_data(resp_ie);
  562. event.assoc_info.resp_ies_len = nla_len(resp_ie);
  563. }
  564. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
  565. }
  566. static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
  567. enum nl80211_commands cmd, struct nlattr *addr)
  568. {
  569. union wpa_event_data event;
  570. enum wpa_event_type ev;
  571. if (nla_len(addr) != ETH_ALEN)
  572. return;
  573. wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
  574. cmd, MAC2STR((u8 *) nla_data(addr)));
  575. if (cmd == NL80211_CMD_AUTHENTICATE)
  576. ev = EVENT_AUTH_TIMED_OUT;
  577. else if (cmd == NL80211_CMD_ASSOCIATE)
  578. ev = EVENT_ASSOC_TIMED_OUT;
  579. else
  580. return;
  581. os_memset(&event, 0, sizeof(event));
  582. os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
  583. wpa_supplicant_event(drv->ctx, ev, &event);
  584. }
  585. static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
  586. struct nlattr *freq, const u8 *frame, size_t len)
  587. {
  588. const struct ieee80211_mgmt *mgmt;
  589. union wpa_event_data event;
  590. u16 fc, stype;
  591. mgmt = (const struct ieee80211_mgmt *) frame;
  592. if (len < 24) {
  593. wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
  594. return;
  595. }
  596. fc = le_to_host16(mgmt->frame_control);
  597. stype = WLAN_FC_GET_STYPE(fc);
  598. os_memset(&event, 0, sizeof(event));
  599. if (freq) {
  600. event.rx_action.freq = nla_get_u32(freq);
  601. drv->last_mgmt_freq = event.rx_action.freq;
  602. }
  603. if (stype == WLAN_FC_STYPE_ACTION) {
  604. event.rx_action.da = mgmt->da;
  605. event.rx_action.sa = mgmt->sa;
  606. event.rx_action.bssid = mgmt->bssid;
  607. event.rx_action.category = mgmt->u.action.category;
  608. event.rx_action.data = &mgmt->u.action.category + 1;
  609. event.rx_action.len = frame + len - event.rx_action.data;
  610. wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
  611. } else {
  612. event.rx_mgmt.frame = frame;
  613. event.rx_mgmt.frame_len = len;
  614. wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
  615. }
  616. }
  617. static void mlme_event_action_tx_status(struct wpa_driver_nl80211_data *drv,
  618. struct nlattr *cookie, const u8 *frame,
  619. size_t len, struct nlattr *ack)
  620. {
  621. union wpa_event_data event;
  622. const struct ieee80211_hdr *hdr;
  623. u16 fc;
  624. u64 cookie_val;
  625. if (!cookie)
  626. return;
  627. cookie_val = nla_get_u64(cookie);
  628. wpa_printf(MSG_DEBUG, "nl80211: Action TX status: cookie=0%llx%s "
  629. "(ack=%d)",
  630. (long long unsigned int) cookie_val,
  631. cookie_val == drv->send_action_cookie ?
  632. " (match)" : " (unknown)", ack != NULL);
  633. if (cookie_val != drv->send_action_cookie)
  634. return;
  635. hdr = (const struct ieee80211_hdr *) frame;
  636. fc = le_to_host16(hdr->frame_control);
  637. os_memset(&event, 0, sizeof(event));
  638. event.tx_status.type = WLAN_FC_GET_TYPE(fc);
  639. event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
  640. event.tx_status.dst = hdr->addr1;
  641. event.tx_status.data = frame;
  642. event.tx_status.data_len = len;
  643. event.tx_status.ack = ack != NULL;
  644. wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
  645. }
  646. static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
  647. enum wpa_event_type type,
  648. const u8 *frame, size_t len)
  649. {
  650. const struct ieee80211_mgmt *mgmt;
  651. union wpa_event_data event;
  652. const u8 *bssid = NULL;
  653. u16 reason_code = 0;
  654. mgmt = (const struct ieee80211_mgmt *) frame;
  655. if (len >= 24) {
  656. bssid = mgmt->bssid;
  657. if (drv->associated != 0 &&
  658. os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
  659. os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
  660. /*
  661. * We have presumably received this deauth as a
  662. * response to a clear_state_mismatch() outgoing
  663. * deauth. Don't let it take us offline!
  664. */
  665. wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
  666. "from Unknown BSSID " MACSTR " -- ignoring",
  667. MAC2STR(bssid));
  668. return;
  669. }
  670. }
  671. drv->associated = 0;
  672. os_memset(&event, 0, sizeof(event));
  673. /* Note: Same offset for Reason Code in both frame subtypes */
  674. if (len >= 24 + sizeof(mgmt->u.deauth))
  675. reason_code = le_to_host16(mgmt->u.deauth.reason_code);
  676. if (type == EVENT_DISASSOC) {
  677. event.disassoc_info.addr = bssid;
  678. event.disassoc_info.reason_code = reason_code;
  679. if (frame + len > mgmt->u.disassoc.variable) {
  680. event.disassoc_info.ie = mgmt->u.disassoc.variable;
  681. event.disassoc_info.ie_len = frame + len -
  682. mgmt->u.disassoc.variable;
  683. }
  684. } else {
  685. event.deauth_info.addr = bssid;
  686. event.deauth_info.reason_code = reason_code;
  687. if (frame + len > mgmt->u.deauth.variable) {
  688. event.deauth_info.ie = mgmt->u.deauth.variable;
  689. event.deauth_info.ie_len = frame + len -
  690. mgmt->u.deauth.variable;
  691. }
  692. }
  693. wpa_supplicant_event(drv->ctx, type, &event);
  694. }
  695. static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
  696. enum wpa_event_type type,
  697. const u8 *frame, size_t len)
  698. {
  699. const struct ieee80211_mgmt *mgmt;
  700. union wpa_event_data event;
  701. u16 reason_code = 0;
  702. if (len < 24)
  703. return;
  704. mgmt = (const struct ieee80211_mgmt *) frame;
  705. os_memset(&event, 0, sizeof(event));
  706. /* Note: Same offset for Reason Code in both frame subtypes */
  707. if (len >= 24 + sizeof(mgmt->u.deauth))
  708. reason_code = le_to_host16(mgmt->u.deauth.reason_code);
  709. if (type == EVENT_UNPROT_DISASSOC) {
  710. event.unprot_disassoc.sa = mgmt->sa;
  711. event.unprot_disassoc.da = mgmt->da;
  712. event.unprot_disassoc.reason_code = reason_code;
  713. } else {
  714. event.unprot_deauth.sa = mgmt->sa;
  715. event.unprot_deauth.da = mgmt->da;
  716. event.unprot_deauth.reason_code = reason_code;
  717. }
  718. wpa_supplicant_event(drv->ctx, type, &event);
  719. }
  720. static void mlme_event(struct wpa_driver_nl80211_data *drv,
  721. enum nl80211_commands cmd, struct nlattr *frame,
  722. struct nlattr *addr, struct nlattr *timed_out,
  723. struct nlattr *freq, struct nlattr *ack,
  724. struct nlattr *cookie)
  725. {
  726. if (timed_out && addr) {
  727. mlme_timeout_event(drv, cmd, addr);
  728. return;
  729. }
  730. if (frame == NULL) {
  731. wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
  732. "data", cmd);
  733. return;
  734. }
  735. wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd);
  736. wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
  737. nla_data(frame), nla_len(frame));
  738. switch (cmd) {
  739. case NL80211_CMD_AUTHENTICATE:
  740. mlme_event_auth(drv, nla_data(frame), nla_len(frame));
  741. break;
  742. case NL80211_CMD_ASSOCIATE:
  743. mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
  744. break;
  745. case NL80211_CMD_DEAUTHENTICATE:
  746. mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
  747. nla_data(frame), nla_len(frame));
  748. break;
  749. case NL80211_CMD_DISASSOCIATE:
  750. mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
  751. nla_data(frame), nla_len(frame));
  752. break;
  753. case NL80211_CMD_FRAME:
  754. mlme_event_mgmt(drv, freq, nla_data(frame), nla_len(frame));
  755. break;
  756. case NL80211_CMD_FRAME_TX_STATUS:
  757. mlme_event_action_tx_status(drv, cookie, nla_data(frame),
  758. nla_len(frame), ack);
  759. break;
  760. case NL80211_CMD_UNPROT_DEAUTHENTICATE:
  761. mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
  762. nla_data(frame), nla_len(frame));
  763. break;
  764. case NL80211_CMD_UNPROT_DISASSOCIATE:
  765. mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
  766. nla_data(frame), nla_len(frame));
  767. break;
  768. default:
  769. break;
  770. }
  771. }
  772. static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
  773. struct nlattr *tb[])
  774. {
  775. union wpa_event_data data;
  776. wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
  777. os_memset(&data, 0, sizeof(data));
  778. if (tb[NL80211_ATTR_MAC]) {
  779. wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
  780. nla_data(tb[NL80211_ATTR_MAC]),
  781. nla_len(tb[NL80211_ATTR_MAC]));
  782. data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
  783. }
  784. if (tb[NL80211_ATTR_KEY_SEQ]) {
  785. wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
  786. nla_data(tb[NL80211_ATTR_KEY_SEQ]),
  787. nla_len(tb[NL80211_ATTR_KEY_SEQ]));
  788. }
  789. if (tb[NL80211_ATTR_KEY_TYPE]) {
  790. enum nl80211_key_type key_type =
  791. nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
  792. wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
  793. if (key_type == NL80211_KEYTYPE_PAIRWISE)
  794. data.michael_mic_failure.unicast = 1;
  795. } else
  796. data.michael_mic_failure.unicast = 1;
  797. if (tb[NL80211_ATTR_KEY_IDX]) {
  798. u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
  799. wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
  800. }
  801. wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
  802. }
  803. static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
  804. struct nlattr *tb[])
  805. {
  806. if (tb[NL80211_ATTR_MAC] == NULL) {
  807. wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
  808. "event");
  809. return;
  810. }
  811. os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
  812. drv->associated = 1;
  813. wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
  814. MAC2STR(drv->bssid));
  815. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
  816. }
  817. static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
  818. int cancel_event, struct nlattr *tb[])
  819. {
  820. unsigned int freq, chan_type, duration;
  821. union wpa_event_data data;
  822. u64 cookie;
  823. if (tb[NL80211_ATTR_WIPHY_FREQ])
  824. freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
  825. else
  826. freq = 0;
  827. if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
  828. chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
  829. else
  830. chan_type = 0;
  831. if (tb[NL80211_ATTR_DURATION])
  832. duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
  833. else
  834. duration = 0;
  835. if (tb[NL80211_ATTR_COOKIE])
  836. cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
  837. else
  838. cookie = 0;
  839. wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
  840. "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
  841. cancel_event, freq, chan_type, duration,
  842. (long long unsigned int) cookie,
  843. cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
  844. if (cookie != drv->remain_on_chan_cookie)
  845. return; /* not for us */
  846. drv->pending_remain_on_chan = !cancel_event;
  847. os_memset(&data, 0, sizeof(data));
  848. data.remain_on_channel.freq = freq;
  849. data.remain_on_channel.duration = duration;
  850. wpa_supplicant_event(drv->ctx, cancel_event ?
  851. EVENT_CANCEL_REMAIN_ON_CHANNEL :
  852. EVENT_REMAIN_ON_CHANNEL, &data);
  853. }
  854. static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
  855. struct nlattr *tb[])
  856. {
  857. union wpa_event_data event;
  858. struct nlattr *nl;
  859. int rem;
  860. struct scan_info *info;
  861. #define MAX_REPORT_FREQS 50
  862. int freqs[MAX_REPORT_FREQS];
  863. int num_freqs = 0;
  864. os_memset(&event, 0, sizeof(event));
  865. info = &event.scan_info;
  866. info->aborted = aborted;
  867. if (tb[NL80211_ATTR_SCAN_SSIDS]) {
  868. nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
  869. struct wpa_driver_scan_ssid *s =
  870. &info->ssids[info->num_ssids];
  871. s->ssid = nla_data(nl);
  872. s->ssid_len = nla_len(nl);
  873. info->num_ssids++;
  874. if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
  875. break;
  876. }
  877. }
  878. if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
  879. nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
  880. {
  881. freqs[num_freqs] = nla_get_u32(nl);
  882. num_freqs++;
  883. if (num_freqs == MAX_REPORT_FREQS - 1)
  884. break;
  885. }
  886. info->freqs = freqs;
  887. info->num_freqs = num_freqs;
  888. }
  889. wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
  890. }
  891. static int get_link_signal(struct nl_msg *msg, void *arg)
  892. {
  893. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  894. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  895. struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
  896. static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
  897. [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
  898. };
  899. struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
  900. static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
  901. [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
  902. [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
  903. [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
  904. [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
  905. };
  906. struct signal_change *sig_change = arg;
  907. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  908. genlmsg_attrlen(gnlh, 0), NULL);
  909. if (!tb[NL80211_ATTR_STA_INFO] ||
  910. nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
  911. tb[NL80211_ATTR_STA_INFO], policy))
  912. return NL_SKIP;
  913. if (!sinfo[NL80211_STA_INFO_SIGNAL])
  914. return NL_SKIP;
  915. sig_change->current_signal =
  916. (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
  917. if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
  918. if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
  919. sinfo[NL80211_STA_INFO_TX_BITRATE],
  920. rate_policy)) {
  921. sig_change->current_txrate = 0;
  922. } else {
  923. if (rinfo[NL80211_RATE_INFO_BITRATE]) {
  924. sig_change->current_txrate =
  925. nla_get_u16(rinfo[
  926. NL80211_RATE_INFO_BITRATE]) * 100;
  927. }
  928. }
  929. }
  930. return NL_SKIP;
  931. }
  932. static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
  933. struct signal_change *sig)
  934. {
  935. struct nl_msg *msg;
  936. sig->current_signal = -9999;
  937. sig->current_txrate = 0;
  938. msg = nlmsg_alloc();
  939. if (!msg)
  940. return -ENOMEM;
  941. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  942. 0, NL80211_CMD_GET_STATION, 0);
  943. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  944. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
  945. return send_and_recv_msgs(drv, msg, get_link_signal, sig);
  946. nla_put_failure:
  947. return -ENOBUFS;
  948. }
  949. static int get_link_noise(struct nl_msg *msg, void *arg)
  950. {
  951. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  952. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  953. struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
  954. static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
  955. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  956. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  957. };
  958. struct signal_change *sig_change = arg;
  959. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  960. genlmsg_attrlen(gnlh, 0), NULL);
  961. if (!tb[NL80211_ATTR_SURVEY_INFO]) {
  962. wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
  963. return NL_SKIP;
  964. }
  965. if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
  966. tb[NL80211_ATTR_SURVEY_INFO],
  967. survey_policy)) {
  968. wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
  969. "attributes!");
  970. return NL_SKIP;
  971. }
  972. if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
  973. return NL_SKIP;
  974. if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
  975. sig_change->frequency)
  976. return NL_SKIP;
  977. if (!sinfo[NL80211_SURVEY_INFO_NOISE])
  978. return NL_SKIP;
  979. sig_change->current_noise =
  980. (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
  981. return NL_SKIP;
  982. }
  983. static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
  984. struct signal_change *sig_change)
  985. {
  986. struct nl_msg *msg;
  987. sig_change->current_noise = 9999;
  988. sig_change->frequency = drv->assoc_freq;
  989. msg = nlmsg_alloc();
  990. if (!msg)
  991. return -ENOMEM;
  992. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  993. NLM_F_DUMP, NL80211_CMD_GET_SURVEY, 0);
  994. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  995. return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
  996. nla_put_failure:
  997. return -ENOBUFS;
  998. }
  999. static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
  1000. struct nlattr *tb[])
  1001. {
  1002. static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
  1003. [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
  1004. [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
  1005. [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
  1006. [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
  1007. };
  1008. struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
  1009. enum nl80211_cqm_rssi_threshold_event event;
  1010. union wpa_event_data ed;
  1011. struct signal_change sig;
  1012. int res;
  1013. if (tb[NL80211_ATTR_CQM] == NULL ||
  1014. nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
  1015. cqm_policy)) {
  1016. wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
  1017. return;
  1018. }
  1019. os_memset(&ed, 0, sizeof(ed));
  1020. if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
  1021. if (!tb[NL80211_ATTR_MAC])
  1022. return;
  1023. os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
  1024. ETH_ALEN);
  1025. wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
  1026. return;
  1027. }
  1028. if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
  1029. return;
  1030. event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
  1031. if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
  1032. wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
  1033. "event: RSSI high");
  1034. ed.signal_change.above_threshold = 1;
  1035. } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
  1036. wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
  1037. "event: RSSI low");
  1038. ed.signal_change.above_threshold = 0;
  1039. } else
  1040. return;
  1041. res = nl80211_get_link_signal(drv, &sig);
  1042. if (res == 0) {
  1043. ed.signal_change.current_signal = sig.current_signal;
  1044. ed.signal_change.current_txrate = sig.current_txrate;
  1045. wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
  1046. sig.current_signal, sig.current_txrate);
  1047. }
  1048. res = nl80211_get_link_noise(drv, &sig);
  1049. if (res == 0) {
  1050. ed.signal_change.current_noise = sig.current_noise;
  1051. wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
  1052. sig.current_noise);
  1053. }
  1054. wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
  1055. }
  1056. static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
  1057. struct nlattr **tb)
  1058. {
  1059. u8 *addr;
  1060. union wpa_event_data data;
  1061. if (tb[NL80211_ATTR_MAC] == NULL)
  1062. return;
  1063. addr = nla_data(tb[NL80211_ATTR_MAC]);
  1064. wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
  1065. if (drv->nlmode != NL80211_IFTYPE_ADHOC)
  1066. return;
  1067. os_memset(&data, 0, sizeof(data));
  1068. os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
  1069. wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
  1070. }
  1071. static int process_event(struct nl_msg *msg, void *arg)
  1072. {
  1073. struct wpa_driver_nl80211_data *drv = arg;
  1074. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1075. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1076. union wpa_event_data data;
  1077. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1078. genlmsg_attrlen(gnlh, 0), NULL);
  1079. if (tb[NL80211_ATTR_IFINDEX]) {
  1080. int ifindex = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
  1081. if (ifindex != drv->ifindex && !have_ifidx(drv, ifindex)) {
  1082. wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d)"
  1083. " for foreign interface (ifindex %d)",
  1084. gnlh->cmd, ifindex);
  1085. return NL_SKIP;
  1086. }
  1087. }
  1088. if (drv->ap_scan_as_station &&
  1089. (gnlh->cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
  1090. gnlh->cmd == NL80211_CMD_SCAN_ABORTED)) {
  1091. wpa_driver_nl80211_set_mode(&drv->first_bss,
  1092. IEEE80211_MODE_AP);
  1093. drv->ap_scan_as_station = 0;
  1094. }
  1095. switch (gnlh->cmd) {
  1096. case NL80211_CMD_TRIGGER_SCAN:
  1097. wpa_printf(MSG_DEBUG, "nl80211: Scan trigger");
  1098. break;
  1099. case NL80211_CMD_NEW_SCAN_RESULTS:
  1100. wpa_printf(MSG_DEBUG, "nl80211: New scan results available");
  1101. drv->scan_complete_events = 1;
  1102. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
  1103. drv->ctx);
  1104. send_scan_event(drv, 0, tb);
  1105. break;
  1106. case NL80211_CMD_SCAN_ABORTED:
  1107. wpa_printf(MSG_DEBUG, "nl80211: Scan aborted");
  1108. /*
  1109. * Need to indicate that scan results are available in order
  1110. * not to make wpa_supplicant stop its scanning.
  1111. */
  1112. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
  1113. drv->ctx);
  1114. send_scan_event(drv, 1, tb);
  1115. break;
  1116. case NL80211_CMD_AUTHENTICATE:
  1117. case NL80211_CMD_ASSOCIATE:
  1118. case NL80211_CMD_DEAUTHENTICATE:
  1119. case NL80211_CMD_DISASSOCIATE:
  1120. case NL80211_CMD_FRAME:
  1121. case NL80211_CMD_FRAME_TX_STATUS:
  1122. case NL80211_CMD_UNPROT_DEAUTHENTICATE:
  1123. case NL80211_CMD_UNPROT_DISASSOCIATE:
  1124. mlme_event(drv, gnlh->cmd, tb[NL80211_ATTR_FRAME],
  1125. tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
  1126. tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
  1127. tb[NL80211_ATTR_COOKIE]);
  1128. break;
  1129. case NL80211_CMD_CONNECT:
  1130. case NL80211_CMD_ROAM:
  1131. mlme_event_connect(drv, gnlh->cmd,
  1132. tb[NL80211_ATTR_STATUS_CODE],
  1133. tb[NL80211_ATTR_MAC],
  1134. tb[NL80211_ATTR_REQ_IE],
  1135. tb[NL80211_ATTR_RESP_IE]);
  1136. break;
  1137. case NL80211_CMD_DISCONNECT:
  1138. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  1139. /*
  1140. * Avoid reporting two disassociation events that could
  1141. * confuse the core code.
  1142. */
  1143. wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
  1144. "event when using userspace SME");
  1145. break;
  1146. }
  1147. drv->associated = 0;
  1148. os_memset(&data, 0, sizeof(data));
  1149. if (tb[NL80211_ATTR_REASON_CODE])
  1150. data.disassoc_info.reason_code =
  1151. nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
  1152. wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, &data);
  1153. break;
  1154. case NL80211_CMD_MICHAEL_MIC_FAILURE:
  1155. mlme_event_michael_mic_failure(drv, tb);
  1156. break;
  1157. case NL80211_CMD_JOIN_IBSS:
  1158. mlme_event_join_ibss(drv, tb);
  1159. break;
  1160. case NL80211_CMD_REMAIN_ON_CHANNEL:
  1161. mlme_event_remain_on_channel(drv, 0, tb);
  1162. break;
  1163. case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
  1164. mlme_event_remain_on_channel(drv, 1, tb);
  1165. break;
  1166. case NL80211_CMD_NOTIFY_CQM:
  1167. nl80211_cqm_event(drv, tb);
  1168. break;
  1169. case NL80211_CMD_REG_CHANGE:
  1170. wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
  1171. wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
  1172. NULL);
  1173. break;
  1174. case NL80211_CMD_REG_BEACON_HINT:
  1175. wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
  1176. wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
  1177. NULL);
  1178. break;
  1179. case NL80211_CMD_NEW_STATION:
  1180. nl80211_new_station_event(drv, tb);
  1181. break;
  1182. default:
  1183. wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
  1184. "(cmd=%d)", gnlh->cmd);
  1185. break;
  1186. }
  1187. return NL_SKIP;
  1188. }
  1189. static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
  1190. void *handle)
  1191. {
  1192. struct nl_cb *cb;
  1193. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  1194. wpa_printf(MSG_DEBUG, "nl80211: Event message available");
  1195. cb = nl_cb_clone(drv->nl_cb);
  1196. if (!cb)
  1197. return;
  1198. nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
  1199. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, process_event, drv);
  1200. nl_recvmsgs(handle, cb);
  1201. nl_cb_put(cb);
  1202. }
  1203. /**
  1204. * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
  1205. * @priv: driver_nl80211 private data
  1206. * @alpha2_arg: country to which to switch to
  1207. * Returns: 0 on success, -1 on failure
  1208. *
  1209. * This asks nl80211 to set the regulatory domain for given
  1210. * country ISO / IEC alpha2.
  1211. */
  1212. static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
  1213. {
  1214. struct i802_bss *bss = priv;
  1215. struct wpa_driver_nl80211_data *drv = bss->drv;
  1216. char alpha2[3];
  1217. struct nl_msg *msg;
  1218. msg = nlmsg_alloc();
  1219. if (!msg)
  1220. return -ENOMEM;
  1221. alpha2[0] = alpha2_arg[0];
  1222. alpha2[1] = alpha2_arg[1];
  1223. alpha2[2] = '\0';
  1224. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1225. 0, NL80211_CMD_REQ_SET_REG, 0);
  1226. NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
  1227. if (send_and_recv_msgs(drv, msg, NULL, NULL))
  1228. return -EINVAL;
  1229. return 0;
  1230. nla_put_failure:
  1231. return -EINVAL;
  1232. }
  1233. #ifndef HOSTAPD
  1234. struct wiphy_info_data {
  1235. int max_scan_ssids;
  1236. int ap_supported;
  1237. int p2p_supported;
  1238. int auth_supported;
  1239. int connect_supported;
  1240. int offchan_tx_supported;
  1241. };
  1242. static int wiphy_info_handler(struct nl_msg *msg, void *arg)
  1243. {
  1244. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1245. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1246. struct wiphy_info_data *info = arg;
  1247. int p2p_go_supported = 0, p2p_client_supported = 0;
  1248. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1249. genlmsg_attrlen(gnlh, 0), NULL);
  1250. if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
  1251. info->max_scan_ssids =
  1252. nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
  1253. if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) {
  1254. struct nlattr *nl_mode;
  1255. int i;
  1256. nla_for_each_nested(nl_mode,
  1257. tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) {
  1258. switch (nl_mode->nla_type) {
  1259. case NL80211_IFTYPE_AP:
  1260. info->ap_supported = 1;
  1261. break;
  1262. case NL80211_IFTYPE_P2P_GO:
  1263. p2p_go_supported = 1;
  1264. break;
  1265. case NL80211_IFTYPE_P2P_CLIENT:
  1266. p2p_client_supported = 1;
  1267. break;
  1268. }
  1269. }
  1270. }
  1271. info->p2p_supported = p2p_go_supported && p2p_client_supported;
  1272. if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) {
  1273. struct nlattr *nl_cmd;
  1274. int i;
  1275. nla_for_each_nested(nl_cmd,
  1276. tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) {
  1277. u32 cmd = nla_get_u32(nl_cmd);
  1278. if (cmd == NL80211_CMD_AUTHENTICATE)
  1279. info->auth_supported = 1;
  1280. else if (cmd == NL80211_CMD_CONNECT)
  1281. info->connect_supported = 1;
  1282. }
  1283. }
  1284. if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK])
  1285. info->offchan_tx_supported = 1;
  1286. return NL_SKIP;
  1287. }
  1288. static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
  1289. struct wiphy_info_data *info)
  1290. {
  1291. struct nl_msg *msg;
  1292. os_memset(info, 0, sizeof(*info));
  1293. msg = nlmsg_alloc();
  1294. if (!msg)
  1295. return -1;
  1296. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1297. 0, NL80211_CMD_GET_WIPHY, 0);
  1298. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex);
  1299. if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info) == 0)
  1300. return 0;
  1301. msg = NULL;
  1302. nla_put_failure:
  1303. nlmsg_free(msg);
  1304. return -1;
  1305. }
  1306. static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
  1307. {
  1308. struct wiphy_info_data info;
  1309. if (wpa_driver_nl80211_get_info(drv, &info))
  1310. return -1;
  1311. drv->has_capability = 1;
  1312. /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
  1313. drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1314. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  1315. WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  1316. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
  1317. drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
  1318. WPA_DRIVER_CAPA_ENC_WEP104 |
  1319. WPA_DRIVER_CAPA_ENC_TKIP |
  1320. WPA_DRIVER_CAPA_ENC_CCMP;
  1321. drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
  1322. WPA_DRIVER_AUTH_SHARED |
  1323. WPA_DRIVER_AUTH_LEAP;
  1324. drv->capa.max_scan_ssids = info.max_scan_ssids;
  1325. if (info.ap_supported)
  1326. drv->capa.flags |= WPA_DRIVER_FLAGS_AP;
  1327. if (info.auth_supported)
  1328. drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
  1329. else if (!info.connect_supported) {
  1330. wpa_printf(MSG_INFO, "nl80211: Driver does not support "
  1331. "authentication/association or connect commands");
  1332. return -1;
  1333. }
  1334. if (info.offchan_tx_supported) {
  1335. wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
  1336. "off-channel TX");
  1337. drv->capa.flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
  1338. }
  1339. drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
  1340. drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
  1341. if (info.p2p_supported)
  1342. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
  1343. drv->capa.max_remain_on_chan = 5000;
  1344. return 0;
  1345. }
  1346. #endif /* HOSTAPD */
  1347. static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
  1348. {
  1349. int ret;
  1350. /* Initialize generic netlink and nl80211 */
  1351. drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  1352. if (drv->nl_cb == NULL) {
  1353. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  1354. "callbacks");
  1355. goto err1;
  1356. }
  1357. drv->nl_handle = nl80211_handle_alloc(drv->nl_cb);
  1358. if (drv->nl_handle == NULL) {
  1359. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  1360. "callbacks");
  1361. goto err2;
  1362. }
  1363. drv->nl_handle_event = nl80211_handle_alloc(drv->nl_cb);
  1364. if (drv->nl_handle_event == NULL) {
  1365. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  1366. "callbacks (event)");
  1367. goto err2b;
  1368. }
  1369. if (genl_connect(drv->nl_handle)) {
  1370. wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
  1371. "netlink");
  1372. goto err3;
  1373. }
  1374. if (genl_connect(drv->nl_handle_event)) {
  1375. wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
  1376. "netlink (event)");
  1377. goto err3;
  1378. }
  1379. #ifdef CONFIG_LIBNL20
  1380. if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
  1381. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  1382. "netlink cache");
  1383. goto err3;
  1384. }
  1385. if (genl_ctrl_alloc_cache(drv->nl_handle_event, &drv->nl_cache_event) <
  1386. 0) {
  1387. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  1388. "netlink cache (event)");
  1389. goto err3b;
  1390. }
  1391. #else /* CONFIG_LIBNL20 */
  1392. drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
  1393. if (drv->nl_cache == NULL) {
  1394. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  1395. "netlink cache");
  1396. goto err3;
  1397. }
  1398. drv->nl_cache_event = genl_ctrl_alloc_cache(drv->nl_handle_event);
  1399. if (drv->nl_cache_event == NULL) {
  1400. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  1401. "netlink cache (event)");
  1402. goto err3b;
  1403. }
  1404. #endif /* CONFIG_LIBNL20 */
  1405. drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
  1406. if (drv->nl80211 == NULL) {
  1407. wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
  1408. "found");
  1409. goto err4;
  1410. }
  1411. ret = nl_get_multicast_id(drv, "nl80211", "scan");
  1412. if (ret >= 0)
  1413. ret = nl_socket_add_membership(drv->nl_handle_event, ret);
  1414. if (ret < 0) {
  1415. wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
  1416. "membership for scan events: %d (%s)",
  1417. ret, strerror(-ret));
  1418. goto err4;
  1419. }
  1420. ret = nl_get_multicast_id(drv, "nl80211", "mlme");
  1421. if (ret >= 0)
  1422. ret = nl_socket_add_membership(drv->nl_handle_event, ret);
  1423. if (ret < 0) {
  1424. wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
  1425. "membership for mlme events: %d (%s)",
  1426. ret, strerror(-ret));
  1427. goto err4;
  1428. }
  1429. ret = nl_get_multicast_id(drv, "nl80211", "regulatory");
  1430. if (ret >= 0)
  1431. ret = nl_socket_add_membership(drv->nl_handle_event, ret);
  1432. if (ret < 0) {
  1433. wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
  1434. "membership for regulatory events: %d (%s)",
  1435. ret, strerror(-ret));
  1436. /* Continue without regulatory events */
  1437. }
  1438. eloop_register_read_sock(nl_socket_get_fd(drv->nl_handle_event),
  1439. wpa_driver_nl80211_event_receive, drv,
  1440. drv->nl_handle_event);
  1441. return 0;
  1442. err4:
  1443. nl_cache_free(drv->nl_cache_event);
  1444. err3b:
  1445. nl_cache_free(drv->nl_cache);
  1446. err3:
  1447. nl80211_handle_destroy(drv->nl_handle_event);
  1448. err2b:
  1449. nl80211_handle_destroy(drv->nl_handle);
  1450. err2:
  1451. nl_cb_put(drv->nl_cb);
  1452. err1:
  1453. return -1;
  1454. }
  1455. static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
  1456. {
  1457. wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
  1458. /*
  1459. * This may be for any interface; use ifdown event to disable
  1460. * interface.
  1461. */
  1462. }
  1463. static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
  1464. {
  1465. struct wpa_driver_nl80211_data *drv = ctx;
  1466. wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
  1467. if (linux_set_iface_flags(drv->ioctl_sock, drv->first_bss.ifname, 1)) {
  1468. wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
  1469. "after rfkill unblock");
  1470. return;
  1471. }
  1472. /* rtnetlink ifup handler will report interface as enabled */
  1473. }
  1474. static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv)
  1475. {
  1476. /* Find phy (radio) to which this interface belongs */
  1477. char buf[90], *pos;
  1478. int f, rv;
  1479. drv->phyname[0] = '\0';
  1480. snprintf(buf, sizeof(buf) - 1, "/sys/class/net/%s/phy80211/name",
  1481. drv->first_bss.ifname);
  1482. f = open(buf, O_RDONLY);
  1483. if (f < 0) {
  1484. wpa_printf(MSG_DEBUG, "Could not open file %s: %s",
  1485. buf, strerror(errno));
  1486. return;
  1487. }
  1488. rv = read(f, drv->phyname, sizeof(drv->phyname) - 1);
  1489. close(f);
  1490. if (rv < 0) {
  1491. wpa_printf(MSG_DEBUG, "Could not read file %s: %s",
  1492. buf, strerror(errno));
  1493. return;
  1494. }
  1495. drv->phyname[rv] = '\0';
  1496. pos = os_strchr(drv->phyname, '\n');
  1497. if (pos)
  1498. *pos = '\0';
  1499. wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
  1500. drv->first_bss.ifname, drv->phyname);
  1501. }
  1502. /**
  1503. * wpa_driver_nl80211_init - Initialize nl80211 driver interface
  1504. * @ctx: context to be used when calling wpa_supplicant functions,
  1505. * e.g., wpa_supplicant_event()
  1506. * @ifname: interface name, e.g., wlan0
  1507. * @global_priv: private driver global data from global_init()
  1508. * Returns: Pointer to private data, %NULL on failure
  1509. */
  1510. static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
  1511. void *global_priv)
  1512. {
  1513. struct wpa_driver_nl80211_data *drv;
  1514. struct netlink_config *cfg;
  1515. struct rfkill_config *rcfg;
  1516. struct i802_bss *bss;
  1517. drv = os_zalloc(sizeof(*drv));
  1518. if (drv == NULL)
  1519. return NULL;
  1520. drv->global = global_priv;
  1521. if (drv->global)
  1522. dl_list_add(&drv->global->interfaces, &drv->list);
  1523. drv->ctx = ctx;
  1524. bss = &drv->first_bss;
  1525. bss->drv = drv;
  1526. os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
  1527. drv->monitor_ifidx = -1;
  1528. drv->monitor_sock = -1;
  1529. drv->ioctl_sock = -1;
  1530. if (wpa_driver_nl80211_init_nl(drv)) {
  1531. os_free(drv);
  1532. return NULL;
  1533. }
  1534. nl80211_get_phy_name(drv);
  1535. drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
  1536. if (drv->ioctl_sock < 0) {
  1537. perror("socket(PF_INET,SOCK_DGRAM)");
  1538. goto failed;
  1539. }
  1540. cfg = os_zalloc(sizeof(*cfg));
  1541. if (cfg == NULL)
  1542. goto failed;
  1543. cfg->ctx = drv;
  1544. cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
  1545. cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
  1546. drv->netlink = netlink_init(cfg);
  1547. if (drv->netlink == NULL) {
  1548. os_free(cfg);
  1549. goto failed;
  1550. }
  1551. rcfg = os_zalloc(sizeof(*rcfg));
  1552. if (rcfg == NULL)
  1553. goto failed;
  1554. rcfg->ctx = drv;
  1555. os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
  1556. rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
  1557. rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
  1558. drv->rfkill = rfkill_init(rcfg);
  1559. if (drv->rfkill == NULL) {
  1560. wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
  1561. os_free(rcfg);
  1562. }
  1563. if (wpa_driver_nl80211_finish_drv_init(drv))
  1564. goto failed;
  1565. return bss;
  1566. failed:
  1567. rfkill_deinit(drv->rfkill);
  1568. netlink_deinit(drv->netlink);
  1569. if (drv->ioctl_sock >= 0)
  1570. close(drv->ioctl_sock);
  1571. genl_family_put(drv->nl80211);
  1572. nl_cache_free(drv->nl_cache);
  1573. nl80211_handle_destroy(drv->nl_handle);
  1574. nl_cb_put(drv->nl_cb);
  1575. eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle_event));
  1576. os_free(drv);
  1577. return NULL;
  1578. }
  1579. static int nl80211_register_frame(struct wpa_driver_nl80211_data *drv,
  1580. struct nl_handle *nl_handle,
  1581. u16 type, const u8 *match, size_t match_len)
  1582. {
  1583. struct nl_msg *msg;
  1584. int ret = -1;
  1585. msg = nlmsg_alloc();
  1586. if (!msg)
  1587. return -1;
  1588. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  1589. NL80211_CMD_REGISTER_ACTION, 0);
  1590. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1591. NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
  1592. NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
  1593. ret = send_and_recv(drv, nl_handle, msg, NULL, NULL);
  1594. msg = NULL;
  1595. if (ret) {
  1596. wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
  1597. "failed (type=%u): ret=%d (%s)",
  1598. type, ret, strerror(-ret));
  1599. wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
  1600. match, match_len);
  1601. goto nla_put_failure;
  1602. }
  1603. ret = 0;
  1604. nla_put_failure:
  1605. nlmsg_free(msg);
  1606. return ret;
  1607. }
  1608. static int nl80211_register_action_frame(struct wpa_driver_nl80211_data *drv,
  1609. const u8 *match, size_t match_len)
  1610. {
  1611. u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
  1612. return nl80211_register_frame(drv, drv->nl_handle_event,
  1613. type, match, match_len);
  1614. }
  1615. static int nl80211_register_action_frames(struct wpa_driver_nl80211_data *drv)
  1616. {
  1617. #ifdef CONFIG_P2P
  1618. /* GAS Initial Request */
  1619. if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0a", 2) < 0)
  1620. return -1;
  1621. /* GAS Initial Response */
  1622. if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0b", 2) < 0)
  1623. return -1;
  1624. /* GAS Comeback Request */
  1625. if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0c", 2) < 0)
  1626. return -1;
  1627. /* GAS Comeback Response */
  1628. if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0d", 2) < 0)
  1629. return -1;
  1630. /* P2P Public Action */
  1631. if (nl80211_register_action_frame(drv,
  1632. (u8 *) "\x04\x09\x50\x6f\x9a\x09",
  1633. 6) < 0)
  1634. return -1;
  1635. /* P2P Action */
  1636. if (nl80211_register_action_frame(drv,
  1637. (u8 *) "\x7f\x50\x6f\x9a\x09",
  1638. 5) < 0)
  1639. return -1;
  1640. #endif /* CONFIG_P2P */
  1641. #ifdef CONFIG_IEEE80211W
  1642. /* SA Query Response */
  1643. if (nl80211_register_action_frame(drv, (u8 *) "\x08\x01", 2) < 0)
  1644. return -1;
  1645. #endif /* CONFIG_IEEE80211W */
  1646. /* FT Action frames */
  1647. if (nl80211_register_action_frame(drv, (u8 *) "\x06", 1) < 0)
  1648. return -1;
  1649. else
  1650. drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
  1651. WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
  1652. return 0;
  1653. }
  1654. static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
  1655. {
  1656. wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
  1657. }
  1658. static int
  1659. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
  1660. {
  1661. struct i802_bss *bss = &drv->first_bss;
  1662. int send_rfkill_event = 0;
  1663. drv->ifindex = if_nametoindex(bss->ifname);
  1664. drv->first_bss.ifindex = drv->ifindex;
  1665. #ifndef HOSTAPD
  1666. if (wpa_driver_nl80211_set_mode(bss, IEEE80211_MODE_INFRA) < 0) {
  1667. wpa_printf(MSG_DEBUG, "nl80211: Could not configure driver to "
  1668. "use managed mode");
  1669. }
  1670. if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 1)) {
  1671. if (rfkill_is_blocked(drv->rfkill)) {
  1672. wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
  1673. "interface '%s' due to rfkill",
  1674. bss->ifname);
  1675. drv->if_disabled = 1;
  1676. send_rfkill_event = 1;
  1677. } else {
  1678. wpa_printf(MSG_ERROR, "nl80211: Could not set "
  1679. "interface '%s' UP", bss->ifname);
  1680. return -1;
  1681. }
  1682. }
  1683. if (wpa_driver_nl80211_capa(drv))
  1684. return -1;
  1685. netlink_send_oper_ifla(drv->netlink, drv->ifindex,
  1686. 1, IF_OPER_DORMANT);
  1687. #endif /* HOSTAPD */
  1688. if (linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, drv->addr))
  1689. return -1;
  1690. if (nl80211_register_action_frames(drv) < 0) {
  1691. wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
  1692. "frame processing - ignore for now");
  1693. /*
  1694. * Older kernel versions did not support this, so ignore the
  1695. * error for now. Some functionality may not be available
  1696. * because of this.
  1697. */
  1698. }
  1699. if (send_rfkill_event) {
  1700. eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
  1701. drv, drv->ctx);
  1702. }
  1703. return 0;
  1704. }
  1705. static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
  1706. {
  1707. struct nl_msg *msg;
  1708. msg = nlmsg_alloc();
  1709. if (!msg)
  1710. return -ENOMEM;
  1711. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1712. 0, NL80211_CMD_DEL_BEACON, 0);
  1713. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1714. return send_and_recv_msgs(drv, msg, NULL, NULL);
  1715. nla_put_failure:
  1716. return -ENOBUFS;
  1717. }
  1718. /**
  1719. * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
  1720. * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
  1721. *
  1722. * Shut down driver interface and processing of driver events. Free
  1723. * private data buffer if one was allocated in wpa_driver_nl80211_init().
  1724. */
  1725. static void wpa_driver_nl80211_deinit(void *priv)
  1726. {
  1727. struct i802_bss *bss = priv;
  1728. struct wpa_driver_nl80211_data *drv = bss->drv;
  1729. if (drv->nl_handle_preq)
  1730. wpa_driver_nl80211_probe_req_report(bss, 0);
  1731. if (drv->added_if_into_bridge) {
  1732. if (linux_br_del_if(drv->ioctl_sock, drv->brname, bss->ifname)
  1733. < 0)
  1734. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  1735. "interface %s from bridge %s: %s",
  1736. bss->ifname, drv->brname, strerror(errno));
  1737. }
  1738. if (drv->added_bridge) {
  1739. if (linux_br_del(drv->ioctl_sock, drv->brname) < 0)
  1740. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  1741. "bridge %s: %s",
  1742. drv->brname, strerror(errno));
  1743. }
  1744. nl80211_remove_monitor_interface(drv);
  1745. if (drv->nlmode == NL80211_IFTYPE_AP)
  1746. wpa_driver_nl80211_del_beacon(drv);
  1747. #ifdef HOSTAPD
  1748. if (drv->last_freq_ht) {
  1749. /* Clear HT flags from the driver */
  1750. struct hostapd_freq_params freq;
  1751. os_memset(&freq, 0, sizeof(freq));
  1752. freq.freq = drv->last_freq;
  1753. i802_set_freq(priv, &freq);
  1754. }
  1755. if (drv->eapol_sock >= 0) {
  1756. eloop_unregister_read_sock(drv->eapol_sock);
  1757. close(drv->eapol_sock);
  1758. }
  1759. if (drv->if_indices != drv->default_if_indices)
  1760. os_free(drv->if_indices);
  1761. #endif /* HOSTAPD */
  1762. if (drv->disable_11b_rates)
  1763. nl80211_disable_11b_rates(drv, drv->ifindex, 0);
  1764. netlink_send_oper_ifla(drv->netlink, drv->ifindex, 0, IF_OPER_UP);
  1765. netlink_deinit(drv->netlink);
  1766. rfkill_deinit(drv->rfkill);
  1767. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
  1768. (void) linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 0);
  1769. wpa_driver_nl80211_set_mode(bss, IEEE80211_MODE_INFRA);
  1770. if (drv->ioctl_sock >= 0)
  1771. close(drv->ioctl_sock);
  1772. eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle_event));
  1773. genl_family_put(drv->nl80211);
  1774. nl_cache_free(drv->nl_cache);
  1775. nl_cache_free(drv->nl_cache_event);
  1776. nl80211_handle_destroy(drv->nl_handle);
  1777. nl80211_handle_destroy(drv->nl_handle_event);
  1778. nl_cb_put(drv->nl_cb);
  1779. os_free(drv->filter_ssids);
  1780. if (drv->global)
  1781. dl_list_del(&drv->list);
  1782. os_free(drv);
  1783. }
  1784. /**
  1785. * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
  1786. * @eloop_ctx: Driver private data
  1787. * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
  1788. *
  1789. * This function can be used as registered timeout when starting a scan to
  1790. * generate a scan completed event if the driver does not report this.
  1791. */
  1792. static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  1793. {
  1794. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  1795. if (drv->ap_scan_as_station) {
  1796. wpa_driver_nl80211_set_mode(&drv->first_bss,
  1797. IEEE80211_MODE_AP);
  1798. drv->ap_scan_as_station = 0;
  1799. }
  1800. wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
  1801. wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
  1802. }
  1803. /**
  1804. * wpa_driver_nl80211_scan - Request the driver to initiate scan
  1805. * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
  1806. * @params: Scan parameters
  1807. * Returns: 0 on success, -1 on failure
  1808. */
  1809. static int wpa_driver_nl80211_scan(void *priv,
  1810. struct wpa_driver_scan_params *params)
  1811. {
  1812. struct i802_bss *bss = priv;
  1813. struct wpa_driver_nl80211_data *drv = bss->drv;
  1814. int ret = 0, timeout;
  1815. struct nl_msg *msg, *ssids, *freqs;
  1816. size_t i;
  1817. msg = nlmsg_alloc();
  1818. ssids = nlmsg_alloc();
  1819. freqs = nlmsg_alloc();
  1820. if (!msg || !ssids || !freqs) {
  1821. nlmsg_free(msg);
  1822. nlmsg_free(ssids);
  1823. nlmsg_free(freqs);
  1824. return -1;
  1825. }
  1826. os_free(drv->filter_ssids);
  1827. drv->filter_ssids = params->filter_ssids;
  1828. params->filter_ssids = NULL;
  1829. drv->num_filter_ssids = params->num_filter_ssids;
  1830. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  1831. NL80211_CMD_TRIGGER_SCAN, 0);
  1832. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1833. for (i = 0; i < params->num_ssids; i++) {
  1834. wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
  1835. params->ssids[i].ssid,
  1836. params->ssids[i].ssid_len);
  1837. NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
  1838. params->ssids[i].ssid);
  1839. }
  1840. if (params->num_ssids)
  1841. nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
  1842. if (params->extra_ies) {
  1843. wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan extra IEs",
  1844. params->extra_ies, params->extra_ies_len);
  1845. NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
  1846. params->extra_ies);
  1847. }
  1848. if (params->freqs) {
  1849. for (i = 0; params->freqs[i]; i++) {
  1850. wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
  1851. "MHz", params->freqs[i]);
  1852. NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
  1853. }
  1854. nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
  1855. }
  1856. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  1857. msg = NULL;
  1858. if (ret) {
  1859. wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
  1860. "(%s)", ret, strerror(-ret));
  1861. #ifdef HOSTAPD
  1862. if (drv->nlmode == NL80211_IFTYPE_AP) {
  1863. /*
  1864. * mac80211 does not allow scan requests in AP mode, so
  1865. * try to do this in station mode.
  1866. */
  1867. if (wpa_driver_nl80211_set_mode(bss,
  1868. IEEE80211_MODE_INFRA))
  1869. goto nla_put_failure;
  1870. if (wpa_driver_nl80211_scan(drv, params)) {
  1871. wpa_driver_nl80211_set_mode(bss,
  1872. IEEE80211_MODE_AP);
  1873. goto nla_put_failure;
  1874. }
  1875. /* Restore AP mode when processing scan results */
  1876. drv->ap_scan_as_station = 1;
  1877. ret = 0;
  1878. } else
  1879. goto nla_put_failure;
  1880. #else /* HOSTAPD */
  1881. goto nla_put_failure;
  1882. #endif /* HOSTAPD */
  1883. }
  1884. /* Not all drivers generate "scan completed" wireless event, so try to
  1885. * read results after a timeout. */
  1886. timeout = 10;
  1887. if (drv->scan_complete_events) {
  1888. /*
  1889. * The driver seems to deliver events to notify when scan is
  1890. * complete, so use longer timeout to avoid race conditions
  1891. * with scanning and following association request.
  1892. */
  1893. timeout = 30;
  1894. }
  1895. wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
  1896. "seconds", ret, timeout);
  1897. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
  1898. eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
  1899. drv, drv->ctx);
  1900. nla_put_failure:
  1901. nlmsg_free(ssids);
  1902. nlmsg_free(msg);
  1903. nlmsg_free(freqs);
  1904. return ret;
  1905. }
  1906. static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
  1907. {
  1908. const u8 *end, *pos;
  1909. if (ies == NULL)
  1910. return NULL;
  1911. pos = ies;
  1912. end = ies + ies_len;
  1913. while (pos + 1 < end) {
  1914. if (pos + 2 + pos[1] > end)
  1915. break;
  1916. if (pos[0] == ie)
  1917. return pos;
  1918. pos += 2 + pos[1];
  1919. }
  1920. return NULL;
  1921. }
  1922. static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
  1923. const u8 *ie, size_t ie_len)
  1924. {
  1925. const u8 *ssid;
  1926. size_t i;
  1927. if (drv->filter_ssids == NULL)
  1928. return 0;
  1929. ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
  1930. if (ssid == NULL)
  1931. return 1;
  1932. for (i = 0; i < drv->num_filter_ssids; i++) {
  1933. if (ssid[1] == drv->filter_ssids[i].ssid_len &&
  1934. os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
  1935. 0)
  1936. return 0;
  1937. }
  1938. return 1;
  1939. }
  1940. struct nl80211_bss_info_arg {
  1941. struct wpa_driver_nl80211_data *drv;
  1942. struct wpa_scan_results *res;
  1943. };
  1944. static int bss_info_handler(struct nl_msg *msg, void *arg)
  1945. {
  1946. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1947. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1948. struct nlattr *bss[NL80211_BSS_MAX + 1];
  1949. static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
  1950. [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
  1951. [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
  1952. [NL80211_BSS_TSF] = { .type = NLA_U64 },
  1953. [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
  1954. [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
  1955. [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
  1956. [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
  1957. [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
  1958. [NL80211_BSS_STATUS] = { .type = NLA_U32 },
  1959. [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
  1960. [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
  1961. };
  1962. struct nl80211_bss_info_arg *_arg = arg;
  1963. struct wpa_scan_results *res = _arg->res;
  1964. struct wpa_scan_res **tmp;
  1965. struct wpa_scan_res *r;
  1966. const u8 *ie, *beacon_ie;
  1967. size_t ie_len, beacon_ie_len;
  1968. u8 *pos;
  1969. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1970. genlmsg_attrlen(gnlh, 0), NULL);
  1971. if (!tb[NL80211_ATTR_BSS])
  1972. return NL_SKIP;
  1973. if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
  1974. bss_policy))
  1975. return NL_SKIP;
  1976. if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
  1977. ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  1978. ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  1979. } else {
  1980. ie = NULL;
  1981. ie_len = 0;
  1982. }
  1983. if (bss[NL80211_BSS_BEACON_IES]) {
  1984. beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
  1985. beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
  1986. } else {
  1987. beacon_ie = NULL;
  1988. beacon_ie_len = 0;
  1989. }
  1990. if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
  1991. ie ? ie_len : beacon_ie_len))
  1992. return NL_SKIP;
  1993. r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
  1994. if (r == NULL)
  1995. return NL_SKIP;
  1996. if (bss[NL80211_BSS_BSSID])
  1997. os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
  1998. ETH_ALEN);
  1999. if (bss[NL80211_BSS_FREQUENCY])
  2000. r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
  2001. if (bss[NL80211_BSS_BEACON_INTERVAL])
  2002. r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
  2003. if (bss[NL80211_BSS_CAPABILITY])
  2004. r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
  2005. r->flags |= WPA_SCAN_NOISE_INVALID;
  2006. if (bss[NL80211_BSS_SIGNAL_MBM]) {
  2007. r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
  2008. r->level /= 100; /* mBm to dBm */
  2009. r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
  2010. } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
  2011. r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
  2012. r->flags |= WPA_SCAN_LEVEL_INVALID;
  2013. } else
  2014. r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
  2015. if (bss[NL80211_BSS_TSF])
  2016. r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
  2017. if (bss[NL80211_BSS_SEEN_MS_AGO])
  2018. r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
  2019. r->ie_len = ie_len;
  2020. pos = (u8 *) (r + 1);
  2021. if (ie) {
  2022. os_memcpy(pos, ie, ie_len);
  2023. pos += ie_len;
  2024. }
  2025. r->beacon_ie_len = beacon_ie_len;
  2026. if (beacon_ie)
  2027. os_memcpy(pos, beacon_ie, beacon_ie_len);
  2028. if (bss[NL80211_BSS_STATUS]) {
  2029. enum nl80211_bss_status status;
  2030. status = nla_get_u32(bss[NL80211_BSS_STATUS]);
  2031. switch (status) {
  2032. case NL80211_BSS_STATUS_AUTHENTICATED:
  2033. r->flags |= WPA_SCAN_AUTHENTICATED;
  2034. break;
  2035. case NL80211_BSS_STATUS_ASSOCIATED:
  2036. r->flags |= WPA_SCAN_ASSOCIATED;
  2037. break;
  2038. default:
  2039. break;
  2040. }
  2041. }
  2042. tmp = os_realloc(res->res,
  2043. (res->num + 1) * sizeof(struct wpa_scan_res *));
  2044. if (tmp == NULL) {
  2045. os_free(r);
  2046. return NL_SKIP;
  2047. }
  2048. tmp[res->num++] = r;
  2049. res->res = tmp;
  2050. return NL_SKIP;
  2051. }
  2052. static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
  2053. const u8 *addr)
  2054. {
  2055. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  2056. wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
  2057. "mismatch (" MACSTR ")", MAC2STR(addr));
  2058. wpa_driver_nl80211_mlme(drv, addr,
  2059. NL80211_CMD_DEAUTHENTICATE,
  2060. WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
  2061. }
  2062. }
  2063. static void wpa_driver_nl80211_check_bss_status(
  2064. struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
  2065. {
  2066. size_t i;
  2067. for (i = 0; i < res->num; i++) {
  2068. struct wpa_scan_res *r = res->res[i];
  2069. if (r->flags & WPA_SCAN_AUTHENTICATED) {
  2070. wpa_printf(MSG_DEBUG, "nl80211: Scan results "
  2071. "indicates BSS status with " MACSTR
  2072. " as authenticated",
  2073. MAC2STR(r->bssid));
  2074. if (drv->nlmode == NL80211_IFTYPE_STATION &&
  2075. os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
  2076. os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
  2077. 0) {
  2078. wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
  2079. " in local state (auth=" MACSTR
  2080. " assoc=" MACSTR ")",
  2081. MAC2STR(drv->auth_bssid),
  2082. MAC2STR(drv->bssid));
  2083. clear_state_mismatch(drv, r->bssid);
  2084. }
  2085. }
  2086. if (r->flags & WPA_SCAN_ASSOCIATED) {
  2087. wpa_printf(MSG_DEBUG, "nl80211: Scan results "
  2088. "indicate BSS status with " MACSTR
  2089. " as associated",
  2090. MAC2STR(r->bssid));
  2091. if (drv->nlmode == NL80211_IFTYPE_STATION &&
  2092. !drv->associated) {
  2093. wpa_printf(MSG_DEBUG, "nl80211: Local state "
  2094. "(not associated) does not match "
  2095. "with BSS state");
  2096. clear_state_mismatch(drv, r->bssid);
  2097. } else if (drv->nlmode == NL80211_IFTYPE_STATION &&
  2098. os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
  2099. 0) {
  2100. wpa_printf(MSG_DEBUG, "nl80211: Local state "
  2101. "(associated with " MACSTR ") does "
  2102. "not match with BSS state",
  2103. MAC2STR(drv->bssid));
  2104. clear_state_mismatch(drv, r->bssid);
  2105. clear_state_mismatch(drv, drv->bssid);
  2106. }
  2107. }
  2108. }
  2109. }
  2110. static void wpa_scan_results_free(struct wpa_scan_results *res)
  2111. {
  2112. size_t i;
  2113. if (res == NULL)
  2114. return;
  2115. for (i = 0; i < res->num; i++)
  2116. os_free(res->res[i]);
  2117. os_free(res->res);
  2118. os_free(res);
  2119. }
  2120. static struct wpa_scan_results *
  2121. nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
  2122. {
  2123. struct nl_msg *msg;
  2124. struct wpa_scan_results *res;
  2125. int ret;
  2126. struct nl80211_bss_info_arg arg;
  2127. res = os_zalloc(sizeof(*res));
  2128. if (res == NULL)
  2129. return NULL;
  2130. msg = nlmsg_alloc();
  2131. if (!msg)
  2132. goto nla_put_failure;
  2133. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, NLM_F_DUMP,
  2134. NL80211_CMD_GET_SCAN, 0);
  2135. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2136. arg.drv = drv;
  2137. arg.res = res;
  2138. ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
  2139. msg = NULL;
  2140. if (ret == 0) {
  2141. wpa_printf(MSG_DEBUG, "Received scan results (%lu BSSes)",
  2142. (unsigned long) res->num);
  2143. return res;
  2144. }
  2145. wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
  2146. "(%s)", ret, strerror(-ret));
  2147. nla_put_failure:
  2148. nlmsg_free(msg);
  2149. wpa_scan_results_free(res);
  2150. return NULL;
  2151. }
  2152. /**
  2153. * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
  2154. * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
  2155. * Returns: Scan results on success, -1 on failure
  2156. */
  2157. static struct wpa_scan_results *
  2158. wpa_driver_nl80211_get_scan_results(void *priv)
  2159. {
  2160. struct i802_bss *bss = priv;
  2161. struct wpa_driver_nl80211_data *drv = bss->drv;
  2162. struct wpa_scan_results *res;
  2163. res = nl80211_get_scan_results(drv);
  2164. if (res)
  2165. wpa_driver_nl80211_check_bss_status(drv, res);
  2166. return res;
  2167. }
  2168. static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
  2169. {
  2170. struct wpa_scan_results *res;
  2171. size_t i;
  2172. res = nl80211_get_scan_results(drv);
  2173. if (res == NULL) {
  2174. wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
  2175. return;
  2176. }
  2177. wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
  2178. for (i = 0; i < res->num; i++) {
  2179. struct wpa_scan_res *r = res->res[i];
  2180. wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
  2181. (int) i, (int) res->num, MAC2STR(r->bssid),
  2182. r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
  2183. r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
  2184. }
  2185. wpa_scan_results_free(res);
  2186. }
  2187. static int wpa_driver_nl80211_set_key(const char *ifname, void *priv,
  2188. enum wpa_alg alg, const u8 *addr,
  2189. int key_idx, int set_tx,
  2190. const u8 *seq, size_t seq_len,
  2191. const u8 *key, size_t key_len)
  2192. {
  2193. struct i802_bss *bss = priv;
  2194. struct wpa_driver_nl80211_data *drv = bss->drv;
  2195. int ifindex = if_nametoindex(ifname);
  2196. struct nl_msg *msg;
  2197. int ret;
  2198. wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
  2199. "set_tx=%d seq_len=%lu key_len=%lu",
  2200. __func__, ifindex, alg, addr, key_idx, set_tx,
  2201. (unsigned long) seq_len, (unsigned long) key_len);
  2202. msg = nlmsg_alloc();
  2203. if (!msg)
  2204. return -ENOMEM;
  2205. if (alg == WPA_ALG_NONE) {
  2206. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2207. 0, NL80211_CMD_DEL_KEY, 0);
  2208. } else {
  2209. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2210. 0, NL80211_CMD_NEW_KEY, 0);
  2211. NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
  2212. switch (alg) {
  2213. case WPA_ALG_WEP:
  2214. if (key_len == 5)
  2215. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  2216. WLAN_CIPHER_SUITE_WEP40);
  2217. else
  2218. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  2219. WLAN_CIPHER_SUITE_WEP104);
  2220. break;
  2221. case WPA_ALG_TKIP:
  2222. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  2223. WLAN_CIPHER_SUITE_TKIP);
  2224. break;
  2225. case WPA_ALG_CCMP:
  2226. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  2227. WLAN_CIPHER_SUITE_CCMP);
  2228. break;
  2229. case WPA_ALG_IGTK:
  2230. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  2231. WLAN_CIPHER_SUITE_AES_CMAC);
  2232. break;
  2233. default:
  2234. wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
  2235. "algorithm %d", __func__, alg);
  2236. nlmsg_free(msg);
  2237. return -1;
  2238. }
  2239. }
  2240. if (seq && seq_len)
  2241. NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
  2242. if (addr && !is_broadcast_ether_addr(addr)) {
  2243. wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
  2244. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  2245. if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
  2246. wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
  2247. NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
  2248. NL80211_KEYTYPE_GROUP);
  2249. }
  2250. } else if (addr && is_broadcast_ether_addr(addr)) {
  2251. struct nl_msg *types;
  2252. int err;
  2253. wpa_printf(MSG_DEBUG, " broadcast key");
  2254. types = nlmsg_alloc();
  2255. if (!types)
  2256. goto nla_put_failure;
  2257. NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
  2258. err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
  2259. types);
  2260. nlmsg_free(types);
  2261. if (err)
  2262. goto nla_put_failure;
  2263. }
  2264. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
  2265. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  2266. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2267. if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
  2268. ret = 0;
  2269. if (ret)
  2270. wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
  2271. ret, strerror(-ret));
  2272. /*
  2273. * If we failed or don't need to set the default TX key (below),
  2274. * we're done here.
  2275. */
  2276. if (ret || !set_tx || alg == WPA_ALG_NONE)
  2277. return ret;
  2278. if (drv->nlmode == NL80211_IFTYPE_AP && addr &&
  2279. !is_broadcast_ether_addr(addr))
  2280. return ret;
  2281. msg = nlmsg_alloc();
  2282. if (!msg)
  2283. return -ENOMEM;
  2284. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2285. 0, NL80211_CMD_SET_KEY, 0);
  2286. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
  2287. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  2288. if (alg == WPA_ALG_IGTK)
  2289. NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
  2290. else
  2291. NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
  2292. if (addr && is_broadcast_ether_addr(addr)) {
  2293. struct nl_msg *types;
  2294. int err;
  2295. types = nlmsg_alloc();
  2296. if (!types)
  2297. goto nla_put_failure;
  2298. NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
  2299. err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
  2300. types);
  2301. nlmsg_free(types);
  2302. if (err)
  2303. goto nla_put_failure;
  2304. } else if (addr) {
  2305. struct nl_msg *types;
  2306. int err;
  2307. types = nlmsg_alloc();
  2308. if (!types)
  2309. goto nla_put_failure;
  2310. NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_UNICAST);
  2311. err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
  2312. types);
  2313. nlmsg_free(types);
  2314. if (err)
  2315. goto nla_put_failure;
  2316. }
  2317. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2318. if (ret == -ENOENT)
  2319. ret = 0;
  2320. if (ret)
  2321. wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
  2322. "err=%d %s)", ret, strerror(-ret));
  2323. return ret;
  2324. nla_put_failure:
  2325. return -ENOBUFS;
  2326. }
  2327. static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
  2328. int key_idx, int defkey,
  2329. const u8 *seq, size_t seq_len,
  2330. const u8 *key, size_t key_len)
  2331. {
  2332. struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
  2333. if (!key_attr)
  2334. return -1;
  2335. if (defkey && alg == WPA_ALG_IGTK)
  2336. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
  2337. else if (defkey)
  2338. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
  2339. NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
  2340. switch (alg) {
  2341. case WPA_ALG_WEP:
  2342. if (key_len == 5)
  2343. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  2344. WLAN_CIPHER_SUITE_WEP40);
  2345. else
  2346. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  2347. WLAN_CIPHER_SUITE_WEP104);
  2348. break;
  2349. case WPA_ALG_TKIP:
  2350. NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
  2351. break;
  2352. case WPA_ALG_CCMP:
  2353. NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
  2354. break;
  2355. case WPA_ALG_IGTK:
  2356. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  2357. WLAN_CIPHER_SUITE_AES_CMAC);
  2358. break;
  2359. default:
  2360. wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
  2361. "algorithm %d", __func__, alg);
  2362. return -1;
  2363. }
  2364. if (seq && seq_len)
  2365. NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
  2366. NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
  2367. nla_nest_end(msg, key_attr);
  2368. return 0;
  2369. nla_put_failure:
  2370. return -1;
  2371. }
  2372. static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
  2373. struct nl_msg *msg)
  2374. {
  2375. int i, privacy = 0;
  2376. struct nlattr *nl_keys, *nl_key;
  2377. for (i = 0; i < 4; i++) {
  2378. if (!params->wep_key[i])
  2379. continue;
  2380. privacy = 1;
  2381. break;
  2382. }
  2383. if (params->wps == WPS_MODE_PRIVACY)
  2384. privacy = 1;
  2385. if (params->pairwise_suite &&
  2386. params->pairwise_suite != WPA_CIPHER_NONE)
  2387. privacy = 1;
  2388. if (!privacy)
  2389. return 0;
  2390. NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
  2391. nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
  2392. if (!nl_keys)
  2393. goto nla_put_failure;
  2394. for (i = 0; i < 4; i++) {
  2395. if (!params->wep_key[i])
  2396. continue;
  2397. nl_key = nla_nest_start(msg, i);
  2398. if (!nl_key)
  2399. goto nla_put_failure;
  2400. NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
  2401. params->wep_key[i]);
  2402. if (params->wep_key_len[i] == 5)
  2403. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  2404. WLAN_CIPHER_SUITE_WEP40);
  2405. else
  2406. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  2407. WLAN_CIPHER_SUITE_WEP104);
  2408. NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
  2409. if (i == params->wep_tx_keyidx)
  2410. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
  2411. nla_nest_end(msg, nl_key);
  2412. }
  2413. nla_nest_end(msg, nl_keys);
  2414. return 0;
  2415. nla_put_failure:
  2416. return -ENOBUFS;
  2417. }
  2418. static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
  2419. const u8 *addr, int cmd, u16 reason_code,
  2420. int local_state_change)
  2421. {
  2422. int ret = -1;
  2423. struct nl_msg *msg;
  2424. msg = nlmsg_alloc();
  2425. if (!msg)
  2426. return -1;
  2427. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, cmd, 0);
  2428. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2429. NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
  2430. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  2431. if (local_state_change)
  2432. NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
  2433. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2434. msg = NULL;
  2435. if (ret) {
  2436. wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
  2437. "(%s)", ret, strerror(-ret));
  2438. goto nla_put_failure;
  2439. }
  2440. ret = 0;
  2441. nla_put_failure:
  2442. nlmsg_free(msg);
  2443. return ret;
  2444. }
  2445. static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
  2446. const u8 *addr, int reason_code)
  2447. {
  2448. wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
  2449. __func__, MAC2STR(addr), reason_code);
  2450. drv->associated = 0;
  2451. return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISCONNECT,
  2452. reason_code, 0);
  2453. }
  2454. static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
  2455. int reason_code)
  2456. {
  2457. struct i802_bss *bss = priv;
  2458. struct wpa_driver_nl80211_data *drv = bss->drv;
  2459. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
  2460. return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
  2461. wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
  2462. __func__, MAC2STR(addr), reason_code);
  2463. drv->associated = 0;
  2464. return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
  2465. reason_code, 0);
  2466. }
  2467. static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
  2468. int reason_code)
  2469. {
  2470. struct i802_bss *bss = priv;
  2471. struct wpa_driver_nl80211_data *drv = bss->drv;
  2472. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
  2473. return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
  2474. wpa_printf(MSG_DEBUG, "%s", __func__);
  2475. drv->associated = 0;
  2476. return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE,
  2477. reason_code, 0);
  2478. }
  2479. static int wpa_driver_nl80211_authenticate(
  2480. void *priv, struct wpa_driver_auth_params *params)
  2481. {
  2482. struct i802_bss *bss = priv;
  2483. struct wpa_driver_nl80211_data *drv = bss->drv;
  2484. int ret = -1, i;
  2485. struct nl_msg *msg;
  2486. enum nl80211_auth_type type;
  2487. int count = 0;
  2488. drv->associated = 0;
  2489. os_memset(drv->auth_bssid, 0, ETH_ALEN);
  2490. /* FIX: IBSS mode */
  2491. if (drv->nlmode != NL80211_IFTYPE_STATION &&
  2492. wpa_driver_nl80211_set_mode(priv, IEEE80211_MODE_INFRA) < 0)
  2493. return -1;
  2494. retry:
  2495. msg = nlmsg_alloc();
  2496. if (!msg)
  2497. return -1;
  2498. wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
  2499. drv->ifindex);
  2500. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  2501. NL80211_CMD_AUTHENTICATE, 0);
  2502. for (i = 0; i < 4; i++) {
  2503. if (!params->wep_key[i])
  2504. continue;
  2505. wpa_driver_nl80211_set_key(bss->ifname, priv, WPA_ALG_WEP,
  2506. NULL, i,
  2507. i == params->wep_tx_keyidx, NULL, 0,
  2508. params->wep_key[i],
  2509. params->wep_key_len[i]);
  2510. if (params->wep_tx_keyidx != i)
  2511. continue;
  2512. if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
  2513. params->wep_key[i], params->wep_key_len[i])) {
  2514. nlmsg_free(msg);
  2515. return -1;
  2516. }
  2517. }
  2518. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2519. if (params->bssid) {
  2520. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  2521. MAC2STR(params->bssid));
  2522. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  2523. }
  2524. if (params->freq) {
  2525. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  2526. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  2527. }
  2528. if (params->ssid) {
  2529. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  2530. params->ssid, params->ssid_len);
  2531. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  2532. params->ssid);
  2533. }
  2534. wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
  2535. if (params->ie)
  2536. NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
  2537. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  2538. type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  2539. else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  2540. type = NL80211_AUTHTYPE_SHARED_KEY;
  2541. else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  2542. type = NL80211_AUTHTYPE_NETWORK_EAP;
  2543. else if (params->auth_alg & WPA_AUTH_ALG_FT)
  2544. type = NL80211_AUTHTYPE_FT;
  2545. else
  2546. goto nla_put_failure;
  2547. wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
  2548. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
  2549. if (params->local_state_change) {
  2550. wpa_printf(MSG_DEBUG, " * Local state change only");
  2551. NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
  2552. }
  2553. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2554. msg = NULL;
  2555. if (ret) {
  2556. wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
  2557. "(%s)", ret, strerror(-ret));
  2558. count++;
  2559. if (ret == -EALREADY && count == 1 && params->bssid &&
  2560. !params->local_state_change) {
  2561. /*
  2562. * mac80211 does not currently accept new
  2563. * authentication if we are already authenticated. As a
  2564. * workaround, force deauthentication and try again.
  2565. */
  2566. wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
  2567. "after forced deauthentication");
  2568. wpa_driver_nl80211_deauthenticate(
  2569. bss, params->bssid,
  2570. WLAN_REASON_PREV_AUTH_NOT_VALID);
  2571. nlmsg_free(msg);
  2572. goto retry;
  2573. }
  2574. goto nla_put_failure;
  2575. }
  2576. ret = 0;
  2577. wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
  2578. "successfully");
  2579. nla_put_failure:
  2580. nlmsg_free(msg);
  2581. return ret;
  2582. }
  2583. struct phy_info_arg {
  2584. u16 *num_modes;
  2585. struct hostapd_hw_modes *modes;
  2586. };
  2587. static int phy_info_handler(struct nl_msg *msg, void *arg)
  2588. {
  2589. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  2590. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  2591. struct phy_info_arg *phy_info = arg;
  2592. struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
  2593. struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
  2594. static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
  2595. [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
  2596. [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
  2597. [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
  2598. [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
  2599. [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
  2600. [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
  2601. };
  2602. struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
  2603. static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
  2604. [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
  2605. [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
  2606. };
  2607. struct nlattr *nl_band;
  2608. struct nlattr *nl_freq;
  2609. struct nlattr *nl_rate;
  2610. int rem_band, rem_freq, rem_rate;
  2611. struct hostapd_hw_modes *mode;
  2612. int idx, mode_is_set;
  2613. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  2614. genlmsg_attrlen(gnlh, 0), NULL);
  2615. if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
  2616. return NL_SKIP;
  2617. nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
  2618. mode = os_realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
  2619. if (!mode)
  2620. return NL_SKIP;
  2621. phy_info->modes = mode;
  2622. mode_is_set = 0;
  2623. mode = &phy_info->modes[*(phy_info->num_modes)];
  2624. memset(mode, 0, sizeof(*mode));
  2625. *(phy_info->num_modes) += 1;
  2626. nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
  2627. nla_len(nl_band), NULL);
  2628. if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
  2629. mode->ht_capab = nla_get_u16(
  2630. tb_band[NL80211_BAND_ATTR_HT_CAPA]);
  2631. }
  2632. if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) {
  2633. mode->a_mpdu_params |= nla_get_u8(
  2634. tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) &
  2635. 0x03;
  2636. }
  2637. if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) {
  2638. mode->a_mpdu_params |= nla_get_u8(
  2639. tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) <<
  2640. 2;
  2641. }
  2642. if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] &&
  2643. nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) {
  2644. u8 *mcs;
  2645. mcs = nla_data(tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
  2646. os_memcpy(mode->mcs_set, mcs, 16);
  2647. }
  2648. nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
  2649. nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
  2650. nla_len(nl_freq), freq_policy);
  2651. if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
  2652. continue;
  2653. mode->num_channels++;
  2654. }
  2655. mode->channels = os_zalloc(mode->num_channels * sizeof(struct hostapd_channel_data));
  2656. if (!mode->channels)
  2657. return NL_SKIP;
  2658. idx = 0;
  2659. nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
  2660. nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
  2661. nla_len(nl_freq), freq_policy);
  2662. if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
  2663. continue;
  2664. mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
  2665. mode->channels[idx].flag = 0;
  2666. if (!mode_is_set) {
  2667. /* crude heuristic */
  2668. if (mode->channels[idx].freq < 4000)
  2669. mode->mode = HOSTAPD_MODE_IEEE80211B;
  2670. else
  2671. mode->mode = HOSTAPD_MODE_IEEE80211A;
  2672. mode_is_set = 1;
  2673. }
  2674. /* crude heuristic */
  2675. if (mode->channels[idx].freq < 4000)
  2676. if (mode->channels[idx].freq == 2484)
  2677. mode->channels[idx].chan = 14;
  2678. else
  2679. mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
  2680. else
  2681. mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
  2682. if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
  2683. mode->channels[idx].flag |=
  2684. HOSTAPD_CHAN_DISABLED;
  2685. if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
  2686. mode->channels[idx].flag |=
  2687. HOSTAPD_CHAN_PASSIVE_SCAN;
  2688. if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
  2689. mode->channels[idx].flag |=
  2690. HOSTAPD_CHAN_NO_IBSS;
  2691. if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
  2692. mode->channels[idx].flag |=
  2693. HOSTAPD_CHAN_RADAR;
  2694. if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
  2695. !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
  2696. mode->channels[idx].max_tx_power =
  2697. nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
  2698. idx++;
  2699. }
  2700. nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
  2701. nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
  2702. nla_len(nl_rate), rate_policy);
  2703. if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
  2704. continue;
  2705. mode->num_rates++;
  2706. }
  2707. mode->rates = os_zalloc(mode->num_rates * sizeof(int));
  2708. if (!mode->rates)
  2709. return NL_SKIP;
  2710. idx = 0;
  2711. nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
  2712. nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
  2713. nla_len(nl_rate), rate_policy);
  2714. if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
  2715. continue;
  2716. mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
  2717. /* crude heuristic */
  2718. if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
  2719. mode->rates[idx] > 200)
  2720. mode->mode = HOSTAPD_MODE_IEEE80211G;
  2721. idx++;
  2722. }
  2723. }
  2724. return NL_SKIP;
  2725. }
  2726. static struct hostapd_hw_modes *
  2727. wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes)
  2728. {
  2729. u16 m;
  2730. struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
  2731. int i, mode11g_idx = -1;
  2732. /* If only 802.11g mode is included, use it to construct matching
  2733. * 802.11b mode data. */
  2734. for (m = 0; m < *num_modes; m++) {
  2735. if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
  2736. return modes; /* 802.11b already included */
  2737. if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
  2738. mode11g_idx = m;
  2739. }
  2740. if (mode11g_idx < 0)
  2741. return modes; /* 2.4 GHz band not supported at all */
  2742. nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
  2743. if (nmodes == NULL)
  2744. return modes; /* Could not add 802.11b mode */
  2745. mode = &nmodes[*num_modes];
  2746. os_memset(mode, 0, sizeof(*mode));
  2747. (*num_modes)++;
  2748. modes = nmodes;
  2749. mode->mode = HOSTAPD_MODE_IEEE80211B;
  2750. mode11g = &modes[mode11g_idx];
  2751. mode->num_channels = mode11g->num_channels;
  2752. mode->channels = os_malloc(mode11g->num_channels *
  2753. sizeof(struct hostapd_channel_data));
  2754. if (mode->channels == NULL) {
  2755. (*num_modes)--;
  2756. return modes; /* Could not add 802.11b mode */
  2757. }
  2758. os_memcpy(mode->channels, mode11g->channels,
  2759. mode11g->num_channels * sizeof(struct hostapd_channel_data));
  2760. mode->num_rates = 0;
  2761. mode->rates = os_malloc(4 * sizeof(int));
  2762. if (mode->rates == NULL) {
  2763. os_free(mode->channels);
  2764. (*num_modes)--;
  2765. return modes; /* Could not add 802.11b mode */
  2766. }
  2767. for (i = 0; i < mode11g->num_rates; i++) {
  2768. if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
  2769. mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
  2770. continue;
  2771. mode->rates[mode->num_rates] = mode11g->rates[i];
  2772. mode->num_rates++;
  2773. if (mode->num_rates == 4)
  2774. break;
  2775. }
  2776. if (mode->num_rates == 0) {
  2777. os_free(mode->channels);
  2778. os_free(mode->rates);
  2779. (*num_modes)--;
  2780. return modes; /* No 802.11b rates */
  2781. }
  2782. wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
  2783. "information");
  2784. return modes;
  2785. }
  2786. static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
  2787. int end)
  2788. {
  2789. int c;
  2790. for (c = 0; c < mode->num_channels; c++) {
  2791. struct hostapd_channel_data *chan = &mode->channels[c];
  2792. if (chan->freq - 10 >= start && chan->freq + 10 <= end)
  2793. chan->flag |= HOSTAPD_CHAN_HT40;
  2794. }
  2795. }
  2796. static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
  2797. int end)
  2798. {
  2799. int c;
  2800. for (c = 0; c < mode->num_channels; c++) {
  2801. struct hostapd_channel_data *chan = &mode->channels[c];
  2802. if (!(chan->flag & HOSTAPD_CHAN_HT40))
  2803. continue;
  2804. if (chan->freq - 30 >= start && chan->freq - 10 <= end)
  2805. chan->flag |= HOSTAPD_CHAN_HT40MINUS;
  2806. if (chan->freq + 10 >= start && chan->freq + 30 <= end)
  2807. chan->flag |= HOSTAPD_CHAN_HT40PLUS;
  2808. }
  2809. }
  2810. static void nl80211_reg_rule_ht40(struct nlattr *tb[],
  2811. struct phy_info_arg *results)
  2812. {
  2813. u32 start, end, max_bw;
  2814. u16 m;
  2815. if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
  2816. tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
  2817. tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
  2818. return;
  2819. start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
  2820. end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
  2821. max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
  2822. wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
  2823. start, end, max_bw);
  2824. if (max_bw < 40)
  2825. return;
  2826. for (m = 0; m < *results->num_modes; m++) {
  2827. if (!(results->modes[m].ht_capab &
  2828. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  2829. continue;
  2830. nl80211_set_ht40_mode(&results->modes[m], start, end);
  2831. }
  2832. }
  2833. static void nl80211_reg_rule_sec(struct nlattr *tb[],
  2834. struct phy_info_arg *results)
  2835. {
  2836. u32 start, end, max_bw;
  2837. u16 m;
  2838. if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
  2839. tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
  2840. tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
  2841. return;
  2842. start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
  2843. end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
  2844. max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
  2845. if (max_bw < 20)
  2846. return;
  2847. for (m = 0; m < *results->num_modes; m++) {
  2848. if (!(results->modes[m].ht_capab &
  2849. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  2850. continue;
  2851. nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
  2852. }
  2853. }
  2854. static int nl80211_get_reg(struct nl_msg *msg, void *arg)
  2855. {
  2856. struct phy_info_arg *results = arg;
  2857. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  2858. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  2859. struct nlattr *nl_rule;
  2860. struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
  2861. int rem_rule;
  2862. static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
  2863. [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
  2864. [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
  2865. [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
  2866. [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
  2867. [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
  2868. [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
  2869. };
  2870. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  2871. genlmsg_attrlen(gnlh, 0), NULL);
  2872. if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
  2873. !tb_msg[NL80211_ATTR_REG_RULES]) {
  2874. wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
  2875. "available");
  2876. return NL_SKIP;
  2877. }
  2878. wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
  2879. (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
  2880. nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
  2881. {
  2882. nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
  2883. nla_data(nl_rule), nla_len(nl_rule), reg_policy);
  2884. nl80211_reg_rule_ht40(tb_rule, results);
  2885. }
  2886. nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
  2887. {
  2888. nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
  2889. nla_data(nl_rule), nla_len(nl_rule), reg_policy);
  2890. nl80211_reg_rule_sec(tb_rule, results);
  2891. }
  2892. return NL_SKIP;
  2893. }
  2894. static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv,
  2895. struct phy_info_arg *results)
  2896. {
  2897. struct nl_msg *msg;
  2898. msg = nlmsg_alloc();
  2899. if (!msg)
  2900. return -ENOMEM;
  2901. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2902. 0, NL80211_CMD_GET_REG, 0);
  2903. return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
  2904. }
  2905. static struct hostapd_hw_modes *
  2906. wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
  2907. {
  2908. struct i802_bss *bss = priv;
  2909. struct wpa_driver_nl80211_data *drv = bss->drv;
  2910. struct nl_msg *msg;
  2911. struct phy_info_arg result = {
  2912. .num_modes = num_modes,
  2913. .modes = NULL,
  2914. };
  2915. *num_modes = 0;
  2916. *flags = 0;
  2917. msg = nlmsg_alloc();
  2918. if (!msg)
  2919. return NULL;
  2920. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2921. 0, NL80211_CMD_GET_WIPHY, 0);
  2922. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2923. if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
  2924. nl80211_set_ht40_flags(drv, &result);
  2925. return wpa_driver_nl80211_add_11b(result.modes, num_modes);
  2926. }
  2927. nla_put_failure:
  2928. return NULL;
  2929. }
  2930. static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data *drv,
  2931. const void *data, size_t len,
  2932. int encrypt)
  2933. {
  2934. __u8 rtap_hdr[] = {
  2935. 0x00, 0x00, /* radiotap version */
  2936. 0x0e, 0x00, /* radiotap length */
  2937. 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
  2938. IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
  2939. 0x00, /* padding */
  2940. 0x00, 0x00, /* RX and TX flags to indicate that */
  2941. 0x00, 0x00, /* this is the injected frame directly */
  2942. };
  2943. struct iovec iov[2] = {
  2944. {
  2945. .iov_base = &rtap_hdr,
  2946. .iov_len = sizeof(rtap_hdr),
  2947. },
  2948. {
  2949. .iov_base = (void *) data,
  2950. .iov_len = len,
  2951. }
  2952. };
  2953. struct msghdr msg = {
  2954. .msg_name = NULL,
  2955. .msg_namelen = 0,
  2956. .msg_iov = iov,
  2957. .msg_iovlen = 2,
  2958. .msg_control = NULL,
  2959. .msg_controllen = 0,
  2960. .msg_flags = 0,
  2961. };
  2962. int res;
  2963. if (encrypt)
  2964. rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
  2965. res = sendmsg(drv->monitor_sock, &msg, 0);
  2966. if (res < 0) {
  2967. wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
  2968. return -1;
  2969. }
  2970. return 0;
  2971. }
  2972. static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
  2973. size_t data_len)
  2974. {
  2975. struct i802_bss *bss = priv;
  2976. struct wpa_driver_nl80211_data *drv = bss->drv;
  2977. struct ieee80211_mgmt *mgmt;
  2978. int encrypt = 1;
  2979. u16 fc;
  2980. mgmt = (struct ieee80211_mgmt *) data;
  2981. fc = le_to_host16(mgmt->frame_control);
  2982. if (drv->nlmode == NL80211_IFTYPE_STATION &&
  2983. WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  2984. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
  2985. /*
  2986. * The use of last_mgmt_freq is a bit of a hack,
  2987. * but it works due to the single-threaded nature
  2988. * of wpa_supplicant.
  2989. */
  2990. return nl80211_send_frame_cmd(drv, drv->last_mgmt_freq, 0,
  2991. data, data_len, NULL);
  2992. }
  2993. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  2994. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
  2995. /*
  2996. * Only one of the authentication frame types is encrypted.
  2997. * In order for static WEP encryption to work properly (i.e.,
  2998. * to not encrypt the frame), we need to tell mac80211 about
  2999. * the frames that must not be encrypted.
  3000. */
  3001. u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  3002. u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
  3003. if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
  3004. encrypt = 0;
  3005. }
  3006. return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt);
  3007. }
  3008. static int wpa_driver_nl80211_set_beacon(void *priv,
  3009. const u8 *head, size_t head_len,
  3010. const u8 *tail, size_t tail_len,
  3011. int dtim_period, int beacon_int)
  3012. {
  3013. struct i802_bss *bss = priv;
  3014. struct wpa_driver_nl80211_data *drv = bss->drv;
  3015. struct nl_msg *msg;
  3016. u8 cmd = NL80211_CMD_NEW_BEACON;
  3017. int ret;
  3018. int beacon_set;
  3019. int ifindex = if_nametoindex(bss->ifname);
  3020. beacon_set = bss->beacon_set;
  3021. msg = nlmsg_alloc();
  3022. if (!msg)
  3023. return -ENOMEM;
  3024. wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
  3025. beacon_set);
  3026. if (beacon_set)
  3027. cmd = NL80211_CMD_SET_BEACON;
  3028. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3029. 0, cmd, 0);
  3030. NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
  3031. NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
  3032. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  3033. NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, beacon_int);
  3034. NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
  3035. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3036. if (ret) {
  3037. wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
  3038. ret, strerror(-ret));
  3039. } else {
  3040. bss->beacon_set = 1;
  3041. }
  3042. return ret;
  3043. nla_put_failure:
  3044. return -ENOBUFS;
  3045. }
  3046. static int wpa_driver_nl80211_set_freq(struct wpa_driver_nl80211_data *drv,
  3047. int freq, int ht_enabled,
  3048. int sec_channel_offset)
  3049. {
  3050. struct nl_msg *msg;
  3051. int ret;
  3052. msg = nlmsg_alloc();
  3053. if (!msg)
  3054. return -1;
  3055. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  3056. NL80211_CMD_SET_WIPHY, 0);
  3057. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3058. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
  3059. if (ht_enabled) {
  3060. switch (sec_channel_offset) {
  3061. case -1:
  3062. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  3063. NL80211_CHAN_HT40MINUS);
  3064. break;
  3065. case 1:
  3066. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  3067. NL80211_CHAN_HT40PLUS);
  3068. break;
  3069. default:
  3070. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  3071. NL80211_CHAN_HT20);
  3072. break;
  3073. }
  3074. }
  3075. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3076. if (ret == 0)
  3077. return 0;
  3078. wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
  3079. "%d (%s)", freq, ret, strerror(-ret));
  3080. nla_put_failure:
  3081. return -1;
  3082. }
  3083. static int wpa_driver_nl80211_sta_add(void *priv,
  3084. struct hostapd_sta_add_params *params)
  3085. {
  3086. struct i802_bss *bss = priv;
  3087. struct wpa_driver_nl80211_data *drv = bss->drv;
  3088. struct nl_msg *msg;
  3089. int ret = -ENOBUFS;
  3090. msg = nlmsg_alloc();
  3091. if (!msg)
  3092. return -ENOMEM;
  3093. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3094. 0, NL80211_CMD_NEW_STATION, 0);
  3095. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  3096. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
  3097. NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
  3098. NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
  3099. params->supp_rates);
  3100. NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
  3101. params->listen_interval);
  3102. if (params->ht_capabilities) {
  3103. NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
  3104. sizeof(*params->ht_capabilities),
  3105. params->ht_capabilities);
  3106. }
  3107. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3108. if (ret)
  3109. wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_NEW_STATION "
  3110. "result: %d (%s)", ret, strerror(-ret));
  3111. if (ret == -EEXIST)
  3112. ret = 0;
  3113. nla_put_failure:
  3114. return ret;
  3115. }
  3116. static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr)
  3117. {
  3118. struct i802_bss *bss = priv;
  3119. struct wpa_driver_nl80211_data *drv = bss->drv;
  3120. struct nl_msg *msg;
  3121. int ret;
  3122. msg = nlmsg_alloc();
  3123. if (!msg)
  3124. return -ENOMEM;
  3125. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3126. 0, NL80211_CMD_DEL_STATION, 0);
  3127. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  3128. if_nametoindex(bss->ifname));
  3129. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  3130. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3131. if (ret == -ENOENT)
  3132. return 0;
  3133. return ret;
  3134. nla_put_failure:
  3135. return -ENOBUFS;
  3136. }
  3137. static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
  3138. int ifidx)
  3139. {
  3140. struct nl_msg *msg;
  3141. wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
  3142. #ifdef HOSTAPD
  3143. /* stop listening for EAPOL on this interface */
  3144. del_ifidx(drv, ifidx);
  3145. #endif /* HOSTAPD */
  3146. msg = nlmsg_alloc();
  3147. if (!msg)
  3148. goto nla_put_failure;
  3149. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3150. 0, NL80211_CMD_DEL_INTERFACE, 0);
  3151. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
  3152. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  3153. return;
  3154. nla_put_failure:
  3155. wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
  3156. }
  3157. static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
  3158. const char *ifname,
  3159. enum nl80211_iftype iftype,
  3160. const u8 *addr, int wds)
  3161. {
  3162. struct nl_msg *msg, *flags = NULL;
  3163. int ifidx;
  3164. int ret = -ENOBUFS;
  3165. msg = nlmsg_alloc();
  3166. if (!msg)
  3167. return -1;
  3168. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3169. 0, NL80211_CMD_NEW_INTERFACE, 0);
  3170. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3171. NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
  3172. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
  3173. if (iftype == NL80211_IFTYPE_MONITOR) {
  3174. int err;
  3175. flags = nlmsg_alloc();
  3176. if (!flags)
  3177. goto nla_put_failure;
  3178. NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
  3179. err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
  3180. nlmsg_free(flags);
  3181. if (err)
  3182. goto nla_put_failure;
  3183. } else if (wds) {
  3184. NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
  3185. }
  3186. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3187. if (ret) {
  3188. nla_put_failure:
  3189. wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
  3190. ifname, ret, strerror(-ret));
  3191. return ret;
  3192. }
  3193. ifidx = if_nametoindex(ifname);
  3194. wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
  3195. ifname, ifidx);
  3196. if (ifidx <= 0)
  3197. return -1;
  3198. #ifdef HOSTAPD
  3199. /* start listening for EAPOL on this interface */
  3200. add_ifidx(drv, ifidx);
  3201. #endif /* HOSTAPD */
  3202. if (addr && iftype != NL80211_IFTYPE_MONITOR &&
  3203. linux_set_ifhwaddr(drv->ioctl_sock, ifname, addr)) {
  3204. nl80211_remove_iface(drv, ifidx);
  3205. return -1;
  3206. }
  3207. return ifidx;
  3208. }
  3209. static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
  3210. const char *ifname, enum nl80211_iftype iftype,
  3211. const u8 *addr, int wds)
  3212. {
  3213. int ret;
  3214. ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
  3215. /* if error occured and interface exists already */
  3216. if (ret == -ENFILE && if_nametoindex(ifname)) {
  3217. wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
  3218. /* Try to remove the interface that was already there. */
  3219. nl80211_remove_iface(drv, if_nametoindex(ifname));
  3220. /* Try to create the interface again */
  3221. ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
  3222. wds);
  3223. }
  3224. if (ret >= 0 && drv->disable_11b_rates)
  3225. nl80211_disable_11b_rates(drv, ret, 1);
  3226. return ret;
  3227. }
  3228. static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
  3229. {
  3230. struct ieee80211_hdr *hdr;
  3231. u16 fc;
  3232. union wpa_event_data event;
  3233. hdr = (struct ieee80211_hdr *) buf;
  3234. fc = le_to_host16(hdr->frame_control);
  3235. os_memset(&event, 0, sizeof(event));
  3236. event.tx_status.type = WLAN_FC_GET_TYPE(fc);
  3237. event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
  3238. event.tx_status.dst = hdr->addr1;
  3239. event.tx_status.data = buf;
  3240. event.tx_status.data_len = len;
  3241. event.tx_status.ack = ok;
  3242. wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
  3243. }
  3244. static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
  3245. u8 *buf, size_t len)
  3246. {
  3247. union wpa_event_data event;
  3248. os_memset(&event, 0, sizeof(event));
  3249. event.rx_from_unknown.frame = buf;
  3250. event.rx_from_unknown.len = len;
  3251. wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
  3252. }
  3253. static void handle_frame(struct wpa_driver_nl80211_data *drv,
  3254. u8 *buf, size_t len, int datarate, int ssi_signal)
  3255. {
  3256. struct ieee80211_hdr *hdr;
  3257. u16 fc;
  3258. union wpa_event_data event;
  3259. hdr = (struct ieee80211_hdr *) buf;
  3260. fc = le_to_host16(hdr->frame_control);
  3261. switch (WLAN_FC_GET_TYPE(fc)) {
  3262. case WLAN_FC_TYPE_MGMT:
  3263. os_memset(&event, 0, sizeof(event));
  3264. event.rx_mgmt.frame = buf;
  3265. event.rx_mgmt.frame_len = len;
  3266. event.rx_mgmt.datarate = datarate;
  3267. event.rx_mgmt.ssi_signal = ssi_signal;
  3268. wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
  3269. break;
  3270. case WLAN_FC_TYPE_CTRL:
  3271. /* can only get here with PS-Poll frames */
  3272. wpa_printf(MSG_DEBUG, "CTRL");
  3273. from_unknown_sta(drv, buf, len);
  3274. break;
  3275. case WLAN_FC_TYPE_DATA:
  3276. from_unknown_sta(drv, buf, len);
  3277. break;
  3278. }
  3279. }
  3280. static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
  3281. {
  3282. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  3283. int len;
  3284. unsigned char buf[3000];
  3285. struct ieee80211_radiotap_iterator iter;
  3286. int ret;
  3287. int datarate = 0, ssi_signal = 0;
  3288. int injected = 0, failed = 0, rxflags = 0;
  3289. len = recv(sock, buf, sizeof(buf), 0);
  3290. if (len < 0) {
  3291. perror("recv");
  3292. return;
  3293. }
  3294. if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
  3295. printf("received invalid radiotap frame\n");
  3296. return;
  3297. }
  3298. while (1) {
  3299. ret = ieee80211_radiotap_iterator_next(&iter);
  3300. if (ret == -ENOENT)
  3301. break;
  3302. if (ret) {
  3303. printf("received invalid radiotap frame (%d)\n", ret);
  3304. return;
  3305. }
  3306. switch (iter.this_arg_index) {
  3307. case IEEE80211_RADIOTAP_FLAGS:
  3308. if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
  3309. len -= 4;
  3310. break;
  3311. case IEEE80211_RADIOTAP_RX_FLAGS:
  3312. rxflags = 1;
  3313. break;
  3314. case IEEE80211_RADIOTAP_TX_FLAGS:
  3315. injected = 1;
  3316. failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
  3317. IEEE80211_RADIOTAP_F_TX_FAIL;
  3318. break;
  3319. case IEEE80211_RADIOTAP_DATA_RETRIES:
  3320. break;
  3321. case IEEE80211_RADIOTAP_CHANNEL:
  3322. /* TODO: convert from freq/flags to channel number */
  3323. break;
  3324. case IEEE80211_RADIOTAP_RATE:
  3325. datarate = *iter.this_arg * 5;
  3326. break;
  3327. case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
  3328. ssi_signal = *iter.this_arg;
  3329. break;
  3330. }
  3331. }
  3332. if (rxflags && injected)
  3333. return;
  3334. if (!injected)
  3335. handle_frame(drv, buf + iter.max_length,
  3336. len - iter.max_length, datarate, ssi_signal);
  3337. else
  3338. handle_tx_callback(drv->ctx, buf + iter.max_length,
  3339. len - iter.max_length, !failed);
  3340. }
  3341. /*
  3342. * we post-process the filter code later and rewrite
  3343. * this to the offset to the last instruction
  3344. */
  3345. #define PASS 0xFF
  3346. #define FAIL 0xFE
  3347. static struct sock_filter msock_filter_insns[] = {
  3348. /*
  3349. * do a little-endian load of the radiotap length field
  3350. */
  3351. /* load lower byte into A */
  3352. BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
  3353. /* put it into X (== index register) */
  3354. BPF_STMT(BPF_MISC| BPF_TAX, 0),
  3355. /* load upper byte into A */
  3356. BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
  3357. /* left-shift it by 8 */
  3358. BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
  3359. /* or with X */
  3360. BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
  3361. /* put result into X */
  3362. BPF_STMT(BPF_MISC| BPF_TAX, 0),
  3363. /*
  3364. * Allow management frames through, this also gives us those
  3365. * management frames that we sent ourselves with status
  3366. */
  3367. /* load the lower byte of the IEEE 802.11 frame control field */
  3368. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  3369. /* mask off frame type and version */
  3370. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
  3371. /* accept frame if it's both 0, fall through otherwise */
  3372. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
  3373. /*
  3374. * TODO: add a bit to radiotap RX flags that indicates
  3375. * that the sending station is not associated, then
  3376. * add a filter here that filters on our DA and that flag
  3377. * to allow us to deauth frames to that bad station.
  3378. *
  3379. * For now allow all To DS data frames through.
  3380. */
  3381. /* load the IEEE 802.11 frame control field */
  3382. BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
  3383. /* mask off frame type, version and DS status */
  3384. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
  3385. /* accept frame if version 0, type 2 and To DS, fall through otherwise
  3386. */
  3387. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
  3388. #if 0
  3389. /*
  3390. * drop non-data frames
  3391. */
  3392. /* load the lower byte of the frame control field */
  3393. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  3394. /* mask off QoS bit */
  3395. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
  3396. /* drop non-data frames */
  3397. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
  3398. #endif
  3399. /* load the upper byte of the frame control field */
  3400. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
  3401. /* mask off toDS/fromDS */
  3402. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
  3403. /* accept WDS frames */
  3404. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
  3405. /*
  3406. * add header length to index
  3407. */
  3408. /* load the lower byte of the frame control field */
  3409. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  3410. /* mask off QoS bit */
  3411. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
  3412. /* right shift it by 6 to give 0 or 2 */
  3413. BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
  3414. /* add data frame header length */
  3415. BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
  3416. /* add index, was start of 802.11 header */
  3417. BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
  3418. /* move to index, now start of LL header */
  3419. BPF_STMT(BPF_MISC | BPF_TAX, 0),
  3420. /*
  3421. * Accept empty data frames, we use those for
  3422. * polling activity.
  3423. */
  3424. BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
  3425. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
  3426. /*
  3427. * Accept EAPOL frames
  3428. */
  3429. BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
  3430. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
  3431. BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
  3432. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
  3433. /* keep these last two statements or change the code below */
  3434. /* return 0 == "DROP" */
  3435. BPF_STMT(BPF_RET | BPF_K, 0),
  3436. /* return ~0 == "keep all" */
  3437. BPF_STMT(BPF_RET | BPF_K, ~0),
  3438. };
  3439. static struct sock_fprog msock_filter = {
  3440. .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
  3441. .filter = msock_filter_insns,
  3442. };
  3443. static int add_monitor_filter(int s)
  3444. {
  3445. int idx;
  3446. /* rewrite all PASS/FAIL jump offsets */
  3447. for (idx = 0; idx < msock_filter.len; idx++) {
  3448. struct sock_filter *insn = &msock_filter_insns[idx];
  3449. if (BPF_CLASS(insn->code) == BPF_JMP) {
  3450. if (insn->code == (BPF_JMP|BPF_JA)) {
  3451. if (insn->k == PASS)
  3452. insn->k = msock_filter.len - idx - 2;
  3453. else if (insn->k == FAIL)
  3454. insn->k = msock_filter.len - idx - 3;
  3455. }
  3456. if (insn->jt == PASS)
  3457. insn->jt = msock_filter.len - idx - 2;
  3458. else if (insn->jt == FAIL)
  3459. insn->jt = msock_filter.len - idx - 3;
  3460. if (insn->jf == PASS)
  3461. insn->jf = msock_filter.len - idx - 2;
  3462. else if (insn->jf == FAIL)
  3463. insn->jf = msock_filter.len - idx - 3;
  3464. }
  3465. }
  3466. if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
  3467. &msock_filter, sizeof(msock_filter))) {
  3468. perror("SO_ATTACH_FILTER");
  3469. return -1;
  3470. }
  3471. return 0;
  3472. }
  3473. static void nl80211_remove_monitor_interface(
  3474. struct wpa_driver_nl80211_data *drv)
  3475. {
  3476. if (drv->monitor_ifidx >= 0) {
  3477. nl80211_remove_iface(drv, drv->monitor_ifidx);
  3478. drv->monitor_ifidx = -1;
  3479. }
  3480. if (drv->monitor_sock >= 0) {
  3481. eloop_unregister_read_sock(drv->monitor_sock);
  3482. close(drv->monitor_sock);
  3483. drv->monitor_sock = -1;
  3484. }
  3485. }
  3486. static int
  3487. nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
  3488. {
  3489. char buf[IFNAMSIZ];
  3490. struct sockaddr_ll ll;
  3491. int optval;
  3492. socklen_t optlen;
  3493. snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
  3494. buf[IFNAMSIZ - 1] = '\0';
  3495. drv->monitor_ifidx =
  3496. nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
  3497. 0);
  3498. if (drv->monitor_ifidx < 0)
  3499. return -1;
  3500. if (linux_set_iface_flags(drv->ioctl_sock, buf, 1))
  3501. goto error;
  3502. memset(&ll, 0, sizeof(ll));
  3503. ll.sll_family = AF_PACKET;
  3504. ll.sll_ifindex = drv->monitor_ifidx;
  3505. drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  3506. if (drv->monitor_sock < 0) {
  3507. perror("socket[PF_PACKET,SOCK_RAW]");
  3508. goto error;
  3509. }
  3510. if (add_monitor_filter(drv->monitor_sock)) {
  3511. wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
  3512. "interface; do filtering in user space");
  3513. /* This works, but will cost in performance. */
  3514. }
  3515. if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
  3516. perror("monitor socket bind");
  3517. goto error;
  3518. }
  3519. optlen = sizeof(optval);
  3520. optval = 20;
  3521. if (setsockopt
  3522. (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
  3523. perror("Failed to set socket priority");
  3524. goto error;
  3525. }
  3526. if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
  3527. drv, NULL)) {
  3528. printf("Could not register monitor read socket\n");
  3529. goto error;
  3530. }
  3531. return 0;
  3532. error:
  3533. nl80211_remove_monitor_interface(drv);
  3534. return -1;
  3535. }
  3536. static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  3537. static int wpa_driver_nl80211_hapd_send_eapol(
  3538. void *priv, const u8 *addr, const u8 *data,
  3539. size_t data_len, int encrypt, const u8 *own_addr)
  3540. {
  3541. struct i802_bss *bss = priv;
  3542. struct wpa_driver_nl80211_data *drv = bss->drv;
  3543. struct ieee80211_hdr *hdr;
  3544. size_t len;
  3545. u8 *pos;
  3546. int res;
  3547. #if 0 /* FIX */
  3548. int qos = sta->flags & WPA_STA_WMM;
  3549. #else
  3550. int qos = 0;
  3551. #endif
  3552. len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
  3553. data_len;
  3554. hdr = os_zalloc(len);
  3555. if (hdr == NULL) {
  3556. printf("malloc() failed for i802_send_data(len=%lu)\n",
  3557. (unsigned long) len);
  3558. return -1;
  3559. }
  3560. hdr->frame_control =
  3561. IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
  3562. hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
  3563. if (encrypt)
  3564. hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  3565. #if 0 /* To be enabled if qos determination is added above */
  3566. if (qos) {
  3567. hdr->frame_control |=
  3568. host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
  3569. }
  3570. #endif
  3571. memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
  3572. memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
  3573. memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
  3574. pos = (u8 *) (hdr + 1);
  3575. #if 0 /* To be enabled if qos determination is added above */
  3576. if (qos) {
  3577. /* add an empty QoS header if needed */
  3578. pos[0] = 0;
  3579. pos[1] = 0;
  3580. pos += 2;
  3581. }
  3582. #endif
  3583. memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
  3584. pos += sizeof(rfc1042_header);
  3585. WPA_PUT_BE16(pos, ETH_P_PAE);
  3586. pos += 2;
  3587. memcpy(pos, data, data_len);
  3588. res = wpa_driver_nl80211_send_frame(drv, (u8 *) hdr, len, encrypt);
  3589. if (res < 0) {
  3590. wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
  3591. "failed: %d (%s)",
  3592. (unsigned long) len, errno, strerror(errno));
  3593. }
  3594. os_free(hdr);
  3595. return res;
  3596. }
  3597. static u32 sta_flags_nl80211(int flags)
  3598. {
  3599. u32 f = 0;
  3600. if (flags & WPA_STA_AUTHORIZED)
  3601. f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
  3602. if (flags & WPA_STA_WMM)
  3603. f |= BIT(NL80211_STA_FLAG_WME);
  3604. if (flags & WPA_STA_SHORT_PREAMBLE)
  3605. f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
  3606. if (flags & WPA_STA_MFP)
  3607. f |= BIT(NL80211_STA_FLAG_MFP);
  3608. return f;
  3609. }
  3610. static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
  3611. int total_flags,
  3612. int flags_or, int flags_and)
  3613. {
  3614. struct i802_bss *bss = priv;
  3615. struct wpa_driver_nl80211_data *drv = bss->drv;
  3616. struct nl_msg *msg, *flags = NULL;
  3617. struct nl80211_sta_flag_update upd;
  3618. msg = nlmsg_alloc();
  3619. if (!msg)
  3620. return -ENOMEM;
  3621. flags = nlmsg_alloc();
  3622. if (!flags) {
  3623. nlmsg_free(msg);
  3624. return -ENOMEM;
  3625. }
  3626. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3627. 0, NL80211_CMD_SET_STATION, 0);
  3628. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  3629. if_nametoindex(bss->ifname));
  3630. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  3631. /*
  3632. * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
  3633. * can be removed eventually.
  3634. */
  3635. if (total_flags & WPA_STA_AUTHORIZED)
  3636. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
  3637. if (total_flags & WPA_STA_WMM)
  3638. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
  3639. if (total_flags & WPA_STA_SHORT_PREAMBLE)
  3640. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
  3641. if (total_flags & WPA_STA_MFP)
  3642. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
  3643. if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
  3644. goto nla_put_failure;
  3645. os_memset(&upd, 0, sizeof(upd));
  3646. upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
  3647. upd.set = sta_flags_nl80211(flags_or);
  3648. NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
  3649. nlmsg_free(flags);
  3650. return send_and_recv_msgs(drv, msg, NULL, NULL);
  3651. nla_put_failure:
  3652. nlmsg_free(flags);
  3653. return -ENOBUFS;
  3654. }
  3655. static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
  3656. struct wpa_driver_associate_params *params)
  3657. {
  3658. if (params->p2p)
  3659. wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
  3660. "group (GO)");
  3661. if (wpa_driver_nl80211_set_mode(&drv->first_bss, params->mode) ||
  3662. wpa_driver_nl80211_set_freq(drv, params->freq, 0, 0)) {
  3663. nl80211_remove_monitor_interface(drv);
  3664. return -1;
  3665. }
  3666. /* TODO: setup monitor interface (and add code somewhere to remove this
  3667. * when AP mode is stopped; associate with mode != 2 or drv_deinit) */
  3668. return 0;
  3669. }
  3670. static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
  3671. {
  3672. struct nl_msg *msg;
  3673. int ret = -1;
  3674. msg = nlmsg_alloc();
  3675. if (!msg)
  3676. return -1;
  3677. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  3678. NL80211_CMD_LEAVE_IBSS, 0);
  3679. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3680. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3681. msg = NULL;
  3682. if (ret) {
  3683. wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
  3684. "(%s)", ret, strerror(-ret));
  3685. goto nla_put_failure;
  3686. }
  3687. ret = 0;
  3688. wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
  3689. nla_put_failure:
  3690. nlmsg_free(msg);
  3691. return ret;
  3692. }
  3693. static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
  3694. struct wpa_driver_associate_params *params)
  3695. {
  3696. struct nl_msg *msg;
  3697. int ret = -1;
  3698. int count = 0;
  3699. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
  3700. if (wpa_driver_nl80211_set_mode(&drv->first_bss, params->mode)) {
  3701. wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
  3702. "IBSS mode");
  3703. return -1;
  3704. }
  3705. retry:
  3706. msg = nlmsg_alloc();
  3707. if (!msg)
  3708. return -1;
  3709. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  3710. NL80211_CMD_JOIN_IBSS, 0);
  3711. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3712. if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
  3713. goto nla_put_failure;
  3714. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  3715. params->ssid, params->ssid_len);
  3716. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  3717. params->ssid);
  3718. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  3719. drv->ssid_len = params->ssid_len;
  3720. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  3721. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  3722. ret = nl80211_set_conn_keys(params, msg);
  3723. if (ret)
  3724. goto nla_put_failure;
  3725. if (params->wpa_ie) {
  3726. wpa_hexdump(MSG_DEBUG,
  3727. " * Extra IEs for Beacon/Probe Response frames",
  3728. params->wpa_ie, params->wpa_ie_len);
  3729. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  3730. params->wpa_ie);
  3731. }
  3732. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3733. msg = NULL;
  3734. if (ret) {
  3735. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
  3736. ret, strerror(-ret));
  3737. count++;
  3738. if (ret == -EALREADY && count == 1) {
  3739. wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
  3740. "forced leave");
  3741. nl80211_leave_ibss(drv);
  3742. nlmsg_free(msg);
  3743. goto retry;
  3744. }
  3745. goto nla_put_failure;
  3746. }
  3747. ret = 0;
  3748. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
  3749. nla_put_failure:
  3750. nlmsg_free(msg);
  3751. return ret;
  3752. }
  3753. static int wpa_driver_nl80211_connect(
  3754. struct wpa_driver_nl80211_data *drv,
  3755. struct wpa_driver_associate_params *params)
  3756. {
  3757. struct nl_msg *msg;
  3758. enum nl80211_auth_type type;
  3759. int ret = 0;
  3760. msg = nlmsg_alloc();
  3761. if (!msg)
  3762. return -1;
  3763. wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
  3764. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  3765. NL80211_CMD_CONNECT, 0);
  3766. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3767. if (params->bssid) {
  3768. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  3769. MAC2STR(params->bssid));
  3770. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  3771. }
  3772. if (params->freq) {
  3773. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  3774. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  3775. }
  3776. if (params->ssid) {
  3777. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  3778. params->ssid, params->ssid_len);
  3779. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  3780. params->ssid);
  3781. if (params->ssid_len > sizeof(drv->ssid))
  3782. goto nla_put_failure;
  3783. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  3784. drv->ssid_len = params->ssid_len;
  3785. }
  3786. wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
  3787. if (params->wpa_ie)
  3788. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  3789. params->wpa_ie);
  3790. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  3791. type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  3792. else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  3793. type = NL80211_AUTHTYPE_SHARED_KEY;
  3794. else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  3795. type = NL80211_AUTHTYPE_NETWORK_EAP;
  3796. else if (params->auth_alg & WPA_AUTH_ALG_FT)
  3797. type = NL80211_AUTHTYPE_FT;
  3798. else
  3799. goto nla_put_failure;
  3800. wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
  3801. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
  3802. if (params->wpa_ie && params->wpa_ie_len) {
  3803. enum nl80211_wpa_versions ver;
  3804. if (params->wpa_ie[0] == WLAN_EID_RSN)
  3805. ver = NL80211_WPA_VERSION_2;
  3806. else
  3807. ver = NL80211_WPA_VERSION_1;
  3808. wpa_printf(MSG_DEBUG, " * WPA Version %d", ver);
  3809. NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
  3810. }
  3811. if (params->pairwise_suite != CIPHER_NONE) {
  3812. int cipher;
  3813. switch (params->pairwise_suite) {
  3814. case CIPHER_WEP40:
  3815. cipher = WLAN_CIPHER_SUITE_WEP40;
  3816. break;
  3817. case CIPHER_WEP104:
  3818. cipher = WLAN_CIPHER_SUITE_WEP104;
  3819. break;
  3820. case CIPHER_CCMP:
  3821. cipher = WLAN_CIPHER_SUITE_CCMP;
  3822. break;
  3823. case CIPHER_TKIP:
  3824. default:
  3825. cipher = WLAN_CIPHER_SUITE_TKIP;
  3826. break;
  3827. }
  3828. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
  3829. }
  3830. if (params->group_suite != CIPHER_NONE) {
  3831. int cipher;
  3832. switch (params->group_suite) {
  3833. case CIPHER_WEP40:
  3834. cipher = WLAN_CIPHER_SUITE_WEP40;
  3835. break;
  3836. case CIPHER_WEP104:
  3837. cipher = WLAN_CIPHER_SUITE_WEP104;
  3838. break;
  3839. case CIPHER_CCMP:
  3840. cipher = WLAN_CIPHER_SUITE_CCMP;
  3841. break;
  3842. case CIPHER_TKIP:
  3843. default:
  3844. cipher = WLAN_CIPHER_SUITE_TKIP;
  3845. break;
  3846. }
  3847. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
  3848. }
  3849. if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
  3850. params->key_mgmt_suite == KEY_MGMT_PSK) {
  3851. int mgmt = WLAN_AKM_SUITE_PSK;
  3852. switch (params->key_mgmt_suite) {
  3853. case KEY_MGMT_802_1X:
  3854. mgmt = WLAN_AKM_SUITE_8021X;
  3855. break;
  3856. case KEY_MGMT_PSK:
  3857. default:
  3858. mgmt = WLAN_AKM_SUITE_PSK;
  3859. break;
  3860. }
  3861. NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
  3862. }
  3863. ret = nl80211_set_conn_keys(params, msg);
  3864. if (ret)
  3865. goto nla_put_failure;
  3866. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3867. msg = NULL;
  3868. if (ret) {
  3869. wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
  3870. "(%s)", ret, strerror(-ret));
  3871. goto nla_put_failure;
  3872. }
  3873. ret = 0;
  3874. wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
  3875. nla_put_failure:
  3876. nlmsg_free(msg);
  3877. return ret;
  3878. }
  3879. static int wpa_driver_nl80211_associate(
  3880. void *priv, struct wpa_driver_associate_params *params)
  3881. {
  3882. struct i802_bss *bss = priv;
  3883. struct wpa_driver_nl80211_data *drv = bss->drv;
  3884. int ret = -1;
  3885. struct nl_msg *msg;
  3886. if (params->mode == IEEE80211_MODE_AP)
  3887. return wpa_driver_nl80211_ap(drv, params);
  3888. if (params->mode == IEEE80211_MODE_IBSS)
  3889. return wpa_driver_nl80211_ibss(drv, params);
  3890. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
  3891. if (wpa_driver_nl80211_set_mode(priv, params->mode) < 0)
  3892. return -1;
  3893. return wpa_driver_nl80211_connect(drv, params);
  3894. }
  3895. drv->associated = 0;
  3896. msg = nlmsg_alloc();
  3897. if (!msg)
  3898. return -1;
  3899. wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
  3900. drv->ifindex);
  3901. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  3902. NL80211_CMD_ASSOCIATE, 0);
  3903. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3904. if (params->bssid) {
  3905. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  3906. MAC2STR(params->bssid));
  3907. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  3908. }
  3909. if (params->freq) {
  3910. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  3911. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  3912. drv->assoc_freq = params->freq;
  3913. } else
  3914. drv->assoc_freq = 0;
  3915. if (params->ssid) {
  3916. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  3917. params->ssid, params->ssid_len);
  3918. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  3919. params->ssid);
  3920. if (params->ssid_len > sizeof(drv->ssid))
  3921. goto nla_put_failure;
  3922. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  3923. drv->ssid_len = params->ssid_len;
  3924. }
  3925. wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
  3926. if (params->wpa_ie)
  3927. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  3928. params->wpa_ie);
  3929. if (params->pairwise_suite != CIPHER_NONE) {
  3930. int cipher;
  3931. switch (params->pairwise_suite) {
  3932. case CIPHER_WEP40:
  3933. cipher = WLAN_CIPHER_SUITE_WEP40;
  3934. break;
  3935. case CIPHER_WEP104:
  3936. cipher = WLAN_CIPHER_SUITE_WEP104;
  3937. break;
  3938. case CIPHER_CCMP:
  3939. cipher = WLAN_CIPHER_SUITE_CCMP;
  3940. break;
  3941. case CIPHER_TKIP:
  3942. default:
  3943. cipher = WLAN_CIPHER_SUITE_TKIP;
  3944. break;
  3945. }
  3946. wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
  3947. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
  3948. }
  3949. if (params->group_suite != CIPHER_NONE) {
  3950. int cipher;
  3951. switch (params->group_suite) {
  3952. case CIPHER_WEP40:
  3953. cipher = WLAN_CIPHER_SUITE_WEP40;
  3954. break;
  3955. case CIPHER_WEP104:
  3956. cipher = WLAN_CIPHER_SUITE_WEP104;
  3957. break;
  3958. case CIPHER_CCMP:
  3959. cipher = WLAN_CIPHER_SUITE_CCMP;
  3960. break;
  3961. case CIPHER_TKIP:
  3962. default:
  3963. cipher = WLAN_CIPHER_SUITE_TKIP;
  3964. break;
  3965. }
  3966. wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
  3967. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
  3968. }
  3969. #ifdef CONFIG_IEEE80211W
  3970. if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
  3971. NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
  3972. #endif /* CONFIG_IEEE80211W */
  3973. NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
  3974. if (params->prev_bssid) {
  3975. wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
  3976. MAC2STR(params->prev_bssid));
  3977. NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
  3978. params->prev_bssid);
  3979. }
  3980. if (params->p2p)
  3981. wpa_printf(MSG_DEBUG, " * P2P group");
  3982. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3983. msg = NULL;
  3984. if (ret) {
  3985. wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
  3986. "(%s)", ret, strerror(-ret));
  3987. nl80211_dump_scan(drv);
  3988. goto nla_put_failure;
  3989. }
  3990. ret = 0;
  3991. wpa_printf(MSG_DEBUG, "nl80211: Association request send "
  3992. "successfully");
  3993. nla_put_failure:
  3994. nlmsg_free(msg);
  3995. return ret;
  3996. }
  3997. static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
  3998. int ifindex, int mode)
  3999. {
  4000. struct nl_msg *msg;
  4001. int ret = -ENOBUFS;
  4002. msg = nlmsg_alloc();
  4003. if (!msg)
  4004. return -ENOMEM;
  4005. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4006. 0, NL80211_CMD_SET_INTERFACE, 0);
  4007. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  4008. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
  4009. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4010. if (!ret)
  4011. return 0;
  4012. nla_put_failure:
  4013. wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
  4014. " %d (%s)", ifindex, mode, ret, strerror(-ret));
  4015. return ret;
  4016. }
  4017. static int wpa_driver_nl80211_set_mode(void *priv, int mode)
  4018. {
  4019. struct i802_bss *bss = priv;
  4020. struct wpa_driver_nl80211_data *drv = bss->drv;
  4021. int ret = -1;
  4022. int nlmode;
  4023. int i;
  4024. switch (mode) {
  4025. case 0:
  4026. nlmode = NL80211_IFTYPE_STATION;
  4027. break;
  4028. case 1:
  4029. nlmode = NL80211_IFTYPE_ADHOC;
  4030. break;
  4031. case 2:
  4032. nlmode = NL80211_IFTYPE_AP;
  4033. break;
  4034. default:
  4035. return -1;
  4036. }
  4037. if (nl80211_set_mode(drv, drv->ifindex, nlmode) == 0) {
  4038. drv->nlmode = nlmode;
  4039. ret = 0;
  4040. goto done;
  4041. }
  4042. if (nlmode == drv->nlmode) {
  4043. wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
  4044. "requested mode - ignore error");
  4045. ret = 0;
  4046. goto done; /* Already in the requested mode */
  4047. }
  4048. /* mac80211 doesn't allow mode changes while the device is up, so
  4049. * take the device down, try to set the mode again, and bring the
  4050. * device back up.
  4051. */
  4052. wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
  4053. "interface down");
  4054. for (i = 0; i < 10; i++) {
  4055. if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 0) ==
  4056. 0) {
  4057. /* Try to set the mode again while the interface is
  4058. * down */
  4059. ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
  4060. if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname,
  4061. 1))
  4062. ret = -1;
  4063. if (!ret)
  4064. break;
  4065. } else
  4066. wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
  4067. "interface down");
  4068. os_sleep(0, 100000);
  4069. }
  4070. if (!ret) {
  4071. wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
  4072. "interface is down");
  4073. drv->nlmode = nlmode;
  4074. }
  4075. done:
  4076. if (!ret && nlmode == NL80211_IFTYPE_AP) {
  4077. /* Setup additional AP mode functionality if needed */
  4078. if (drv->monitor_ifidx < 0 &&
  4079. nl80211_create_monitor_interface(drv))
  4080. return -1;
  4081. } else if (!ret && nlmode != NL80211_IFTYPE_AP) {
  4082. /* Remove additional AP mode functionality */
  4083. nl80211_remove_monitor_interface(drv);
  4084. bss->beacon_set = 0;
  4085. }
  4086. if (ret)
  4087. wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
  4088. "from %d failed", nlmode, drv->nlmode);
  4089. return ret;
  4090. }
  4091. static int wpa_driver_nl80211_get_capa(void *priv,
  4092. struct wpa_driver_capa *capa)
  4093. {
  4094. struct i802_bss *bss = priv;
  4095. struct wpa_driver_nl80211_data *drv = bss->drv;
  4096. if (!drv->has_capability)
  4097. return -1;
  4098. os_memcpy(capa, &drv->capa, sizeof(*capa));
  4099. return 0;
  4100. }
  4101. static int wpa_driver_nl80211_set_operstate(void *priv, int state)
  4102. {
  4103. struct i802_bss *bss = priv;
  4104. struct wpa_driver_nl80211_data *drv = bss->drv;
  4105. wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
  4106. __func__, drv->operstate, state, state ? "UP" : "DORMANT");
  4107. drv->operstate = state;
  4108. return netlink_send_oper_ifla(drv->netlink, drv->ifindex, -1,
  4109. state ? IF_OPER_UP : IF_OPER_DORMANT);
  4110. }
  4111. static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
  4112. {
  4113. struct i802_bss *bss = priv;
  4114. struct wpa_driver_nl80211_data *drv = bss->drv;
  4115. struct nl_msg *msg;
  4116. struct nl80211_sta_flag_update upd;
  4117. msg = nlmsg_alloc();
  4118. if (!msg)
  4119. return -ENOMEM;
  4120. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4121. 0, NL80211_CMD_SET_STATION, 0);
  4122. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  4123. if_nametoindex(bss->ifname));
  4124. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
  4125. os_memset(&upd, 0, sizeof(upd));
  4126. upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
  4127. if (authorized)
  4128. upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
  4129. NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
  4130. return send_and_recv_msgs(drv, msg, NULL, NULL);
  4131. nla_put_failure:
  4132. return -ENOBUFS;
  4133. }
  4134. #ifdef HOSTAPD
  4135. static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  4136. {
  4137. int i;
  4138. int *old;
  4139. wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
  4140. ifidx);
  4141. for (i = 0; i < drv->num_if_indices; i++) {
  4142. if (drv->if_indices[i] == 0) {
  4143. drv->if_indices[i] = ifidx;
  4144. return;
  4145. }
  4146. }
  4147. if (drv->if_indices != drv->default_if_indices)
  4148. old = drv->if_indices;
  4149. else
  4150. old = NULL;
  4151. drv->if_indices = os_realloc(old,
  4152. sizeof(int) * (drv->num_if_indices + 1));
  4153. if (!drv->if_indices) {
  4154. if (!old)
  4155. drv->if_indices = drv->default_if_indices;
  4156. else
  4157. drv->if_indices = old;
  4158. wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
  4159. "interfaces");
  4160. wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
  4161. return;
  4162. } else if (!old)
  4163. os_memcpy(drv->if_indices, drv->default_if_indices,
  4164. sizeof(drv->default_if_indices));
  4165. drv->if_indices[drv->num_if_indices] = ifidx;
  4166. drv->num_if_indices++;
  4167. }
  4168. static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  4169. {
  4170. int i;
  4171. for (i = 0; i < drv->num_if_indices; i++) {
  4172. if (drv->if_indices[i] == ifidx) {
  4173. drv->if_indices[i] = 0;
  4174. break;
  4175. }
  4176. }
  4177. }
  4178. static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  4179. {
  4180. int i;
  4181. for (i = 0; i < drv->num_if_indices; i++)
  4182. if (drv->if_indices[i] == ifidx)
  4183. return 1;
  4184. return 0;
  4185. }
  4186. static inline int min_int(int a, int b)
  4187. {
  4188. if (a < b)
  4189. return a;
  4190. return b;
  4191. }
  4192. static int get_key_handler(struct nl_msg *msg, void *arg)
  4193. {
  4194. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  4195. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  4196. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  4197. genlmsg_attrlen(gnlh, 0), NULL);
  4198. /*
  4199. * TODO: validate the key index and mac address!
  4200. * Otherwise, there's a race condition as soon as
  4201. * the kernel starts sending key notifications.
  4202. */
  4203. if (tb[NL80211_ATTR_KEY_SEQ])
  4204. memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
  4205. min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
  4206. return NL_SKIP;
  4207. }
  4208. static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
  4209. int idx, u8 *seq)
  4210. {
  4211. struct i802_bss *bss = priv;
  4212. struct wpa_driver_nl80211_data *drv = bss->drv;
  4213. struct nl_msg *msg;
  4214. msg = nlmsg_alloc();
  4215. if (!msg)
  4216. return -ENOMEM;
  4217. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4218. 0, NL80211_CMD_GET_KEY, 0);
  4219. if (addr)
  4220. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  4221. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
  4222. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
  4223. memset(seq, 0, 6);
  4224. return send_and_recv_msgs(drv, msg, get_key_handler, seq);
  4225. nla_put_failure:
  4226. return -ENOBUFS;
  4227. }
  4228. static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
  4229. int mode)
  4230. {
  4231. struct i802_bss *bss = priv;
  4232. struct wpa_driver_nl80211_data *drv = bss->drv;
  4233. struct nl_msg *msg;
  4234. u8 rates[NL80211_MAX_SUPP_RATES];
  4235. u8 rates_len = 0;
  4236. int i;
  4237. msg = nlmsg_alloc();
  4238. if (!msg)
  4239. return -ENOMEM;
  4240. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  4241. NL80211_CMD_SET_BSS, 0);
  4242. for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
  4243. rates[rates_len++] = basic_rates[i] / 5;
  4244. NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
  4245. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  4246. return send_and_recv_msgs(drv, msg, NULL, NULL);
  4247. nla_put_failure:
  4248. return -ENOBUFS;
  4249. }
  4250. #endif /* HOSTAPD */
  4251. /* Set kernel driver on given frequency (MHz) */
  4252. static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
  4253. {
  4254. struct i802_bss *bss = priv;
  4255. struct wpa_driver_nl80211_data *drv = bss->drv;
  4256. return wpa_driver_nl80211_set_freq(drv, freq->freq, freq->ht_enabled,
  4257. freq->sec_channel_offset);
  4258. }
  4259. #ifdef HOSTAPD
  4260. static int i802_set_rts(void *priv, int rts)
  4261. {
  4262. struct i802_bss *bss = priv;
  4263. struct wpa_driver_nl80211_data *drv = bss->drv;
  4264. struct nl_msg *msg;
  4265. int ret = -ENOBUFS;
  4266. u32 val;
  4267. msg = nlmsg_alloc();
  4268. if (!msg)
  4269. return -ENOMEM;
  4270. if (rts >= 2347)
  4271. val = (u32) -1;
  4272. else
  4273. val = rts;
  4274. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4275. 0, NL80211_CMD_SET_WIPHY, 0);
  4276. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  4277. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
  4278. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4279. if (!ret)
  4280. return 0;
  4281. nla_put_failure:
  4282. wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
  4283. "%d (%s)", rts, ret, strerror(-ret));
  4284. return ret;
  4285. }
  4286. static int i802_set_frag(void *priv, int frag)
  4287. {
  4288. struct i802_bss *bss = priv;
  4289. struct wpa_driver_nl80211_data *drv = bss->drv;
  4290. struct nl_msg *msg;
  4291. int ret = -ENOBUFS;
  4292. u32 val;
  4293. msg = nlmsg_alloc();
  4294. if (!msg)
  4295. return -ENOMEM;
  4296. if (frag >= 2346)
  4297. val = (u32) -1;
  4298. else
  4299. val = frag;
  4300. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4301. 0, NL80211_CMD_SET_WIPHY, 0);
  4302. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  4303. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
  4304. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4305. if (!ret)
  4306. return 0;
  4307. nla_put_failure:
  4308. wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
  4309. "%d: %d (%s)", frag, ret, strerror(-ret));
  4310. return ret;
  4311. }
  4312. static int i802_flush(void *priv)
  4313. {
  4314. struct i802_bss *bss = priv;
  4315. struct wpa_driver_nl80211_data *drv = bss->drv;
  4316. struct nl_msg *msg;
  4317. msg = nlmsg_alloc();
  4318. if (!msg)
  4319. return -1;
  4320. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4321. 0, NL80211_CMD_DEL_STATION, 0);
  4322. /*
  4323. * XXX: FIX! this needs to flush all VLANs too
  4324. */
  4325. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  4326. if_nametoindex(bss->ifname));
  4327. return send_and_recv_msgs(drv, msg, NULL, NULL);
  4328. nla_put_failure:
  4329. return -ENOBUFS;
  4330. }
  4331. static int get_sta_handler(struct nl_msg *msg, void *arg)
  4332. {
  4333. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  4334. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  4335. struct hostap_sta_driver_data *data = arg;
  4336. struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
  4337. static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
  4338. [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
  4339. [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
  4340. [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
  4341. [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
  4342. [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
  4343. };
  4344. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  4345. genlmsg_attrlen(gnlh, 0), NULL);
  4346. /*
  4347. * TODO: validate the interface and mac address!
  4348. * Otherwise, there's a race condition as soon as
  4349. * the kernel starts sending station notifications.
  4350. */
  4351. if (!tb[NL80211_ATTR_STA_INFO]) {
  4352. wpa_printf(MSG_DEBUG, "sta stats missing!");
  4353. return NL_SKIP;
  4354. }
  4355. if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
  4356. tb[NL80211_ATTR_STA_INFO],
  4357. stats_policy)) {
  4358. wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
  4359. return NL_SKIP;
  4360. }
  4361. if (stats[NL80211_STA_INFO_INACTIVE_TIME])
  4362. data->inactive_msec =
  4363. nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
  4364. if (stats[NL80211_STA_INFO_RX_BYTES])
  4365. data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
  4366. if (stats[NL80211_STA_INFO_TX_BYTES])
  4367. data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
  4368. if (stats[NL80211_STA_INFO_RX_PACKETS])
  4369. data->rx_packets =
  4370. nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
  4371. if (stats[NL80211_STA_INFO_TX_PACKETS])
  4372. data->tx_packets =
  4373. nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
  4374. return NL_SKIP;
  4375. }
  4376. static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
  4377. const u8 *addr)
  4378. {
  4379. struct i802_bss *bss = priv;
  4380. struct wpa_driver_nl80211_data *drv = bss->drv;
  4381. struct nl_msg *msg;
  4382. os_memset(data, 0, sizeof(*data));
  4383. msg = nlmsg_alloc();
  4384. if (!msg)
  4385. return -ENOMEM;
  4386. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4387. 0, NL80211_CMD_GET_STATION, 0);
  4388. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  4389. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  4390. return send_and_recv_msgs(drv, msg, get_sta_handler, data);
  4391. nla_put_failure:
  4392. return -ENOBUFS;
  4393. }
  4394. static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
  4395. int cw_min, int cw_max, int burst_time)
  4396. {
  4397. struct i802_bss *bss = priv;
  4398. struct wpa_driver_nl80211_data *drv = bss->drv;
  4399. struct nl_msg *msg;
  4400. struct nlattr *txq, *params;
  4401. msg = nlmsg_alloc();
  4402. if (!msg)
  4403. return -1;
  4404. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4405. 0, NL80211_CMD_SET_WIPHY, 0);
  4406. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  4407. txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
  4408. if (!txq)
  4409. goto nla_put_failure;
  4410. /* We are only sending parameters for a single TXQ at a time */
  4411. params = nla_nest_start(msg, 1);
  4412. if (!params)
  4413. goto nla_put_failure;
  4414. switch (queue) {
  4415. case 0:
  4416. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
  4417. break;
  4418. case 1:
  4419. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
  4420. break;
  4421. case 2:
  4422. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
  4423. break;
  4424. case 3:
  4425. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
  4426. break;
  4427. }
  4428. /* Burst time is configured in units of 0.1 msec and TXOP parameter in
  4429. * 32 usec, so need to convert the value here. */
  4430. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
  4431. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
  4432. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
  4433. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
  4434. nla_nest_end(msg, params);
  4435. nla_nest_end(msg, txq);
  4436. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  4437. return 0;
  4438. nla_put_failure:
  4439. return -1;
  4440. }
  4441. static int i802_set_bss(void *priv, int cts, int preamble, int slot,
  4442. int ht_opmode)
  4443. {
  4444. struct i802_bss *bss = priv;
  4445. struct wpa_driver_nl80211_data *drv = bss->drv;
  4446. struct nl_msg *msg;
  4447. msg = nlmsg_alloc();
  4448. if (!msg)
  4449. return -ENOMEM;
  4450. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  4451. NL80211_CMD_SET_BSS, 0);
  4452. if (cts >= 0)
  4453. NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
  4454. if (preamble >= 0)
  4455. NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
  4456. if (slot >= 0)
  4457. NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
  4458. if (ht_opmode >= 0)
  4459. NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
  4460. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  4461. return send_and_recv_msgs(drv, msg, NULL, NULL);
  4462. nla_put_failure:
  4463. return -ENOBUFS;
  4464. }
  4465. static int i802_set_cts_protect(void *priv, int value)
  4466. {
  4467. return i802_set_bss(priv, value, -1, -1, -1);
  4468. }
  4469. static int i802_set_preamble(void *priv, int value)
  4470. {
  4471. return i802_set_bss(priv, -1, value, -1, -1);
  4472. }
  4473. static int i802_set_short_slot_time(void *priv, int value)
  4474. {
  4475. return i802_set_bss(priv, -1, -1, value, -1);
  4476. }
  4477. static int i802_set_sta_vlan(void *priv, const u8 *addr,
  4478. const char *ifname, int vlan_id)
  4479. {
  4480. struct i802_bss *bss = priv;
  4481. struct wpa_driver_nl80211_data *drv = bss->drv;
  4482. struct nl_msg *msg;
  4483. int ret = -ENOBUFS;
  4484. msg = nlmsg_alloc();
  4485. if (!msg)
  4486. return -ENOMEM;
  4487. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4488. 0, NL80211_CMD_SET_STATION, 0);
  4489. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  4490. if_nametoindex(bss->ifname));
  4491. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  4492. NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
  4493. if_nametoindex(ifname));
  4494. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4495. if (ret < 0) {
  4496. wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
  4497. MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
  4498. MAC2STR(addr), ifname, vlan_id, ret,
  4499. strerror(-ret));
  4500. }
  4501. nla_put_failure:
  4502. return ret;
  4503. }
  4504. static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
  4505. const char *bridge_ifname)
  4506. {
  4507. struct i802_bss *bss = priv;
  4508. struct wpa_driver_nl80211_data *drv = bss->drv;
  4509. char name[IFNAMSIZ + 1];
  4510. os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
  4511. wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
  4512. " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
  4513. if (val) {
  4514. if (!if_nametoindex(name)) {
  4515. if (nl80211_create_iface(drv, name,
  4516. NL80211_IFTYPE_AP_VLAN,
  4517. NULL, 1) < 0)
  4518. return -1;
  4519. if (bridge_ifname &&
  4520. linux_br_add_if(drv->ioctl_sock, bridge_ifname,
  4521. name) < 0)
  4522. return -1;
  4523. }
  4524. linux_set_iface_flags(drv->ioctl_sock, name, 1);
  4525. return i802_set_sta_vlan(priv, addr, name, 0);
  4526. } else {
  4527. i802_set_sta_vlan(priv, addr, bss->ifname, 0);
  4528. return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
  4529. name);
  4530. }
  4531. }
  4532. static int i802_set_ht_params(void *priv, const u8 *ht_capab,
  4533. size_t ht_capab_len, const u8 *ht_oper,
  4534. size_t ht_oper_len)
  4535. {
  4536. if (ht_oper_len >= 6) {
  4537. /* ht opmode uses 16bit in octet 5 & 6 */
  4538. u16 ht_opmode = le_to_host16(((u16 *) ht_oper)[2]);
  4539. return i802_set_bss(priv, -1, -1, -1, ht_opmode);
  4540. } else
  4541. return -1;
  4542. }
  4543. static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
  4544. {
  4545. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  4546. struct sockaddr_ll lladdr;
  4547. unsigned char buf[3000];
  4548. int len;
  4549. socklen_t fromlen = sizeof(lladdr);
  4550. len = recvfrom(sock, buf, sizeof(buf), 0,
  4551. (struct sockaddr *)&lladdr, &fromlen);
  4552. if (len < 0) {
  4553. perror("recv");
  4554. return;
  4555. }
  4556. if (have_ifidx(drv, lladdr.sll_ifindex))
  4557. drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
  4558. }
  4559. static int i802_get_inact_sec(void *priv, const u8 *addr)
  4560. {
  4561. struct hostap_sta_driver_data data;
  4562. int ret;
  4563. data.inactive_msec = (unsigned long) -1;
  4564. ret = i802_read_sta_data(priv, &data, addr);
  4565. if (ret || data.inactive_msec == (unsigned long) -1)
  4566. return -1;
  4567. return data.inactive_msec / 1000;
  4568. }
  4569. static int i802_sta_clear_stats(void *priv, const u8 *addr)
  4570. {
  4571. #if 0
  4572. /* TODO */
  4573. #endif
  4574. return 0;
  4575. }
  4576. static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
  4577. int reason)
  4578. {
  4579. struct i802_bss *bss = priv;
  4580. struct ieee80211_mgmt mgmt;
  4581. memset(&mgmt, 0, sizeof(mgmt));
  4582. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  4583. WLAN_FC_STYPE_DEAUTH);
  4584. memcpy(mgmt.da, addr, ETH_ALEN);
  4585. memcpy(mgmt.sa, own_addr, ETH_ALEN);
  4586. memcpy(mgmt.bssid, own_addr, ETH_ALEN);
  4587. mgmt.u.deauth.reason_code = host_to_le16(reason);
  4588. return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
  4589. IEEE80211_HDRLEN +
  4590. sizeof(mgmt.u.deauth));
  4591. }
  4592. static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
  4593. int reason)
  4594. {
  4595. struct i802_bss *bss = priv;
  4596. struct ieee80211_mgmt mgmt;
  4597. memset(&mgmt, 0, sizeof(mgmt));
  4598. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  4599. WLAN_FC_STYPE_DISASSOC);
  4600. memcpy(mgmt.da, addr, ETH_ALEN);
  4601. memcpy(mgmt.sa, own_addr, ETH_ALEN);
  4602. memcpy(mgmt.bssid, own_addr, ETH_ALEN);
  4603. mgmt.u.disassoc.reason_code = host_to_le16(reason);
  4604. return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
  4605. IEEE80211_HDRLEN +
  4606. sizeof(mgmt.u.disassoc));
  4607. }
  4608. static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
  4609. const char *brname, const char *ifname)
  4610. {
  4611. int ifindex;
  4612. char in_br[IFNAMSIZ];
  4613. os_strlcpy(drv->brname, brname, IFNAMSIZ);
  4614. ifindex = if_nametoindex(brname);
  4615. if (ifindex == 0) {
  4616. /*
  4617. * Bridge was configured, but the bridge device does
  4618. * not exist. Try to add it now.
  4619. */
  4620. if (linux_br_add(drv->ioctl_sock, brname) < 0) {
  4621. wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
  4622. "bridge interface %s: %s",
  4623. brname, strerror(errno));
  4624. return -1;
  4625. }
  4626. drv->added_bridge = 1;
  4627. add_ifidx(drv, if_nametoindex(brname));
  4628. }
  4629. if (linux_br_get(in_br, ifname) == 0) {
  4630. if (os_strcmp(in_br, brname) == 0)
  4631. return 0; /* already in the bridge */
  4632. wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
  4633. "bridge %s", ifname, in_br);
  4634. if (linux_br_del_if(drv->ioctl_sock, in_br, ifname) < 0) {
  4635. wpa_printf(MSG_ERROR, "nl80211: Failed to "
  4636. "remove interface %s from bridge "
  4637. "%s: %s",
  4638. ifname, brname, strerror(errno));
  4639. return -1;
  4640. }
  4641. }
  4642. wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
  4643. ifname, brname);
  4644. if (linux_br_add_if(drv->ioctl_sock, brname, ifname) < 0) {
  4645. wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
  4646. "into bridge %s: %s",
  4647. ifname, brname, strerror(errno));
  4648. return -1;
  4649. }
  4650. drv->added_if_into_bridge = 1;
  4651. return 0;
  4652. }
  4653. static void *i802_init(struct hostapd_data *hapd,
  4654. struct wpa_init_params *params)
  4655. {
  4656. struct wpa_driver_nl80211_data *drv;
  4657. struct i802_bss *bss;
  4658. size_t i;
  4659. char brname[IFNAMSIZ];
  4660. int ifindex, br_ifindex;
  4661. int br_added = 0;
  4662. bss = wpa_driver_nl80211_init(hapd, params->ifname, NULL);
  4663. if (bss == NULL)
  4664. return NULL;
  4665. drv = bss->drv;
  4666. drv->nlmode = NL80211_IFTYPE_AP;
  4667. if (linux_br_get(brname, params->ifname) == 0) {
  4668. wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
  4669. params->ifname, brname);
  4670. br_ifindex = if_nametoindex(brname);
  4671. } else {
  4672. brname[0] = '\0';
  4673. br_ifindex = 0;
  4674. }
  4675. drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
  4676. drv->if_indices = drv->default_if_indices;
  4677. for (i = 0; i < params->num_bridge; i++) {
  4678. if (params->bridge[i]) {
  4679. ifindex = if_nametoindex(params->bridge[i]);
  4680. if (ifindex)
  4681. add_ifidx(drv, ifindex);
  4682. if (ifindex == br_ifindex)
  4683. br_added = 1;
  4684. }
  4685. }
  4686. if (!br_added && br_ifindex &&
  4687. (params->num_bridge == 0 || !params->bridge[0]))
  4688. add_ifidx(drv, br_ifindex);
  4689. /* start listening for EAPOL on the default AP interface */
  4690. add_ifidx(drv, drv->ifindex);
  4691. if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 0))
  4692. goto failed;
  4693. if (params->bssid) {
  4694. if (linux_set_ifhwaddr(drv->ioctl_sock, bss->ifname,
  4695. params->bssid))
  4696. goto failed;
  4697. }
  4698. if (wpa_driver_nl80211_set_mode(bss, IEEE80211_MODE_AP)) {
  4699. wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
  4700. "into AP mode", bss->ifname);
  4701. goto failed;
  4702. }
  4703. if (params->num_bridge && params->bridge[0] &&
  4704. i802_check_bridge(drv, params->bridge[0], params->ifname) < 0)
  4705. goto failed;
  4706. if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 1))
  4707. goto failed;
  4708. drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
  4709. if (drv->eapol_sock < 0) {
  4710. perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
  4711. goto failed;
  4712. }
  4713. if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
  4714. {
  4715. printf("Could not register read socket for eapol\n");
  4716. goto failed;
  4717. }
  4718. if (linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, params->own_addr))
  4719. goto failed;
  4720. return bss;
  4721. failed:
  4722. nl80211_remove_monitor_interface(drv);
  4723. rfkill_deinit(drv->rfkill);
  4724. netlink_deinit(drv->netlink);
  4725. if (drv->ioctl_sock >= 0)
  4726. close(drv->ioctl_sock);
  4727. genl_family_put(drv->nl80211);
  4728. nl_cache_free(drv->nl_cache);
  4729. nl80211_handle_destroy(drv->nl_handle);
  4730. nl_cb_put(drv->nl_cb);
  4731. eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle_event));
  4732. os_free(drv);
  4733. return NULL;
  4734. }
  4735. static void i802_deinit(void *priv)
  4736. {
  4737. wpa_driver_nl80211_deinit(priv);
  4738. }
  4739. #endif /* HOSTAPD */
  4740. static enum nl80211_iftype wpa_driver_nl80211_if_type(
  4741. enum wpa_driver_if_type type)
  4742. {
  4743. switch (type) {
  4744. case WPA_IF_STATION:
  4745. return NL80211_IFTYPE_STATION;
  4746. case WPA_IF_P2P_CLIENT:
  4747. case WPA_IF_P2P_GROUP:
  4748. return NL80211_IFTYPE_P2P_CLIENT;
  4749. case WPA_IF_AP_VLAN:
  4750. return NL80211_IFTYPE_AP_VLAN;
  4751. case WPA_IF_AP_BSS:
  4752. return NL80211_IFTYPE_AP;
  4753. case WPA_IF_P2P_GO:
  4754. return NL80211_IFTYPE_P2P_GO;
  4755. }
  4756. return -1;
  4757. }
  4758. #ifdef CONFIG_P2P
  4759. static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
  4760. {
  4761. struct wpa_driver_nl80211_data *drv;
  4762. dl_list_for_each(drv, &global->interfaces,
  4763. struct wpa_driver_nl80211_data, list) {
  4764. if (os_memcmp(addr, drv->addr, ETH_ALEN) == 0)
  4765. return 1;
  4766. }
  4767. return 0;
  4768. }
  4769. static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
  4770. u8 *new_addr)
  4771. {
  4772. unsigned int idx;
  4773. if (!drv->global)
  4774. return -1;
  4775. os_memcpy(new_addr, drv->addr, ETH_ALEN);
  4776. for (idx = 0; idx < 64; idx++) {
  4777. new_addr[0] = drv->addr[0] | 0x02;
  4778. new_addr[0] ^= idx << 2;
  4779. if (!nl80211_addr_in_use(drv->global, new_addr))
  4780. break;
  4781. }
  4782. if (idx == 64)
  4783. return -1;
  4784. wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
  4785. MACSTR, MAC2STR(new_addr));
  4786. return 0;
  4787. }
  4788. #endif /* CONFIG_P2P */
  4789. static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
  4790. const char *ifname, const u8 *addr,
  4791. void *bss_ctx, void **drv_priv,
  4792. char *force_ifname, u8 *if_addr)
  4793. {
  4794. struct i802_bss *bss = priv;
  4795. struct wpa_driver_nl80211_data *drv = bss->drv;
  4796. int ifidx;
  4797. #ifdef HOSTAPD
  4798. struct i802_bss *new_bss = NULL;
  4799. if (type == WPA_IF_AP_BSS) {
  4800. new_bss = os_zalloc(sizeof(*new_bss));
  4801. if (new_bss == NULL)
  4802. return -1;
  4803. }
  4804. #endif /* HOSTAPD */
  4805. if (addr)
  4806. os_memcpy(if_addr, addr, ETH_ALEN);
  4807. ifidx = nl80211_create_iface(drv, ifname,
  4808. wpa_driver_nl80211_if_type(type), addr,
  4809. 0);
  4810. if (ifidx < 0) {
  4811. #ifdef HOSTAPD
  4812. os_free(new_bss);
  4813. #endif /* HOSTAPD */
  4814. return -1;
  4815. }
  4816. if (!addr &&
  4817. linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, if_addr) < 0) {
  4818. nl80211_remove_iface(drv, ifidx);
  4819. return -1;
  4820. }
  4821. #ifdef CONFIG_P2P
  4822. if (!addr &&
  4823. (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
  4824. type == WPA_IF_P2P_GO)) {
  4825. /* Enforce unique P2P Interface Address */
  4826. u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN];
  4827. if (linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, own_addr)
  4828. < 0 ||
  4829. linux_get_ifhwaddr(drv->ioctl_sock, ifname, new_addr) < 0)
  4830. {
  4831. nl80211_remove_iface(drv, ifidx);
  4832. return -1;
  4833. }
  4834. if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) {
  4835. wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
  4836. "for P2P group interface");
  4837. if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
  4838. nl80211_remove_iface(drv, ifidx);
  4839. return -1;
  4840. }
  4841. if (linux_set_ifhwaddr(drv->ioctl_sock, ifname,
  4842. new_addr) < 0) {
  4843. nl80211_remove_iface(drv, ifidx);
  4844. return -1;
  4845. }
  4846. os_memcpy(if_addr, new_addr, ETH_ALEN);
  4847. }
  4848. }
  4849. #endif /* CONFIG_P2P */
  4850. #ifdef HOSTAPD
  4851. if (type == WPA_IF_AP_BSS) {
  4852. if (linux_set_iface_flags(drv->ioctl_sock, ifname, 1)) {
  4853. nl80211_remove_iface(drv, ifidx);
  4854. os_free(new_bss);
  4855. return -1;
  4856. }
  4857. os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
  4858. new_bss->ifindex = ifidx;
  4859. new_bss->drv = drv;
  4860. new_bss->next = drv->first_bss.next;
  4861. drv->first_bss.next = new_bss;
  4862. if (drv_priv)
  4863. *drv_priv = new_bss;
  4864. }
  4865. #endif /* HOSTAPD */
  4866. return 0;
  4867. }
  4868. static int wpa_driver_nl80211_if_remove(void *priv,
  4869. enum wpa_driver_if_type type,
  4870. const char *ifname)
  4871. {
  4872. struct i802_bss *bss = priv;
  4873. struct wpa_driver_nl80211_data *drv = bss->drv;
  4874. int ifindex = if_nametoindex(ifname);
  4875. wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
  4876. __func__, type, ifname, ifindex);
  4877. if (ifindex <= 0)
  4878. return -1;
  4879. nl80211_remove_iface(drv, ifindex);
  4880. #ifdef HOSTAPD
  4881. if (type != WPA_IF_AP_BSS)
  4882. return 0;
  4883. if (bss != &drv->first_bss) {
  4884. struct i802_bss *tbss;
  4885. for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
  4886. if (tbss->next == bss) {
  4887. tbss->next = bss->next;
  4888. os_free(bss);
  4889. bss = NULL;
  4890. break;
  4891. }
  4892. }
  4893. if (bss)
  4894. wpa_printf(MSG_INFO, "nl80211: %s - could not find "
  4895. "BSS %p in the list", __func__, bss);
  4896. }
  4897. #endif /* HOSTAPD */
  4898. return 0;
  4899. }
  4900. static int cookie_handler(struct nl_msg *msg, void *arg)
  4901. {
  4902. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  4903. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  4904. u64 *cookie = arg;
  4905. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  4906. genlmsg_attrlen(gnlh, 0), NULL);
  4907. if (tb[NL80211_ATTR_COOKIE])
  4908. *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
  4909. return NL_SKIP;
  4910. }
  4911. static int nl80211_send_frame_cmd(struct wpa_driver_nl80211_data *drv,
  4912. unsigned int freq, unsigned int wait,
  4913. const u8 *buf, size_t buf_len,
  4914. u64 *cookie_out)
  4915. {
  4916. struct nl_msg *msg;
  4917. u64 cookie;
  4918. int ret = -1;
  4919. msg = nlmsg_alloc();
  4920. if (!msg)
  4921. return -1;
  4922. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  4923. NL80211_CMD_FRAME, 0);
  4924. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  4925. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
  4926. NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
  4927. NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
  4928. NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
  4929. cookie = 0;
  4930. ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
  4931. msg = NULL;
  4932. if (ret) {
  4933. wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
  4934. "(%s)", ret, strerror(-ret));
  4935. goto nla_put_failure;
  4936. }
  4937. wpa_printf(MSG_DEBUG, "nl80211: Frame TX command accepted; "
  4938. "cookie 0x%llx", (long long unsigned int) cookie);
  4939. if (cookie_out)
  4940. *cookie_out = cookie;
  4941. nla_put_failure:
  4942. nlmsg_free(msg);
  4943. return ret;
  4944. }
  4945. static int wpa_driver_nl80211_send_action(void *priv, unsigned int freq,
  4946. unsigned int wait_time,
  4947. const u8 *dst, const u8 *src,
  4948. const u8 *bssid,
  4949. const u8 *data, size_t data_len)
  4950. {
  4951. struct i802_bss *bss = priv;
  4952. struct wpa_driver_nl80211_data *drv = bss->drv;
  4953. int ret = -1;
  4954. u8 *buf;
  4955. struct ieee80211_hdr *hdr;
  4956. wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
  4957. "wait=%d ms)", drv->ifindex, wait_time);
  4958. buf = os_zalloc(24 + data_len);
  4959. if (buf == NULL)
  4960. return ret;
  4961. os_memcpy(buf + 24, data, data_len);
  4962. hdr = (struct ieee80211_hdr *) buf;
  4963. hdr->frame_control =
  4964. IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
  4965. os_memcpy(hdr->addr1, dst, ETH_ALEN);
  4966. os_memcpy(hdr->addr2, src, ETH_ALEN);
  4967. os_memcpy(hdr->addr3, bssid, ETH_ALEN);
  4968. if (drv->nlmode == NL80211_IFTYPE_AP)
  4969. ret = wpa_driver_nl80211_send_mlme(priv, buf, 24 + data_len);
  4970. else
  4971. ret = nl80211_send_frame_cmd(drv, freq, wait_time, buf,
  4972. 24 + data_len,
  4973. &drv->send_action_cookie);
  4974. os_free(buf);
  4975. return ret;
  4976. }
  4977. static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
  4978. {
  4979. struct i802_bss *bss = priv;
  4980. struct wpa_driver_nl80211_data *drv = bss->drv;
  4981. struct nl_msg *msg;
  4982. int ret;
  4983. msg = nlmsg_alloc();
  4984. if (!msg)
  4985. return;
  4986. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  4987. NL80211_CMD_FRAME_WAIT_CANCEL, 0);
  4988. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  4989. NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
  4990. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4991. msg = NULL;
  4992. if (ret)
  4993. wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
  4994. "(%s)", ret, strerror(-ret));
  4995. nla_put_failure:
  4996. nlmsg_free(msg);
  4997. }
  4998. static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
  4999. unsigned int duration)
  5000. {
  5001. struct i802_bss *bss = priv;
  5002. struct wpa_driver_nl80211_data *drv = bss->drv;
  5003. struct nl_msg *msg;
  5004. int ret;
  5005. u64 cookie;
  5006. msg = nlmsg_alloc();
  5007. if (!msg)
  5008. return -1;
  5009. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  5010. NL80211_CMD_REMAIN_ON_CHANNEL, 0);
  5011. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  5012. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
  5013. NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
  5014. cookie = 0;
  5015. ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
  5016. if (ret == 0) {
  5017. wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
  5018. "0x%llx for freq=%u MHz duration=%u",
  5019. (long long unsigned int) cookie, freq, duration);
  5020. drv->remain_on_chan_cookie = cookie;
  5021. return 0;
  5022. }
  5023. wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
  5024. "(freq=%d duration=%u): %d (%s)",
  5025. freq, duration, ret, strerror(-ret));
  5026. nla_put_failure:
  5027. return -1;
  5028. }
  5029. static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
  5030. {
  5031. struct i802_bss *bss = priv;
  5032. struct wpa_driver_nl80211_data *drv = bss->drv;
  5033. struct nl_msg *msg;
  5034. int ret;
  5035. if (!drv->pending_remain_on_chan) {
  5036. wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
  5037. "to cancel");
  5038. return -1;
  5039. }
  5040. wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
  5041. "0x%llx",
  5042. (long long unsigned int) drv->remain_on_chan_cookie);
  5043. msg = nlmsg_alloc();
  5044. if (!msg)
  5045. return -1;
  5046. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  5047. NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL, 0);
  5048. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  5049. NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
  5050. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5051. if (ret == 0)
  5052. return 0;
  5053. wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
  5054. "%d (%s)", ret, strerror(-ret));
  5055. nla_put_failure:
  5056. return -1;
  5057. }
  5058. static int wpa_driver_nl80211_probe_req_report(void *priv, int report)
  5059. {
  5060. struct i802_bss *bss = priv;
  5061. struct wpa_driver_nl80211_data *drv = bss->drv;
  5062. if (drv->nlmode != NL80211_IFTYPE_STATION) {
  5063. wpa_printf(MSG_DEBUG, "nl80211: probe_req_report control only "
  5064. "allowed in station mode (iftype=%d)",
  5065. drv->nlmode);
  5066. return -1;
  5067. }
  5068. if (!report) {
  5069. if (drv->nl_handle_preq) {
  5070. eloop_unregister_read_sock(
  5071. nl_socket_get_fd(drv->nl_handle_preq));
  5072. nl_cache_free(drv->nl_cache_preq);
  5073. nl80211_handle_destroy(drv->nl_handle_preq);
  5074. drv->nl_handle_preq = NULL;
  5075. }
  5076. return 0;
  5077. }
  5078. if (drv->nl_handle_preq) {
  5079. wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
  5080. "already on!");
  5081. return 0;
  5082. }
  5083. drv->nl_handle_preq = nl80211_handle_alloc(drv->nl_cb);
  5084. if (drv->nl_handle_preq == NULL) {
  5085. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate "
  5086. "netlink callbacks (preq)");
  5087. goto out_err1;
  5088. }
  5089. if (genl_connect(drv->nl_handle_preq)) {
  5090. wpa_printf(MSG_ERROR, "nl80211: Failed to connect to "
  5091. "generic netlink (preq)");
  5092. goto out_err2;
  5093. return -1;
  5094. }
  5095. #ifdef CONFIG_LIBNL20
  5096. if (genl_ctrl_alloc_cache(drv->nl_handle_preq,
  5097. &drv->nl_cache_preq) < 0) {
  5098. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  5099. "netlink cache (preq)");
  5100. goto out_err2;
  5101. }
  5102. #else /* CONFIG_LIBNL20 */
  5103. drv->nl_cache_preq = genl_ctrl_alloc_cache(drv->nl_handle_preq);
  5104. if (drv->nl_cache_preq == NULL) {
  5105. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  5106. "netlink cache (preq)");
  5107. goto out_err2;
  5108. }
  5109. #endif /* CONFIG_LIBNL20 */
  5110. if (nl80211_register_frame(drv, drv->nl_handle_preq,
  5111. (WLAN_FC_TYPE_MGMT << 2) |
  5112. (WLAN_FC_STYPE_PROBE_REQ << 4),
  5113. NULL, 0) < 0) {
  5114. goto out_err3;
  5115. }
  5116. eloop_register_read_sock(nl_socket_get_fd(drv->nl_handle_preq),
  5117. wpa_driver_nl80211_event_receive, drv,
  5118. drv->nl_handle_preq);
  5119. return 0;
  5120. out_err3:
  5121. nl_cache_free(drv->nl_cache_preq);
  5122. out_err2:
  5123. nl80211_handle_destroy(drv->nl_handle_preq);
  5124. drv->nl_handle_preq = NULL;
  5125. out_err1:
  5126. return -1;
  5127. }
  5128. static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
  5129. int ifindex, int disabled)
  5130. {
  5131. struct nl_msg *msg;
  5132. struct nlattr *bands, *band;
  5133. int ret;
  5134. msg = nlmsg_alloc();
  5135. if (!msg)
  5136. return -1;
  5137. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  5138. NL80211_CMD_SET_TX_BITRATE_MASK, 0);
  5139. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  5140. bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
  5141. if (!bands)
  5142. goto nla_put_failure;
  5143. /*
  5144. * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
  5145. * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
  5146. * rates. All 5 GHz rates are left enabled.
  5147. */
  5148. band = nla_nest_start(msg, NL80211_BAND_2GHZ);
  5149. if (!band)
  5150. goto nla_put_failure;
  5151. NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
  5152. "\x0c\x12\x18\x24\x30\x48\x60\x6c");
  5153. nla_nest_end(msg, band);
  5154. nla_nest_end(msg, bands);
  5155. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5156. msg = NULL;
  5157. if (ret) {
  5158. wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
  5159. "(%s)", ret, strerror(-ret));
  5160. }
  5161. return ret;
  5162. nla_put_failure:
  5163. nlmsg_free(msg);
  5164. return -1;
  5165. }
  5166. static int wpa_driver_nl80211_disable_11b_rates(void *priv, int disabled)
  5167. {
  5168. struct i802_bss *bss = priv;
  5169. struct wpa_driver_nl80211_data *drv = bss->drv;
  5170. drv->disable_11b_rates = disabled;
  5171. return nl80211_disable_11b_rates(drv, drv->ifindex, disabled);
  5172. }
  5173. static int wpa_driver_nl80211_deinit_ap(void *priv)
  5174. {
  5175. struct i802_bss *bss = priv;
  5176. struct wpa_driver_nl80211_data *drv = bss->drv;
  5177. if (drv->nlmode != NL80211_IFTYPE_AP)
  5178. return -1;
  5179. wpa_driver_nl80211_del_beacon(drv);
  5180. return wpa_driver_nl80211_set_mode(priv, IEEE80211_MODE_INFRA);
  5181. }
  5182. static void wpa_driver_nl80211_resume(void *priv)
  5183. {
  5184. struct i802_bss *bss = priv;
  5185. struct wpa_driver_nl80211_data *drv = bss->drv;
  5186. if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 1)) {
  5187. wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on "
  5188. "resume event");
  5189. }
  5190. }
  5191. static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
  5192. const u8 *ies, size_t ies_len)
  5193. {
  5194. struct i802_bss *bss = priv;
  5195. struct wpa_driver_nl80211_data *drv = bss->drv;
  5196. int ret;
  5197. u8 *data, *pos;
  5198. size_t data_len;
  5199. u8 own_addr[ETH_ALEN];
  5200. if (linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, own_addr) < 0)
  5201. return -1;
  5202. if (action != 1) {
  5203. wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
  5204. "action %d", action);
  5205. return -1;
  5206. }
  5207. /*
  5208. * Action frame payload:
  5209. * Category[1] = 6 (Fast BSS Transition)
  5210. * Action[1] = 1 (Fast BSS Transition Request)
  5211. * STA Address
  5212. * Target AP Address
  5213. * FT IEs
  5214. */
  5215. data_len = 2 + 2 * ETH_ALEN + ies_len;
  5216. data = os_malloc(data_len);
  5217. if (data == NULL)
  5218. return -1;
  5219. pos = data;
  5220. *pos++ = 0x06; /* FT Action category */
  5221. *pos++ = action;
  5222. os_memcpy(pos, own_addr, ETH_ALEN);
  5223. pos += ETH_ALEN;
  5224. os_memcpy(pos, target_ap, ETH_ALEN);
  5225. pos += ETH_ALEN;
  5226. os_memcpy(pos, ies, ies_len);
  5227. ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
  5228. drv->bssid, own_addr, drv->bssid,
  5229. data, data_len);
  5230. os_free(data);
  5231. return ret;
  5232. }
  5233. static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
  5234. {
  5235. struct i802_bss *bss = priv;
  5236. struct wpa_driver_nl80211_data *drv = bss->drv;
  5237. struct nl_msg *msg, *cqm = NULL;
  5238. wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
  5239. "hysteresis=%d", threshold, hysteresis);
  5240. msg = nlmsg_alloc();
  5241. if (!msg)
  5242. return -1;
  5243. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  5244. 0, NL80211_CMD_SET_CQM, 0);
  5245. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  5246. cqm = nlmsg_alloc();
  5247. if (cqm == NULL)
  5248. return -1;
  5249. NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
  5250. NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
  5251. nla_put_nested(msg, NL80211_ATTR_CQM, cqm);
  5252. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  5253. return 0;
  5254. msg = NULL;
  5255. nla_put_failure:
  5256. if (cqm)
  5257. nlmsg_free(cqm);
  5258. nlmsg_free(msg);
  5259. return -1;
  5260. }
  5261. static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
  5262. int encrypt)
  5263. {
  5264. struct i802_bss *bss = priv;
  5265. struct wpa_driver_nl80211_data *drv = bss->drv;
  5266. return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt);
  5267. }
  5268. static int nl80211_set_intra_bss(void *priv, int enabled)
  5269. {
  5270. struct i802_bss *bss = priv;
  5271. struct wpa_driver_nl80211_data *drv = bss->drv;
  5272. struct nl_msg *msg;
  5273. msg = nlmsg_alloc();
  5274. if (!msg)
  5275. return -ENOMEM;
  5276. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  5277. NL80211_CMD_SET_BSS, 0);
  5278. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  5279. NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, !enabled);
  5280. return send_and_recv_msgs(drv, msg, NULL, NULL);
  5281. nla_put_failure:
  5282. return -ENOBUFS;
  5283. }
  5284. static int nl80211_set_param(void *priv, const char *param)
  5285. {
  5286. wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
  5287. if (param == NULL)
  5288. return 0;
  5289. #ifdef CONFIG_P2P
  5290. if (os_strstr(param, "use_p2p_group_interface=1")) {
  5291. struct i802_bss *bss = priv;
  5292. struct wpa_driver_nl80211_data *drv = bss->drv;
  5293. wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
  5294. "interface");
  5295. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
  5296. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
  5297. }
  5298. #endif /* CONFIG_P2P */
  5299. return 0;
  5300. }
  5301. static void * nl80211_global_init(void)
  5302. {
  5303. struct nl80211_global *global;
  5304. global = os_zalloc(sizeof(*global));
  5305. if (global == NULL)
  5306. return NULL;
  5307. dl_list_init(&global->interfaces);
  5308. return global;
  5309. }
  5310. static void nl80211_global_deinit(void *priv)
  5311. {
  5312. struct nl80211_global *global = priv;
  5313. if (global == NULL)
  5314. return;
  5315. if (!dl_list_empty(&global->interfaces)) {
  5316. wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
  5317. "nl80211_global_deinit",
  5318. dl_list_len(&global->interfaces));
  5319. }
  5320. os_free(global);
  5321. }
  5322. static const char * nl80211_get_radio_name(void *priv)
  5323. {
  5324. struct i802_bss *bss = priv;
  5325. struct wpa_driver_nl80211_data *drv = bss->drv;
  5326. return drv->phyname;
  5327. }
  5328. const struct wpa_driver_ops wpa_driver_nl80211_ops = {
  5329. .name = "nl80211",
  5330. .desc = "Linux nl80211/cfg80211",
  5331. .get_bssid = wpa_driver_nl80211_get_bssid,
  5332. .get_ssid = wpa_driver_nl80211_get_ssid,
  5333. .set_key = wpa_driver_nl80211_set_key,
  5334. .scan2 = wpa_driver_nl80211_scan,
  5335. .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
  5336. .deauthenticate = wpa_driver_nl80211_deauthenticate,
  5337. .disassociate = wpa_driver_nl80211_disassociate,
  5338. .authenticate = wpa_driver_nl80211_authenticate,
  5339. .associate = wpa_driver_nl80211_associate,
  5340. .global_init = nl80211_global_init,
  5341. .global_deinit = nl80211_global_deinit,
  5342. .init2 = wpa_driver_nl80211_init,
  5343. .deinit = wpa_driver_nl80211_deinit,
  5344. .get_capa = wpa_driver_nl80211_get_capa,
  5345. .set_operstate = wpa_driver_nl80211_set_operstate,
  5346. .set_supp_port = wpa_driver_nl80211_set_supp_port,
  5347. .set_country = wpa_driver_nl80211_set_country,
  5348. .set_beacon = wpa_driver_nl80211_set_beacon,
  5349. .if_add = wpa_driver_nl80211_if_add,
  5350. .if_remove = wpa_driver_nl80211_if_remove,
  5351. .send_mlme = wpa_driver_nl80211_send_mlme,
  5352. .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
  5353. .sta_add = wpa_driver_nl80211_sta_add,
  5354. .sta_remove = wpa_driver_nl80211_sta_remove,
  5355. .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
  5356. .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
  5357. #ifdef HOSTAPD
  5358. .hapd_init = i802_init,
  5359. .hapd_deinit = i802_deinit,
  5360. .get_seqnum = i802_get_seqnum,
  5361. .flush = i802_flush,
  5362. .read_sta_data = i802_read_sta_data,
  5363. .sta_deauth = i802_sta_deauth,
  5364. .sta_disassoc = i802_sta_disassoc,
  5365. .get_inact_sec = i802_get_inact_sec,
  5366. .sta_clear_stats = i802_sta_clear_stats,
  5367. .set_rts = i802_set_rts,
  5368. .set_frag = i802_set_frag,
  5369. .set_rate_sets = i802_set_rate_sets,
  5370. .set_cts_protect = i802_set_cts_protect,
  5371. .set_preamble = i802_set_preamble,
  5372. .set_short_slot_time = i802_set_short_slot_time,
  5373. .set_tx_queue_params = i802_set_tx_queue_params,
  5374. .set_sta_vlan = i802_set_sta_vlan,
  5375. .set_wds_sta = i802_set_wds_sta,
  5376. .set_ht_params = i802_set_ht_params,
  5377. #endif /* HOSTAPD */
  5378. .set_freq = i802_set_freq,
  5379. .send_action = wpa_driver_nl80211_send_action,
  5380. .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
  5381. .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
  5382. .cancel_remain_on_channel =
  5383. wpa_driver_nl80211_cancel_remain_on_channel,
  5384. .probe_req_report = wpa_driver_nl80211_probe_req_report,
  5385. .disable_11b_rates = wpa_driver_nl80211_disable_11b_rates,
  5386. .deinit_ap = wpa_driver_nl80211_deinit_ap,
  5387. .resume = wpa_driver_nl80211_resume,
  5388. .send_ft_action = nl80211_send_ft_action,
  5389. .signal_monitor = nl80211_signal_monitor,
  5390. .send_frame = nl80211_send_frame,
  5391. .set_intra_bss = nl80211_set_intra_bss,
  5392. .set_param = nl80211_set_param,
  5393. .get_radio_name = nl80211_get_radio_name,
  5394. };