wpa_supplicant.c 171 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254
  1. /*
  2. * WPA Supplicant
  3. * Copyright (c) 2003-2016, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. *
  8. * This file implements functions for registering and unregistering
  9. * %wpa_supplicant interfaces. In addition, this file contains number of
  10. * functions for managing network connections.
  11. */
  12. #include "includes.h"
  13. #include "common.h"
  14. #include "crypto/random.h"
  15. #include "crypto/sha1.h"
  16. #include "eapol_supp/eapol_supp_sm.h"
  17. #include "eap_peer/eap.h"
  18. #include "eap_peer/eap_proxy.h"
  19. #include "eap_server/eap_methods.h"
  20. #include "rsn_supp/wpa.h"
  21. #include "eloop.h"
  22. #include "config.h"
  23. #include "utils/ext_password.h"
  24. #include "l2_packet/l2_packet.h"
  25. #include "wpa_supplicant_i.h"
  26. #include "driver_i.h"
  27. #include "ctrl_iface.h"
  28. #include "pcsc_funcs.h"
  29. #include "common/version.h"
  30. #include "rsn_supp/preauth.h"
  31. #include "rsn_supp/pmksa_cache.h"
  32. #include "common/wpa_ctrl.h"
  33. #include "common/ieee802_11_defs.h"
  34. #include "common/hw_features_common.h"
  35. #include "p2p/p2p.h"
  36. #include "fst/fst.h"
  37. #include "blacklist.h"
  38. #include "wpas_glue.h"
  39. #include "wps_supplicant.h"
  40. #include "ibss_rsn.h"
  41. #include "sme.h"
  42. #include "gas_query.h"
  43. #include "ap.h"
  44. #include "p2p_supplicant.h"
  45. #include "wifi_display.h"
  46. #include "notify.h"
  47. #include "bgscan.h"
  48. #include "autoscan.h"
  49. #include "bss.h"
  50. #include "scan.h"
  51. #include "offchannel.h"
  52. #include "hs20_supplicant.h"
  53. #include "wnm_sta.h"
  54. #include "wpas_kay.h"
  55. #include "mesh.h"
  56. const char *const wpa_supplicant_version =
  57. "wpa_supplicant v" VERSION_STR "\n"
  58. "Copyright (c) 2003-2016, Jouni Malinen <j@w1.fi> and contributors";
  59. const char *const wpa_supplicant_license =
  60. "This software may be distributed under the terms of the BSD license.\n"
  61. "See README for more details.\n"
  62. #ifdef EAP_TLS_OPENSSL
  63. "\nThis product includes software developed by the OpenSSL Project\n"
  64. "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
  65. #endif /* EAP_TLS_OPENSSL */
  66. ;
  67. #ifndef CONFIG_NO_STDOUT_DEBUG
  68. /* Long text divided into parts in order to fit in C89 strings size limits. */
  69. const char *const wpa_supplicant_full_license1 =
  70. "";
  71. const char *const wpa_supplicant_full_license2 =
  72. "This software may be distributed under the terms of the BSD license.\n"
  73. "\n"
  74. "Redistribution and use in source and binary forms, with or without\n"
  75. "modification, are permitted provided that the following conditions are\n"
  76. "met:\n"
  77. "\n";
  78. const char *const wpa_supplicant_full_license3 =
  79. "1. Redistributions of source code must retain the above copyright\n"
  80. " notice, this list of conditions and the following disclaimer.\n"
  81. "\n"
  82. "2. Redistributions in binary form must reproduce the above copyright\n"
  83. " notice, this list of conditions and the following disclaimer in the\n"
  84. " documentation and/or other materials provided with the distribution.\n"
  85. "\n";
  86. const char *const wpa_supplicant_full_license4 =
  87. "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
  88. " names of its contributors may be used to endorse or promote products\n"
  89. " derived from this software without specific prior written permission.\n"
  90. "\n"
  91. "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
  92. "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
  93. "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
  94. "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
  95. const char *const wpa_supplicant_full_license5 =
  96. "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
  97. "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
  98. "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
  99. "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
  100. "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
  101. "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
  102. "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  103. "\n";
  104. #endif /* CONFIG_NO_STDOUT_DEBUG */
  105. /* Configure default/group WEP keys for static WEP */
  106. int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  107. {
  108. int i, set = 0;
  109. for (i = 0; i < NUM_WEP_KEYS; i++) {
  110. if (ssid->wep_key_len[i] == 0)
  111. continue;
  112. set = 1;
  113. wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
  114. i, i == ssid->wep_tx_keyidx, NULL, 0,
  115. ssid->wep_key[i], ssid->wep_key_len[i]);
  116. }
  117. return set;
  118. }
  119. int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
  120. struct wpa_ssid *ssid)
  121. {
  122. u8 key[32];
  123. size_t keylen;
  124. enum wpa_alg alg;
  125. u8 seq[6] = { 0 };
  126. int ret;
  127. /* IBSS/WPA-None uses only one key (Group) for both receiving and
  128. * sending unicast and multicast packets. */
  129. if (ssid->mode != WPAS_MODE_IBSS) {
  130. wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
  131. "IBSS/ad-hoc) for WPA-None", ssid->mode);
  132. return -1;
  133. }
  134. if (!ssid->psk_set) {
  135. wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
  136. "WPA-None");
  137. return -1;
  138. }
  139. switch (wpa_s->group_cipher) {
  140. case WPA_CIPHER_CCMP:
  141. os_memcpy(key, ssid->psk, 16);
  142. keylen = 16;
  143. alg = WPA_ALG_CCMP;
  144. break;
  145. case WPA_CIPHER_GCMP:
  146. os_memcpy(key, ssid->psk, 16);
  147. keylen = 16;
  148. alg = WPA_ALG_GCMP;
  149. break;
  150. case WPA_CIPHER_TKIP:
  151. /* WPA-None uses the same Michael MIC key for both TX and RX */
  152. os_memcpy(key, ssid->psk, 16 + 8);
  153. os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
  154. keylen = 32;
  155. alg = WPA_ALG_TKIP;
  156. break;
  157. default:
  158. wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
  159. "WPA-None", wpa_s->group_cipher);
  160. return -1;
  161. }
  162. /* TODO: should actually remember the previously used seq#, both for TX
  163. * and RX from each STA.. */
  164. ret = wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
  165. os_memset(key, 0, sizeof(key));
  166. return ret;
  167. }
  168. static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
  169. {
  170. struct wpa_supplicant *wpa_s = eloop_ctx;
  171. const u8 *bssid = wpa_s->bssid;
  172. if (is_zero_ether_addr(bssid))
  173. bssid = wpa_s->pending_bssid;
  174. wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
  175. MAC2STR(bssid));
  176. wpa_blacklist_add(wpa_s, bssid);
  177. wpa_sm_notify_disassoc(wpa_s->wpa);
  178. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  179. wpa_s->reassociate = 1;
  180. /*
  181. * If we timed out, the AP or the local radio may be busy.
  182. * So, wait a second until scanning again.
  183. */
  184. wpa_supplicant_req_scan(wpa_s, 1, 0);
  185. }
  186. /**
  187. * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
  188. * @wpa_s: Pointer to wpa_supplicant data
  189. * @sec: Number of seconds after which to time out authentication
  190. * @usec: Number of microseconds after which to time out authentication
  191. *
  192. * This function is used to schedule a timeout for the current authentication
  193. * attempt.
  194. */
  195. void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
  196. int sec, int usec)
  197. {
  198. if (wpa_s->conf->ap_scan == 0 &&
  199. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
  200. return;
  201. wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
  202. "%d usec", sec, usec);
  203. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  204. eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
  205. }
  206. /**
  207. * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
  208. * @wpa_s: Pointer to wpa_supplicant data
  209. *
  210. * This function is used to cancel authentication timeout scheduled with
  211. * wpa_supplicant_req_auth_timeout() and it is called when authentication has
  212. * been completed.
  213. */
  214. void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
  215. {
  216. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
  217. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  218. wpa_blacklist_del(wpa_s, wpa_s->bssid);
  219. }
  220. /**
  221. * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
  222. * @wpa_s: Pointer to wpa_supplicant data
  223. *
  224. * This function is used to configure EAPOL state machine based on the selected
  225. * authentication mode.
  226. */
  227. void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
  228. {
  229. #ifdef IEEE8021X_EAPOL
  230. struct eapol_config eapol_conf;
  231. struct wpa_ssid *ssid = wpa_s->current_ssid;
  232. #ifdef CONFIG_IBSS_RSN
  233. if (ssid->mode == WPAS_MODE_IBSS &&
  234. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  235. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  236. /*
  237. * RSN IBSS authentication is per-STA and we can disable the
  238. * per-BSSID EAPOL authentication.
  239. */
  240. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  241. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  242. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  243. return;
  244. }
  245. #endif /* CONFIG_IBSS_RSN */
  246. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  247. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  248. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  249. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  250. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  251. else
  252. eapol_sm_notify_portControl(wpa_s->eapol, Auto);
  253. os_memset(&eapol_conf, 0, sizeof(eapol_conf));
  254. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  255. eapol_conf.accept_802_1x_keys = 1;
  256. eapol_conf.required_keys = 0;
  257. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
  258. eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
  259. }
  260. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
  261. eapol_conf.required_keys |=
  262. EAPOL_REQUIRE_KEY_BROADCAST;
  263. }
  264. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)
  265. eapol_conf.required_keys = 0;
  266. }
  267. eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
  268. eapol_conf.workaround = ssid->eap_workaround;
  269. eapol_conf.eap_disabled =
  270. !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
  271. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  272. wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
  273. eapol_conf.external_sim = wpa_s->conf->external_sim;
  274. #ifdef CONFIG_WPS
  275. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
  276. eapol_conf.wps |= EAPOL_LOCAL_WPS_IN_USE;
  277. if (wpa_s->current_bss) {
  278. struct wpabuf *ie;
  279. ie = wpa_bss_get_vendor_ie_multi(wpa_s->current_bss,
  280. WPS_IE_VENDOR_TYPE);
  281. if (ie) {
  282. if (wps_is_20(ie))
  283. eapol_conf.wps |=
  284. EAPOL_PEER_IS_WPS20_AP;
  285. wpabuf_free(ie);
  286. }
  287. }
  288. }
  289. #endif /* CONFIG_WPS */
  290. eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
  291. ieee802_1x_alloc_kay_sm(wpa_s, ssid);
  292. #endif /* IEEE8021X_EAPOL */
  293. }
  294. /**
  295. * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
  296. * @wpa_s: Pointer to wpa_supplicant data
  297. * @ssid: Configuration data for the network
  298. *
  299. * This function is used to configure WPA state machine and related parameters
  300. * to a mode where WPA is not enabled. This is called as part of the
  301. * authentication configuration when the selected network does not use WPA.
  302. */
  303. void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
  304. struct wpa_ssid *ssid)
  305. {
  306. int i;
  307. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  308. wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
  309. else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
  310. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
  311. else
  312. wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
  313. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  314. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  315. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  316. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  317. wpa_s->group_cipher = WPA_CIPHER_NONE;
  318. wpa_s->mgmt_group_cipher = 0;
  319. for (i = 0; i < NUM_WEP_KEYS; i++) {
  320. if (ssid->wep_key_len[i] > 5) {
  321. wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
  322. wpa_s->group_cipher = WPA_CIPHER_WEP104;
  323. break;
  324. } else if (ssid->wep_key_len[i] > 0) {
  325. wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
  326. wpa_s->group_cipher = WPA_CIPHER_WEP40;
  327. break;
  328. }
  329. }
  330. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
  331. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  332. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  333. wpa_s->pairwise_cipher);
  334. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  335. #ifdef CONFIG_IEEE80211W
  336. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  337. wpa_s->mgmt_group_cipher);
  338. #endif /* CONFIG_IEEE80211W */
  339. pmksa_cache_clear_current(wpa_s->wpa);
  340. }
  341. void free_hw_features(struct wpa_supplicant *wpa_s)
  342. {
  343. int i;
  344. if (wpa_s->hw.modes == NULL)
  345. return;
  346. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  347. os_free(wpa_s->hw.modes[i].channels);
  348. os_free(wpa_s->hw.modes[i].rates);
  349. }
  350. os_free(wpa_s->hw.modes);
  351. wpa_s->hw.modes = NULL;
  352. }
  353. static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
  354. {
  355. int i;
  356. bgscan_deinit(wpa_s);
  357. autoscan_deinit(wpa_s);
  358. scard_deinit(wpa_s->scard);
  359. wpa_s->scard = NULL;
  360. wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
  361. eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
  362. l2_packet_deinit(wpa_s->l2);
  363. wpa_s->l2 = NULL;
  364. if (wpa_s->l2_br) {
  365. l2_packet_deinit(wpa_s->l2_br);
  366. wpa_s->l2_br = NULL;
  367. }
  368. #ifdef CONFIG_TESTING_OPTIONS
  369. l2_packet_deinit(wpa_s->l2_test);
  370. wpa_s->l2_test = NULL;
  371. #endif /* CONFIG_TESTING_OPTIONS */
  372. if (wpa_s->conf != NULL) {
  373. struct wpa_ssid *ssid;
  374. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  375. wpas_notify_network_removed(wpa_s, ssid);
  376. }
  377. os_free(wpa_s->confname);
  378. wpa_s->confname = NULL;
  379. os_free(wpa_s->confanother);
  380. wpa_s->confanother = NULL;
  381. wpa_sm_set_eapol(wpa_s->wpa, NULL);
  382. eapol_sm_deinit(wpa_s->eapol);
  383. wpa_s->eapol = NULL;
  384. rsn_preauth_deinit(wpa_s->wpa);
  385. #ifdef CONFIG_TDLS
  386. wpa_tdls_deinit(wpa_s->wpa);
  387. #endif /* CONFIG_TDLS */
  388. wmm_ac_clear_saved_tspecs(wpa_s);
  389. pmksa_candidate_free(wpa_s->wpa);
  390. wpa_sm_deinit(wpa_s->wpa);
  391. wpa_s->wpa = NULL;
  392. wpa_blacklist_clear(wpa_s);
  393. wpa_bss_deinit(wpa_s);
  394. wpa_supplicant_cancel_delayed_sched_scan(wpa_s);
  395. wpa_supplicant_cancel_scan(wpa_s);
  396. wpa_supplicant_cancel_auth_timeout(wpa_s);
  397. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  398. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  399. eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
  400. wpa_s, NULL);
  401. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  402. eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
  403. wpas_wps_deinit(wpa_s);
  404. wpabuf_free(wpa_s->pending_eapol_rx);
  405. wpa_s->pending_eapol_rx = NULL;
  406. #ifdef CONFIG_IBSS_RSN
  407. ibss_rsn_deinit(wpa_s->ibss_rsn);
  408. wpa_s->ibss_rsn = NULL;
  409. #endif /* CONFIG_IBSS_RSN */
  410. sme_deinit(wpa_s);
  411. #ifdef CONFIG_AP
  412. wpa_supplicant_ap_deinit(wpa_s);
  413. #endif /* CONFIG_AP */
  414. wpas_p2p_deinit(wpa_s);
  415. #ifdef CONFIG_OFFCHANNEL
  416. offchannel_deinit(wpa_s);
  417. #endif /* CONFIG_OFFCHANNEL */
  418. wpa_supplicant_cancel_sched_scan(wpa_s);
  419. os_free(wpa_s->next_scan_freqs);
  420. wpa_s->next_scan_freqs = NULL;
  421. os_free(wpa_s->manual_scan_freqs);
  422. wpa_s->manual_scan_freqs = NULL;
  423. os_free(wpa_s->manual_sched_scan_freqs);
  424. wpa_s->manual_sched_scan_freqs = NULL;
  425. wpas_mac_addr_rand_scan_clear(wpa_s, MAC_ADDR_RAND_ALL);
  426. /*
  427. * Need to remove any pending gas-query radio work before the
  428. * gas_query_deinit() call because gas_query::work has not yet been set
  429. * for works that have not been started. gas_query_free() will be unable
  430. * to cancel such pending radio works and once the pending gas-query
  431. * radio work eventually gets removed, the deinit notification call to
  432. * gas_query_start_cb() would result in dereferencing freed memory.
  433. */
  434. if (wpa_s->radio)
  435. radio_remove_works(wpa_s, "gas-query", 0);
  436. gas_query_deinit(wpa_s->gas);
  437. wpa_s->gas = NULL;
  438. free_hw_features(wpa_s);
  439. ieee802_1x_dealloc_kay_sm(wpa_s);
  440. os_free(wpa_s->bssid_filter);
  441. wpa_s->bssid_filter = NULL;
  442. os_free(wpa_s->disallow_aps_bssid);
  443. wpa_s->disallow_aps_bssid = NULL;
  444. os_free(wpa_s->disallow_aps_ssid);
  445. wpa_s->disallow_aps_ssid = NULL;
  446. wnm_bss_keep_alive_deinit(wpa_s);
  447. #ifdef CONFIG_WNM
  448. wnm_deallocate_memory(wpa_s);
  449. #endif /* CONFIG_WNM */
  450. ext_password_deinit(wpa_s->ext_pw);
  451. wpa_s->ext_pw = NULL;
  452. wpabuf_free(wpa_s->last_gas_resp);
  453. wpa_s->last_gas_resp = NULL;
  454. wpabuf_free(wpa_s->prev_gas_resp);
  455. wpa_s->prev_gas_resp = NULL;
  456. os_free(wpa_s->last_scan_res);
  457. wpa_s->last_scan_res = NULL;
  458. #ifdef CONFIG_HS20
  459. hs20_deinit(wpa_s);
  460. #endif /* CONFIG_HS20 */
  461. for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
  462. wpabuf_free(wpa_s->vendor_elem[i]);
  463. wpa_s->vendor_elem[i] = NULL;
  464. }
  465. wmm_ac_notify_disassoc(wpa_s);
  466. wpa_s->sched_scan_plans_num = 0;
  467. os_free(wpa_s->sched_scan_plans);
  468. wpa_s->sched_scan_plans = NULL;
  469. }
  470. /**
  471. * wpa_clear_keys - Clear keys configured for the driver
  472. * @wpa_s: Pointer to wpa_supplicant data
  473. * @addr: Previously used BSSID or %NULL if not available
  474. *
  475. * This function clears the encryption keys that has been previously configured
  476. * for the driver.
  477. */
  478. void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
  479. {
  480. int i, max;
  481. #ifdef CONFIG_IEEE80211W
  482. max = 6;
  483. #else /* CONFIG_IEEE80211W */
  484. max = 4;
  485. #endif /* CONFIG_IEEE80211W */
  486. /* MLME-DELETEKEYS.request */
  487. for (i = 0; i < max; i++) {
  488. if (wpa_s->keys_cleared & BIT(i))
  489. continue;
  490. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, i, 0, NULL, 0,
  491. NULL, 0);
  492. }
  493. if (!(wpa_s->keys_cleared & BIT(0)) && addr &&
  494. !is_zero_ether_addr(addr)) {
  495. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
  496. 0);
  497. /* MLME-SETPROTECTION.request(None) */
  498. wpa_drv_mlme_setprotection(
  499. wpa_s, addr,
  500. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  501. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  502. }
  503. wpa_s->keys_cleared = (u32) -1;
  504. }
  505. /**
  506. * wpa_supplicant_state_txt - Get the connection state name as a text string
  507. * @state: State (wpa_state; WPA_*)
  508. * Returns: The state name as a printable text string
  509. */
  510. const char * wpa_supplicant_state_txt(enum wpa_states state)
  511. {
  512. switch (state) {
  513. case WPA_DISCONNECTED:
  514. return "DISCONNECTED";
  515. case WPA_INACTIVE:
  516. return "INACTIVE";
  517. case WPA_INTERFACE_DISABLED:
  518. return "INTERFACE_DISABLED";
  519. case WPA_SCANNING:
  520. return "SCANNING";
  521. case WPA_AUTHENTICATING:
  522. return "AUTHENTICATING";
  523. case WPA_ASSOCIATING:
  524. return "ASSOCIATING";
  525. case WPA_ASSOCIATED:
  526. return "ASSOCIATED";
  527. case WPA_4WAY_HANDSHAKE:
  528. return "4WAY_HANDSHAKE";
  529. case WPA_GROUP_HANDSHAKE:
  530. return "GROUP_HANDSHAKE";
  531. case WPA_COMPLETED:
  532. return "COMPLETED";
  533. default:
  534. return "UNKNOWN";
  535. }
  536. }
  537. #ifdef CONFIG_BGSCAN
  538. static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
  539. {
  540. const char *name;
  541. if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan)
  542. name = wpa_s->current_ssid->bgscan;
  543. else
  544. name = wpa_s->conf->bgscan;
  545. if (name == NULL || name[0] == '\0')
  546. return;
  547. if (wpas_driver_bss_selection(wpa_s))
  548. return;
  549. if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
  550. return;
  551. #ifdef CONFIG_P2P
  552. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  553. return;
  554. #endif /* CONFIG_P2P */
  555. bgscan_deinit(wpa_s);
  556. if (wpa_s->current_ssid) {
  557. if (bgscan_init(wpa_s, wpa_s->current_ssid, name)) {
  558. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  559. "bgscan");
  560. /*
  561. * Live without bgscan; it is only used as a roaming
  562. * optimization, so the initial connection is not
  563. * affected.
  564. */
  565. } else {
  566. struct wpa_scan_results *scan_res;
  567. wpa_s->bgscan_ssid = wpa_s->current_ssid;
  568. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL,
  569. 0);
  570. if (scan_res) {
  571. bgscan_notify_scan(wpa_s, scan_res);
  572. wpa_scan_results_free(scan_res);
  573. }
  574. }
  575. } else
  576. wpa_s->bgscan_ssid = NULL;
  577. }
  578. static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
  579. {
  580. if (wpa_s->bgscan_ssid != NULL) {
  581. bgscan_deinit(wpa_s);
  582. wpa_s->bgscan_ssid = NULL;
  583. }
  584. }
  585. #endif /* CONFIG_BGSCAN */
  586. static void wpa_supplicant_start_autoscan(struct wpa_supplicant *wpa_s)
  587. {
  588. if (autoscan_init(wpa_s, 0))
  589. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize autoscan");
  590. }
  591. static void wpa_supplicant_stop_autoscan(struct wpa_supplicant *wpa_s)
  592. {
  593. autoscan_deinit(wpa_s);
  594. }
  595. void wpa_supplicant_reinit_autoscan(struct wpa_supplicant *wpa_s)
  596. {
  597. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  598. wpa_s->wpa_state == WPA_SCANNING) {
  599. autoscan_deinit(wpa_s);
  600. wpa_supplicant_start_autoscan(wpa_s);
  601. }
  602. }
  603. /**
  604. * wpa_supplicant_set_state - Set current connection state
  605. * @wpa_s: Pointer to wpa_supplicant data
  606. * @state: The new connection state
  607. *
  608. * This function is called whenever the connection state changes, e.g.,
  609. * association is completed for WPA/WPA2 4-Way Handshake is started.
  610. */
  611. void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
  612. enum wpa_states state)
  613. {
  614. enum wpa_states old_state = wpa_s->wpa_state;
  615. wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
  616. wpa_supplicant_state_txt(wpa_s->wpa_state),
  617. wpa_supplicant_state_txt(state));
  618. if (state == WPA_INTERFACE_DISABLED) {
  619. /* Assure normal scan when interface is restored */
  620. wpa_s->normal_scans = 0;
  621. }
  622. if (state == WPA_COMPLETED) {
  623. wpas_connect_work_done(wpa_s);
  624. /* Reinitialize normal_scan counter */
  625. wpa_s->normal_scans = 0;
  626. }
  627. #ifdef CONFIG_P2P
  628. /*
  629. * P2PS client has to reply to Probe Request frames received on the
  630. * group operating channel. Enable Probe Request frame reporting for
  631. * P2P connected client in case p2p_cli_probe configuration property is
  632. * set to 1.
  633. */
  634. if (wpa_s->conf->p2p_cli_probe && wpa_s->current_ssid &&
  635. wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  636. wpa_s->current_ssid->p2p_group) {
  637. if (state == WPA_COMPLETED && !wpa_s->p2p_cli_probe) {
  638. wpa_dbg(wpa_s, MSG_DEBUG,
  639. "P2P: Enable CLI Probe Request RX reporting");
  640. wpa_s->p2p_cli_probe =
  641. wpa_drv_probe_req_report(wpa_s, 1) >= 0;
  642. } else if (state != WPA_COMPLETED && wpa_s->p2p_cli_probe) {
  643. wpa_dbg(wpa_s, MSG_DEBUG,
  644. "P2P: Disable CLI Probe Request RX reporting");
  645. wpa_s->p2p_cli_probe = 0;
  646. wpa_drv_probe_req_report(wpa_s, 0);
  647. }
  648. }
  649. #endif /* CONFIG_P2P */
  650. if (state != WPA_SCANNING)
  651. wpa_supplicant_notify_scanning(wpa_s, 0);
  652. if (state == WPA_COMPLETED && wpa_s->new_connection) {
  653. struct wpa_ssid *ssid = wpa_s->current_ssid;
  654. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  655. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
  656. MACSTR " completed [id=%d id_str=%s]",
  657. MAC2STR(wpa_s->bssid),
  658. ssid ? ssid->id : -1,
  659. ssid && ssid->id_str ? ssid->id_str : "");
  660. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  661. wpas_clear_temp_disabled(wpa_s, ssid, 1);
  662. wpa_blacklist_clear(wpa_s);
  663. wpa_s->extra_blacklist_count = 0;
  664. wpa_s->new_connection = 0;
  665. wpa_drv_set_operstate(wpa_s, 1);
  666. #ifndef IEEE8021X_EAPOL
  667. wpa_drv_set_supp_port(wpa_s, 1);
  668. #endif /* IEEE8021X_EAPOL */
  669. wpa_s->after_wps = 0;
  670. wpa_s->known_wps_freq = 0;
  671. wpas_p2p_completed(wpa_s);
  672. sme_sched_obss_scan(wpa_s, 1);
  673. } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
  674. state == WPA_ASSOCIATED) {
  675. wpa_s->new_connection = 1;
  676. wpa_drv_set_operstate(wpa_s, 0);
  677. #ifndef IEEE8021X_EAPOL
  678. wpa_drv_set_supp_port(wpa_s, 0);
  679. #endif /* IEEE8021X_EAPOL */
  680. sme_sched_obss_scan(wpa_s, 0);
  681. }
  682. wpa_s->wpa_state = state;
  683. #ifdef CONFIG_BGSCAN
  684. if (state == WPA_COMPLETED)
  685. wpa_supplicant_start_bgscan(wpa_s);
  686. else if (state < WPA_ASSOCIATED)
  687. wpa_supplicant_stop_bgscan(wpa_s);
  688. #endif /* CONFIG_BGSCAN */
  689. if (state == WPA_AUTHENTICATING)
  690. wpa_supplicant_stop_autoscan(wpa_s);
  691. if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  692. wpa_supplicant_start_autoscan(wpa_s);
  693. if (old_state >= WPA_ASSOCIATED && wpa_s->wpa_state < WPA_ASSOCIATED)
  694. wmm_ac_notify_disassoc(wpa_s);
  695. if (wpa_s->wpa_state != old_state) {
  696. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  697. /*
  698. * Notify the P2P Device interface about a state change in one
  699. * of the interfaces.
  700. */
  701. wpas_p2p_indicate_state_change(wpa_s);
  702. if (wpa_s->wpa_state == WPA_COMPLETED ||
  703. old_state == WPA_COMPLETED)
  704. wpas_notify_auth_changed(wpa_s);
  705. }
  706. }
  707. void wpa_supplicant_terminate_proc(struct wpa_global *global)
  708. {
  709. int pending = 0;
  710. #ifdef CONFIG_WPS
  711. struct wpa_supplicant *wpa_s = global->ifaces;
  712. while (wpa_s) {
  713. struct wpa_supplicant *next = wpa_s->next;
  714. if (wpas_wps_terminate_pending(wpa_s) == 1)
  715. pending = 1;
  716. #ifdef CONFIG_P2P
  717. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE ||
  718. (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group))
  719. wpas_p2p_disconnect(wpa_s);
  720. #endif /* CONFIG_P2P */
  721. wpa_s = next;
  722. }
  723. #endif /* CONFIG_WPS */
  724. if (pending)
  725. return;
  726. eloop_terminate();
  727. }
  728. static void wpa_supplicant_terminate(int sig, void *signal_ctx)
  729. {
  730. struct wpa_global *global = signal_ctx;
  731. wpa_supplicant_terminate_proc(global);
  732. }
  733. void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
  734. {
  735. enum wpa_states old_state = wpa_s->wpa_state;
  736. wpa_s->pairwise_cipher = 0;
  737. wpa_s->group_cipher = 0;
  738. wpa_s->mgmt_group_cipher = 0;
  739. wpa_s->key_mgmt = 0;
  740. if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
  741. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  742. if (wpa_s->wpa_state != old_state)
  743. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  744. }
  745. /**
  746. * wpa_supplicant_reload_configuration - Reload configuration data
  747. * @wpa_s: Pointer to wpa_supplicant data
  748. * Returns: 0 on success or -1 if configuration parsing failed
  749. *
  750. * This function can be used to request that the configuration data is reloaded
  751. * (e.g., after configuration file change). This function is reloading
  752. * configuration only for one interface, so this may need to be called multiple
  753. * times if %wpa_supplicant is controlling multiple interfaces and all
  754. * interfaces need reconfiguration.
  755. */
  756. int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
  757. {
  758. struct wpa_config *conf;
  759. int reconf_ctrl;
  760. int old_ap_scan;
  761. if (wpa_s->confname == NULL)
  762. return -1;
  763. conf = wpa_config_read(wpa_s->confname, NULL);
  764. if (conf == NULL) {
  765. wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
  766. "file '%s' - exiting", wpa_s->confname);
  767. return -1;
  768. }
  769. wpa_config_read(wpa_s->confanother, conf);
  770. conf->changed_parameters = (unsigned int) -1;
  771. reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
  772. || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
  773. os_strcmp(conf->ctrl_interface,
  774. wpa_s->conf->ctrl_interface) != 0);
  775. if (reconf_ctrl && wpa_s->ctrl_iface) {
  776. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  777. wpa_s->ctrl_iface = NULL;
  778. }
  779. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  780. if (wpa_s->current_ssid) {
  781. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  782. wpa_s->own_disconnect_req = 1;
  783. wpa_supplicant_deauthenticate(wpa_s,
  784. WLAN_REASON_DEAUTH_LEAVING);
  785. }
  786. /*
  787. * TODO: should notify EAPOL SM about changes in opensc_engine_path,
  788. * pkcs11_engine_path, pkcs11_module_path, openssl_ciphers.
  789. */
  790. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  791. /*
  792. * Clear forced success to clear EAP state for next
  793. * authentication.
  794. */
  795. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  796. }
  797. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  798. wpa_sm_set_config(wpa_s->wpa, NULL);
  799. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  800. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  801. rsn_preauth_deinit(wpa_s->wpa);
  802. old_ap_scan = wpa_s->conf->ap_scan;
  803. wpa_config_free(wpa_s->conf);
  804. wpa_s->conf = conf;
  805. if (old_ap_scan != wpa_s->conf->ap_scan)
  806. wpas_notify_ap_scan_changed(wpa_s);
  807. if (reconf_ctrl)
  808. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  809. wpa_supplicant_update_config(wpa_s);
  810. wpa_supplicant_clear_status(wpa_s);
  811. if (wpa_supplicant_enabled_networks(wpa_s)) {
  812. wpa_s->reassociate = 1;
  813. wpa_supplicant_req_scan(wpa_s, 0, 0);
  814. }
  815. wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
  816. return 0;
  817. }
  818. static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
  819. {
  820. struct wpa_global *global = signal_ctx;
  821. struct wpa_supplicant *wpa_s;
  822. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  823. wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
  824. sig);
  825. if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
  826. wpa_supplicant_terminate_proc(global);
  827. }
  828. }
  829. if (wpa_debug_reopen_file() < 0) {
  830. /* Ignore errors since we cannot really do much to fix this */
  831. wpa_printf(MSG_DEBUG, "Could not reopen debug log file");
  832. }
  833. }
  834. static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
  835. struct wpa_ssid *ssid,
  836. struct wpa_ie_data *ie)
  837. {
  838. int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
  839. if (ret) {
  840. if (ret == -2) {
  841. wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
  842. "from association info");
  843. }
  844. return -1;
  845. }
  846. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
  847. "cipher suites");
  848. if (!(ie->group_cipher & ssid->group_cipher)) {
  849. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
  850. "cipher 0x%x (mask 0x%x) - reject",
  851. ie->group_cipher, ssid->group_cipher);
  852. return -1;
  853. }
  854. if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
  855. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
  856. "cipher 0x%x (mask 0x%x) - reject",
  857. ie->pairwise_cipher, ssid->pairwise_cipher);
  858. return -1;
  859. }
  860. if (!(ie->key_mgmt & ssid->key_mgmt)) {
  861. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
  862. "management 0x%x (mask 0x%x) - reject",
  863. ie->key_mgmt, ssid->key_mgmt);
  864. return -1;
  865. }
  866. #ifdef CONFIG_IEEE80211W
  867. if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
  868. wpas_get_ssid_pmf(wpa_s, ssid) == MGMT_FRAME_PROTECTION_REQUIRED) {
  869. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
  870. "that does not support management frame protection - "
  871. "reject");
  872. return -1;
  873. }
  874. #endif /* CONFIG_IEEE80211W */
  875. return 0;
  876. }
  877. /**
  878. * wpa_supplicant_set_suites - Set authentication and encryption parameters
  879. * @wpa_s: Pointer to wpa_supplicant data
  880. * @bss: Scan results for the selected BSS, or %NULL if not available
  881. * @ssid: Configuration data for the selected network
  882. * @wpa_ie: Buffer for the WPA/RSN IE
  883. * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
  884. * used buffer length in case the functions returns success.
  885. * Returns: 0 on success or -1 on failure
  886. *
  887. * This function is used to configure authentication and encryption parameters
  888. * based on the network configuration and scan result for the selected BSS (if
  889. * available).
  890. */
  891. int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
  892. struct wpa_bss *bss, struct wpa_ssid *ssid,
  893. u8 *wpa_ie, size_t *wpa_ie_len)
  894. {
  895. struct wpa_ie_data ie;
  896. int sel, proto;
  897. const u8 *bss_wpa, *bss_rsn, *bss_osen;
  898. if (bss) {
  899. bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  900. bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  901. bss_osen = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
  902. } else
  903. bss_wpa = bss_rsn = bss_osen = NULL;
  904. if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
  905. wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
  906. (ie.group_cipher & ssid->group_cipher) &&
  907. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  908. (ie.key_mgmt & ssid->key_mgmt)) {
  909. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
  910. proto = WPA_PROTO_RSN;
  911. } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
  912. wpa_parse_wpa_ie(bss_wpa, 2 + bss_wpa[1], &ie) == 0 &&
  913. (ie.group_cipher & ssid->group_cipher) &&
  914. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  915. (ie.key_mgmt & ssid->key_mgmt)) {
  916. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
  917. proto = WPA_PROTO_WPA;
  918. #ifdef CONFIG_HS20
  919. } else if (bss_osen && (ssid->proto & WPA_PROTO_OSEN)) {
  920. wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using OSEN");
  921. /* TODO: parse OSEN element */
  922. os_memset(&ie, 0, sizeof(ie));
  923. ie.group_cipher = WPA_CIPHER_CCMP;
  924. ie.pairwise_cipher = WPA_CIPHER_CCMP;
  925. ie.key_mgmt = WPA_KEY_MGMT_OSEN;
  926. proto = WPA_PROTO_OSEN;
  927. #endif /* CONFIG_HS20 */
  928. } else if (bss) {
  929. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
  930. wpa_dbg(wpa_s, MSG_DEBUG,
  931. "WPA: ssid proto=0x%x pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
  932. ssid->proto, ssid->pairwise_cipher, ssid->group_cipher,
  933. ssid->key_mgmt);
  934. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: BSS " MACSTR " ssid='%s'%s%s%s",
  935. MAC2STR(bss->bssid),
  936. wpa_ssid_txt(bss->ssid, bss->ssid_len),
  937. bss_wpa ? " WPA" : "",
  938. bss_rsn ? " RSN" : "",
  939. bss_osen ? " OSEN" : "");
  940. if (bss_rsn) {
  941. wpa_hexdump(MSG_DEBUG, "RSN", bss_rsn, 2 + bss_rsn[1]);
  942. if (wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie)) {
  943. wpa_dbg(wpa_s, MSG_DEBUG,
  944. "Could not parse RSN element");
  945. } else {
  946. wpa_dbg(wpa_s, MSG_DEBUG,
  947. "RSN: pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
  948. ie.pairwise_cipher, ie.group_cipher,
  949. ie.key_mgmt);
  950. }
  951. }
  952. if (bss_wpa) {
  953. wpa_hexdump(MSG_DEBUG, "WPA", bss_wpa, 2 + bss_wpa[1]);
  954. if (wpa_parse_wpa_ie(bss_wpa, 2 + bss_wpa[1], &ie)) {
  955. wpa_dbg(wpa_s, MSG_DEBUG,
  956. "Could not parse WPA element");
  957. } else {
  958. wpa_dbg(wpa_s, MSG_DEBUG,
  959. "WPA: pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
  960. ie.pairwise_cipher, ie.group_cipher,
  961. ie.key_mgmt);
  962. }
  963. }
  964. return -1;
  965. } else {
  966. if (ssid->proto & WPA_PROTO_OSEN)
  967. proto = WPA_PROTO_OSEN;
  968. else if (ssid->proto & WPA_PROTO_RSN)
  969. proto = WPA_PROTO_RSN;
  970. else
  971. proto = WPA_PROTO_WPA;
  972. if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
  973. os_memset(&ie, 0, sizeof(ie));
  974. ie.group_cipher = ssid->group_cipher;
  975. ie.pairwise_cipher = ssid->pairwise_cipher;
  976. ie.key_mgmt = ssid->key_mgmt;
  977. #ifdef CONFIG_IEEE80211W
  978. ie.mgmt_group_cipher =
  979. ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
  980. WPA_CIPHER_AES_128_CMAC : 0;
  981. #endif /* CONFIG_IEEE80211W */
  982. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
  983. "based on configuration");
  984. } else
  985. proto = ie.proto;
  986. }
  987. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
  988. "pairwise %d key_mgmt %d proto %d",
  989. ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
  990. #ifdef CONFIG_IEEE80211W
  991. if (ssid->ieee80211w) {
  992. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
  993. ie.mgmt_group_cipher);
  994. }
  995. #endif /* CONFIG_IEEE80211W */
  996. wpa_s->wpa_proto = proto;
  997. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
  998. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
  999. !!(ssid->proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)));
  1000. if (bss || !wpa_s->ap_ies_from_associnfo) {
  1001. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
  1002. bss_wpa ? 2 + bss_wpa[1] : 0) ||
  1003. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
  1004. bss_rsn ? 2 + bss_rsn[1] : 0))
  1005. return -1;
  1006. }
  1007. #ifdef CONFIG_NO_WPA
  1008. wpa_s->group_cipher = WPA_CIPHER_NONE;
  1009. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  1010. #else /* CONFIG_NO_WPA */
  1011. sel = ie.group_cipher & ssid->group_cipher;
  1012. wpa_s->group_cipher = wpa_pick_group_cipher(sel);
  1013. if (wpa_s->group_cipher < 0) {
  1014. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
  1015. "cipher");
  1016. return -1;
  1017. }
  1018. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK %s",
  1019. wpa_cipher_txt(wpa_s->group_cipher));
  1020. sel = ie.pairwise_cipher & ssid->pairwise_cipher;
  1021. wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(sel, 1);
  1022. if (wpa_s->pairwise_cipher < 0) {
  1023. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
  1024. "cipher");
  1025. return -1;
  1026. }
  1027. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK %s",
  1028. wpa_cipher_txt(wpa_s->pairwise_cipher));
  1029. #endif /* CONFIG_NO_WPA */
  1030. sel = ie.key_mgmt & ssid->key_mgmt;
  1031. #ifdef CONFIG_SAE
  1032. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE))
  1033. sel &= ~(WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_FT_SAE);
  1034. #endif /* CONFIG_SAE */
  1035. if (0) {
  1036. #ifdef CONFIG_SUITEB192
  1037. } else if (sel & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
  1038. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
  1039. wpa_dbg(wpa_s, MSG_DEBUG,
  1040. "WPA: using KEY_MGMT 802.1X with Suite B (192-bit)");
  1041. #endif /* CONFIG_SUITEB192 */
  1042. #ifdef CONFIG_SUITEB
  1043. } else if (sel & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
  1044. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B;
  1045. wpa_dbg(wpa_s, MSG_DEBUG,
  1046. "WPA: using KEY_MGMT 802.1X with Suite B");
  1047. #endif /* CONFIG_SUITEB */
  1048. #ifdef CONFIG_IEEE80211R
  1049. } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
  1050. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
  1051. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
  1052. } else if (sel & WPA_KEY_MGMT_FT_PSK) {
  1053. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
  1054. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
  1055. #endif /* CONFIG_IEEE80211R */
  1056. #ifdef CONFIG_SAE
  1057. } else if (sel & WPA_KEY_MGMT_SAE) {
  1058. wpa_s->key_mgmt = WPA_KEY_MGMT_SAE;
  1059. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT SAE");
  1060. } else if (sel & WPA_KEY_MGMT_FT_SAE) {
  1061. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_SAE;
  1062. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT FT/SAE");
  1063. #endif /* CONFIG_SAE */
  1064. #ifdef CONFIG_IEEE80211W
  1065. } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  1066. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
  1067. wpa_dbg(wpa_s, MSG_DEBUG,
  1068. "WPA: using KEY_MGMT 802.1X with SHA256");
  1069. } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
  1070. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
  1071. wpa_dbg(wpa_s, MSG_DEBUG,
  1072. "WPA: using KEY_MGMT PSK with SHA256");
  1073. #endif /* CONFIG_IEEE80211W */
  1074. } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
  1075. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  1076. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
  1077. } else if (sel & WPA_KEY_MGMT_PSK) {
  1078. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  1079. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
  1080. } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
  1081. wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
  1082. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
  1083. #ifdef CONFIG_HS20
  1084. } else if (sel & WPA_KEY_MGMT_OSEN) {
  1085. wpa_s->key_mgmt = WPA_KEY_MGMT_OSEN;
  1086. wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using KEY_MGMT OSEN");
  1087. #endif /* CONFIG_HS20 */
  1088. } else {
  1089. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
  1090. "authenticated key management type");
  1091. return -1;
  1092. }
  1093. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  1094. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  1095. wpa_s->pairwise_cipher);
  1096. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  1097. #ifdef CONFIG_IEEE80211W
  1098. sel = ie.mgmt_group_cipher;
  1099. if (wpas_get_ssid_pmf(wpa_s, ssid) == NO_MGMT_FRAME_PROTECTION ||
  1100. !(ie.capabilities & WPA_CAPABILITY_MFPC))
  1101. sel = 0;
  1102. if (sel & WPA_CIPHER_AES_128_CMAC) {
  1103. wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
  1104. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  1105. "AES-128-CMAC");
  1106. } else if (sel & WPA_CIPHER_BIP_GMAC_128) {
  1107. wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_128;
  1108. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  1109. "BIP-GMAC-128");
  1110. } else if (sel & WPA_CIPHER_BIP_GMAC_256) {
  1111. wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_256;
  1112. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  1113. "BIP-GMAC-256");
  1114. } else if (sel & WPA_CIPHER_BIP_CMAC_256) {
  1115. wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_CMAC_256;
  1116. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  1117. "BIP-CMAC-256");
  1118. } else {
  1119. wpa_s->mgmt_group_cipher = 0;
  1120. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
  1121. }
  1122. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  1123. wpa_s->mgmt_group_cipher);
  1124. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP,
  1125. wpas_get_ssid_pmf(wpa_s, ssid));
  1126. #endif /* CONFIG_IEEE80211W */
  1127. if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
  1128. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
  1129. return -1;
  1130. }
  1131. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
  1132. int psk_set = 0;
  1133. if (ssid->psk_set) {
  1134. wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN, NULL,
  1135. NULL);
  1136. psk_set = 1;
  1137. }
  1138. #ifndef CONFIG_NO_PBKDF2
  1139. if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
  1140. ssid->passphrase) {
  1141. u8 psk[PMK_LEN];
  1142. pbkdf2_sha1(ssid->passphrase, bss->ssid, bss->ssid_len,
  1143. 4096, psk, PMK_LEN);
  1144. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  1145. psk, PMK_LEN);
  1146. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL, NULL);
  1147. psk_set = 1;
  1148. os_memset(psk, 0, sizeof(psk));
  1149. }
  1150. #endif /* CONFIG_NO_PBKDF2 */
  1151. #ifdef CONFIG_EXT_PASSWORD
  1152. if (ssid->ext_psk) {
  1153. struct wpabuf *pw = ext_password_get(wpa_s->ext_pw,
  1154. ssid->ext_psk);
  1155. char pw_str[64 + 1];
  1156. u8 psk[PMK_LEN];
  1157. if (pw == NULL) {
  1158. wpa_msg(wpa_s, MSG_INFO, "EXT PW: No PSK "
  1159. "found from external storage");
  1160. return -1;
  1161. }
  1162. if (wpabuf_len(pw) < 8 || wpabuf_len(pw) > 64) {
  1163. wpa_msg(wpa_s, MSG_INFO, "EXT PW: Unexpected "
  1164. "PSK length %d in external storage",
  1165. (int) wpabuf_len(pw));
  1166. ext_password_free(pw);
  1167. return -1;
  1168. }
  1169. os_memcpy(pw_str, wpabuf_head(pw), wpabuf_len(pw));
  1170. pw_str[wpabuf_len(pw)] = '\0';
  1171. #ifndef CONFIG_NO_PBKDF2
  1172. if (wpabuf_len(pw) >= 8 && wpabuf_len(pw) < 64 && bss)
  1173. {
  1174. pbkdf2_sha1(pw_str, bss->ssid, bss->ssid_len,
  1175. 4096, psk, PMK_LEN);
  1176. os_memset(pw_str, 0, sizeof(pw_str));
  1177. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from "
  1178. "external passphrase)",
  1179. psk, PMK_LEN);
  1180. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL,
  1181. NULL);
  1182. psk_set = 1;
  1183. os_memset(psk, 0, sizeof(psk));
  1184. } else
  1185. #endif /* CONFIG_NO_PBKDF2 */
  1186. if (wpabuf_len(pw) == 2 * PMK_LEN) {
  1187. if (hexstr2bin(pw_str, psk, PMK_LEN) < 0) {
  1188. wpa_msg(wpa_s, MSG_INFO, "EXT PW: "
  1189. "Invalid PSK hex string");
  1190. os_memset(pw_str, 0, sizeof(pw_str));
  1191. ext_password_free(pw);
  1192. return -1;
  1193. }
  1194. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL,
  1195. NULL);
  1196. psk_set = 1;
  1197. os_memset(psk, 0, sizeof(psk));
  1198. } else {
  1199. wpa_msg(wpa_s, MSG_INFO, "EXT PW: No suitable "
  1200. "PSK available");
  1201. os_memset(pw_str, 0, sizeof(pw_str));
  1202. ext_password_free(pw);
  1203. return -1;
  1204. }
  1205. os_memset(pw_str, 0, sizeof(pw_str));
  1206. ext_password_free(pw);
  1207. }
  1208. #endif /* CONFIG_EXT_PASSWORD */
  1209. if (!psk_set) {
  1210. wpa_msg(wpa_s, MSG_INFO,
  1211. "No PSK available for association");
  1212. return -1;
  1213. }
  1214. } else
  1215. wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
  1216. return 0;
  1217. }
  1218. static void wpas_ext_capab_byte(struct wpa_supplicant *wpa_s, u8 *pos, int idx)
  1219. {
  1220. *pos = 0x00;
  1221. switch (idx) {
  1222. case 0: /* Bits 0-7 */
  1223. break;
  1224. case 1: /* Bits 8-15 */
  1225. break;
  1226. case 2: /* Bits 16-23 */
  1227. #ifdef CONFIG_WNM
  1228. *pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
  1229. *pos |= 0x08; /* Bit 19 - BSS Transition */
  1230. #endif /* CONFIG_WNM */
  1231. break;
  1232. case 3: /* Bits 24-31 */
  1233. #ifdef CONFIG_WNM
  1234. *pos |= 0x02; /* Bit 25 - SSID List */
  1235. #endif /* CONFIG_WNM */
  1236. #ifdef CONFIG_INTERWORKING
  1237. if (wpa_s->conf->interworking)
  1238. *pos |= 0x80; /* Bit 31 - Interworking */
  1239. #endif /* CONFIG_INTERWORKING */
  1240. break;
  1241. case 4: /* Bits 32-39 */
  1242. #ifdef CONFIG_INTERWORKING
  1243. if (wpa_s->drv_flags / WPA_DRIVER_FLAGS_QOS_MAPPING)
  1244. *pos |= 0x01; /* Bit 32 - QoS Map */
  1245. #endif /* CONFIG_INTERWORKING */
  1246. break;
  1247. case 5: /* Bits 40-47 */
  1248. #ifdef CONFIG_HS20
  1249. if (wpa_s->conf->hs20)
  1250. *pos |= 0x40; /* Bit 46 - WNM-Notification */
  1251. #endif /* CONFIG_HS20 */
  1252. break;
  1253. case 6: /* Bits 48-55 */
  1254. break;
  1255. }
  1256. }
  1257. int wpas_build_ext_capab(struct wpa_supplicant *wpa_s, u8 *buf, size_t buflen)
  1258. {
  1259. u8 *pos = buf;
  1260. u8 len = 6, i;
  1261. if (len < wpa_s->extended_capa_len)
  1262. len = wpa_s->extended_capa_len;
  1263. if (buflen < (size_t) len + 2) {
  1264. wpa_printf(MSG_INFO,
  1265. "Not enough room for building extended capabilities element");
  1266. return -1;
  1267. }
  1268. *pos++ = WLAN_EID_EXT_CAPAB;
  1269. *pos++ = len;
  1270. for (i = 0; i < len; i++, pos++) {
  1271. wpas_ext_capab_byte(wpa_s, pos, i);
  1272. if (i < wpa_s->extended_capa_len) {
  1273. *pos &= ~wpa_s->extended_capa_mask[i];
  1274. *pos |= wpa_s->extended_capa[i];
  1275. }
  1276. }
  1277. while (len > 0 && buf[1 + len] == 0) {
  1278. len--;
  1279. buf[1] = len;
  1280. }
  1281. if (len == 0)
  1282. return 0;
  1283. return 2 + len;
  1284. }
  1285. static int wpas_valid_bss(struct wpa_supplicant *wpa_s,
  1286. struct wpa_bss *test_bss)
  1287. {
  1288. struct wpa_bss *bss;
  1289. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1290. if (bss == test_bss)
  1291. return 1;
  1292. }
  1293. return 0;
  1294. }
  1295. static int wpas_valid_ssid(struct wpa_supplicant *wpa_s,
  1296. struct wpa_ssid *test_ssid)
  1297. {
  1298. struct wpa_ssid *ssid;
  1299. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1300. if (ssid == test_ssid)
  1301. return 1;
  1302. }
  1303. return 0;
  1304. }
  1305. int wpas_valid_bss_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *test_bss,
  1306. struct wpa_ssid *test_ssid)
  1307. {
  1308. if (test_bss && !wpas_valid_bss(wpa_s, test_bss))
  1309. return 0;
  1310. return test_ssid == NULL || wpas_valid_ssid(wpa_s, test_ssid);
  1311. }
  1312. void wpas_connect_work_free(struct wpa_connect_work *cwork)
  1313. {
  1314. if (cwork == NULL)
  1315. return;
  1316. os_free(cwork);
  1317. }
  1318. void wpas_connect_work_done(struct wpa_supplicant *wpa_s)
  1319. {
  1320. struct wpa_connect_work *cwork;
  1321. struct wpa_radio_work *work = wpa_s->connect_work;
  1322. if (!work)
  1323. return;
  1324. wpa_s->connect_work = NULL;
  1325. cwork = work->ctx;
  1326. work->ctx = NULL;
  1327. wpas_connect_work_free(cwork);
  1328. radio_work_done(work);
  1329. }
  1330. int wpas_update_random_addr(struct wpa_supplicant *wpa_s, int style)
  1331. {
  1332. struct os_reltime now;
  1333. u8 addr[ETH_ALEN];
  1334. os_get_reltime(&now);
  1335. if (wpa_s->last_mac_addr_style == style &&
  1336. wpa_s->last_mac_addr_change.sec != 0 &&
  1337. !os_reltime_expired(&now, &wpa_s->last_mac_addr_change,
  1338. wpa_s->conf->rand_addr_lifetime)) {
  1339. wpa_msg(wpa_s, MSG_DEBUG,
  1340. "Previously selected random MAC address has not yet expired");
  1341. return 0;
  1342. }
  1343. switch (style) {
  1344. case 1:
  1345. if (random_mac_addr(addr) < 0)
  1346. return -1;
  1347. break;
  1348. case 2:
  1349. os_memcpy(addr, wpa_s->perm_addr, ETH_ALEN);
  1350. if (random_mac_addr_keep_oui(addr) < 0)
  1351. return -1;
  1352. break;
  1353. default:
  1354. return -1;
  1355. }
  1356. if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
  1357. wpa_msg(wpa_s, MSG_INFO,
  1358. "Failed to set random MAC address");
  1359. return -1;
  1360. }
  1361. os_get_reltime(&wpa_s->last_mac_addr_change);
  1362. wpa_s->mac_addr_changed = 1;
  1363. wpa_s->last_mac_addr_style = style;
  1364. if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
  1365. wpa_msg(wpa_s, MSG_INFO,
  1366. "Could not update MAC address information");
  1367. return -1;
  1368. }
  1369. wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
  1370. MAC2STR(addr));
  1371. return 0;
  1372. }
  1373. int wpas_update_random_addr_disassoc(struct wpa_supplicant *wpa_s)
  1374. {
  1375. if (wpa_s->wpa_state >= WPA_AUTHENTICATING ||
  1376. !wpa_s->conf->preassoc_mac_addr)
  1377. return 0;
  1378. return wpas_update_random_addr(wpa_s, wpa_s->conf->preassoc_mac_addr);
  1379. }
  1380. static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit);
  1381. /**
  1382. * wpa_supplicant_associate - Request association
  1383. * @wpa_s: Pointer to wpa_supplicant data
  1384. * @bss: Scan results for the selected BSS, or %NULL if not available
  1385. * @ssid: Configuration data for the selected network
  1386. *
  1387. * This function is used to request %wpa_supplicant to associate with a BSS.
  1388. */
  1389. void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
  1390. struct wpa_bss *bss, struct wpa_ssid *ssid)
  1391. {
  1392. struct wpa_connect_work *cwork;
  1393. int rand_style;
  1394. wpa_s->own_disconnect_req = 0;
  1395. /*
  1396. * If we are starting a new connection, any previously pending EAPOL
  1397. * RX cannot be valid anymore.
  1398. */
  1399. wpabuf_free(wpa_s->pending_eapol_rx);
  1400. wpa_s->pending_eapol_rx = NULL;
  1401. if (ssid->mac_addr == -1)
  1402. rand_style = wpa_s->conf->mac_addr;
  1403. else
  1404. rand_style = ssid->mac_addr;
  1405. wmm_ac_clear_saved_tspecs(wpa_s);
  1406. wpa_s->reassoc_same_bss = 0;
  1407. if (wpa_s->last_ssid == ssid) {
  1408. wpa_dbg(wpa_s, MSG_DEBUG, "Re-association to the same ESS");
  1409. if (wpa_s->current_bss && wpa_s->current_bss == bss) {
  1410. wmm_ac_save_tspecs(wpa_s);
  1411. wpa_s->reassoc_same_bss = 1;
  1412. }
  1413. } else if (rand_style > 0) {
  1414. if (wpas_update_random_addr(wpa_s, rand_style) < 0)
  1415. return;
  1416. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  1417. } else if (wpa_s->mac_addr_changed) {
  1418. if (wpa_drv_set_mac_addr(wpa_s, NULL) < 0) {
  1419. wpa_msg(wpa_s, MSG_INFO,
  1420. "Could not restore permanent MAC address");
  1421. return;
  1422. }
  1423. wpa_s->mac_addr_changed = 0;
  1424. if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
  1425. wpa_msg(wpa_s, MSG_INFO,
  1426. "Could not update MAC address information");
  1427. return;
  1428. }
  1429. wpa_msg(wpa_s, MSG_DEBUG, "Using permanent MAC address");
  1430. }
  1431. wpa_s->last_ssid = ssid;
  1432. #ifdef CONFIG_IBSS_RSN
  1433. ibss_rsn_deinit(wpa_s->ibss_rsn);
  1434. wpa_s->ibss_rsn = NULL;
  1435. #endif /* CONFIG_IBSS_RSN */
  1436. if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
  1437. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  1438. #ifdef CONFIG_AP
  1439. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
  1440. wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
  1441. "mode");
  1442. return;
  1443. }
  1444. if (wpa_supplicant_create_ap(wpa_s, ssid) < 0) {
  1445. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  1446. if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  1447. wpas_p2p_ap_setup_failed(wpa_s);
  1448. return;
  1449. }
  1450. wpa_s->current_bss = bss;
  1451. #else /* CONFIG_AP */
  1452. wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
  1453. "the build");
  1454. #endif /* CONFIG_AP */
  1455. return;
  1456. }
  1457. if (ssid->mode == WPAS_MODE_MESH) {
  1458. #ifdef CONFIG_MESH
  1459. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MESH)) {
  1460. wpa_msg(wpa_s, MSG_INFO,
  1461. "Driver does not support mesh mode");
  1462. return;
  1463. }
  1464. if (bss)
  1465. ssid->frequency = bss->freq;
  1466. if (wpa_supplicant_join_mesh(wpa_s, ssid) < 0) {
  1467. wpa_msg(wpa_s, MSG_ERROR, "Could not join mesh");
  1468. return;
  1469. }
  1470. wpa_s->current_bss = bss;
  1471. wpa_msg(wpa_s, MSG_INFO, MESH_GROUP_STARTED "ssid=\"%s\" id=%d",
  1472. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  1473. ssid->id);
  1474. #else /* CONFIG_MESH */
  1475. wpa_msg(wpa_s, MSG_ERROR,
  1476. "mesh mode support not included in the build");
  1477. #endif /* CONFIG_MESH */
  1478. return;
  1479. }
  1480. #ifdef CONFIG_TDLS
  1481. if (bss)
  1482. wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
  1483. bss->ie_len);
  1484. #endif /* CONFIG_TDLS */
  1485. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
  1486. ssid->mode == IEEE80211_MODE_INFRA) {
  1487. sme_authenticate(wpa_s, bss, ssid);
  1488. return;
  1489. }
  1490. if (wpa_s->connect_work) {
  1491. wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since connect_work exist");
  1492. return;
  1493. }
  1494. if (radio_work_pending(wpa_s, "connect")) {
  1495. wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since pending work exist");
  1496. return;
  1497. }
  1498. wpas_abort_ongoing_scan(wpa_s);
  1499. cwork = os_zalloc(sizeof(*cwork));
  1500. if (cwork == NULL)
  1501. return;
  1502. cwork->bss = bss;
  1503. cwork->ssid = ssid;
  1504. if (radio_add_work(wpa_s, bss ? bss->freq : 0, "connect", 1,
  1505. wpas_start_assoc_cb, cwork) < 0) {
  1506. os_free(cwork);
  1507. }
  1508. }
  1509. static int bss_is_ibss(struct wpa_bss *bss)
  1510. {
  1511. return (bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
  1512. IEEE80211_CAP_IBSS;
  1513. }
  1514. static int drv_supports_vht(struct wpa_supplicant *wpa_s,
  1515. const struct wpa_ssid *ssid)
  1516. {
  1517. enum hostapd_hw_mode hw_mode;
  1518. struct hostapd_hw_modes *mode = NULL;
  1519. u8 channel;
  1520. int i;
  1521. #ifdef CONFIG_HT_OVERRIDES
  1522. if (ssid->disable_ht)
  1523. return 0;
  1524. #endif /* CONFIG_HT_OVERRIDES */
  1525. hw_mode = ieee80211_freq_to_chan(ssid->frequency, &channel);
  1526. if (hw_mode == NUM_HOSTAPD_MODES)
  1527. return 0;
  1528. for (i = 0; wpa_s->hw.modes && i < wpa_s->hw.num_modes; i++) {
  1529. if (wpa_s->hw.modes[i].mode == hw_mode) {
  1530. mode = &wpa_s->hw.modes[i];
  1531. break;
  1532. }
  1533. }
  1534. if (!mode)
  1535. return 0;
  1536. return mode->vht_capab != 0;
  1537. }
  1538. void ibss_mesh_setup_freq(struct wpa_supplicant *wpa_s,
  1539. const struct wpa_ssid *ssid,
  1540. struct hostapd_freq_params *freq)
  1541. {
  1542. enum hostapd_hw_mode hw_mode;
  1543. struct hostapd_hw_modes *mode = NULL;
  1544. int ht40plus[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
  1545. 184, 192 };
  1546. int vht80[] = { 36, 52, 100, 116, 132, 149 };
  1547. struct hostapd_channel_data *pri_chan = NULL, *sec_chan = NULL;
  1548. u8 channel;
  1549. int i, chan_idx, ht40 = -1, res, obss_scan = 1;
  1550. unsigned int j, k;
  1551. struct hostapd_freq_params vht_freq;
  1552. int chwidth, seg0, seg1;
  1553. u32 vht_caps = 0;
  1554. freq->freq = ssid->frequency;
  1555. for (j = 0; j < wpa_s->last_scan_res_used; j++) {
  1556. struct wpa_bss *bss = wpa_s->last_scan_res[j];
  1557. if (ssid->mode != WPAS_MODE_IBSS)
  1558. break;
  1559. /* Don't adjust control freq in case of fixed_freq */
  1560. if (ssid->fixed_freq)
  1561. break;
  1562. if (!bss_is_ibss(bss))
  1563. continue;
  1564. if (ssid->ssid_len == bss->ssid_len &&
  1565. os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) == 0) {
  1566. wpa_printf(MSG_DEBUG,
  1567. "IBSS already found in scan results, adjust control freq: %d",
  1568. bss->freq);
  1569. freq->freq = bss->freq;
  1570. obss_scan = 0;
  1571. break;
  1572. }
  1573. }
  1574. /* For IBSS check HT_IBSS flag */
  1575. if (ssid->mode == WPAS_MODE_IBSS &&
  1576. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_HT_IBSS))
  1577. return;
  1578. if (wpa_s->group_cipher == WPA_CIPHER_WEP40 ||
  1579. wpa_s->group_cipher == WPA_CIPHER_WEP104 ||
  1580. wpa_s->pairwise_cipher == WPA_CIPHER_TKIP) {
  1581. wpa_printf(MSG_DEBUG,
  1582. "IBSS: WEP/TKIP detected, do not try to enable HT");
  1583. return;
  1584. }
  1585. hw_mode = ieee80211_freq_to_chan(freq->freq, &channel);
  1586. for (i = 0; wpa_s->hw.modes && i < wpa_s->hw.num_modes; i++) {
  1587. if (wpa_s->hw.modes[i].mode == hw_mode) {
  1588. mode = &wpa_s->hw.modes[i];
  1589. break;
  1590. }
  1591. }
  1592. if (!mode)
  1593. return;
  1594. freq->ht_enabled = ht_supported(mode);
  1595. if (!freq->ht_enabled)
  1596. return;
  1597. /* Setup higher BW only for 5 GHz */
  1598. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  1599. return;
  1600. for (chan_idx = 0; chan_idx < mode->num_channels; chan_idx++) {
  1601. pri_chan = &mode->channels[chan_idx];
  1602. if (pri_chan->chan == channel)
  1603. break;
  1604. pri_chan = NULL;
  1605. }
  1606. if (!pri_chan)
  1607. return;
  1608. /* Check primary channel flags */
  1609. if (pri_chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
  1610. return;
  1611. /* Check/setup HT40+/HT40- */
  1612. for (j = 0; j < ARRAY_SIZE(ht40plus); j++) {
  1613. if (ht40plus[j] == channel) {
  1614. ht40 = 1;
  1615. break;
  1616. }
  1617. }
  1618. /* Find secondary channel */
  1619. for (i = 0; i < mode->num_channels; i++) {
  1620. sec_chan = &mode->channels[i];
  1621. if (sec_chan->chan == channel + ht40 * 4)
  1622. break;
  1623. sec_chan = NULL;
  1624. }
  1625. if (!sec_chan)
  1626. return;
  1627. /* Check secondary channel flags */
  1628. if (sec_chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
  1629. return;
  1630. freq->channel = pri_chan->chan;
  1631. switch (ht40) {
  1632. case -1:
  1633. if (!(pri_chan->flag & HOSTAPD_CHAN_HT40MINUS))
  1634. return;
  1635. freq->sec_channel_offset = -1;
  1636. break;
  1637. case 1:
  1638. if (!(pri_chan->flag & HOSTAPD_CHAN_HT40PLUS))
  1639. return;
  1640. freq->sec_channel_offset = 1;
  1641. break;
  1642. default:
  1643. break;
  1644. }
  1645. if (freq->sec_channel_offset && obss_scan) {
  1646. struct wpa_scan_results *scan_res;
  1647. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  1648. if (scan_res == NULL) {
  1649. /* Back to HT20 */
  1650. freq->sec_channel_offset = 0;
  1651. return;
  1652. }
  1653. res = check_40mhz_5g(mode, scan_res, pri_chan->chan,
  1654. sec_chan->chan);
  1655. switch (res) {
  1656. case 0:
  1657. /* Back to HT20 */
  1658. freq->sec_channel_offset = 0;
  1659. break;
  1660. case 1:
  1661. /* Configuration allowed */
  1662. break;
  1663. case 2:
  1664. /* Switch pri/sec channels */
  1665. freq->freq = hw_get_freq(mode, sec_chan->chan);
  1666. freq->sec_channel_offset = -freq->sec_channel_offset;
  1667. freq->channel = sec_chan->chan;
  1668. break;
  1669. default:
  1670. freq->sec_channel_offset = 0;
  1671. break;
  1672. }
  1673. wpa_scan_results_free(scan_res);
  1674. }
  1675. wpa_printf(MSG_DEBUG,
  1676. "IBSS/mesh: setup freq channel %d, sec_channel_offset %d",
  1677. freq->channel, freq->sec_channel_offset);
  1678. if (!drv_supports_vht(wpa_s, ssid))
  1679. return;
  1680. /* For IBSS check VHT_IBSS flag */
  1681. if (ssid->mode == WPAS_MODE_IBSS &&
  1682. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_VHT_IBSS))
  1683. return;
  1684. vht_freq = *freq;
  1685. vht_freq.vht_enabled = vht_supported(mode);
  1686. if (!vht_freq.vht_enabled)
  1687. return;
  1688. /* setup center_freq1, bandwidth */
  1689. for (j = 0; j < ARRAY_SIZE(vht80); j++) {
  1690. if (freq->channel >= vht80[j] &&
  1691. freq->channel < vht80[j] + 16)
  1692. break;
  1693. }
  1694. if (j == ARRAY_SIZE(vht80))
  1695. return;
  1696. for (i = vht80[j]; i < vht80[j] + 16; i += 4) {
  1697. struct hostapd_channel_data *chan;
  1698. chan = hw_get_channel_chan(mode, i, NULL);
  1699. if (!chan)
  1700. return;
  1701. /* Back to HT configuration if channel not usable */
  1702. if (chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
  1703. return;
  1704. }
  1705. chwidth = VHT_CHANWIDTH_80MHZ;
  1706. seg0 = vht80[j] + 6;
  1707. seg1 = 0;
  1708. if (ssid->max_oper_chwidth == VHT_CHANWIDTH_80P80MHZ) {
  1709. /* setup center_freq2, bandwidth */
  1710. for (k = 0; k < ARRAY_SIZE(vht80); k++) {
  1711. /* Only accept 80 MHz segments separated by a gap */
  1712. if (j == k || abs(vht80[j] - vht80[k]) == 16)
  1713. continue;
  1714. for (i = vht80[k]; i < vht80[k] + 16; i += 4) {
  1715. struct hostapd_channel_data *chan;
  1716. chan = hw_get_channel_chan(mode, i, NULL);
  1717. if (!chan)
  1718. continue;
  1719. if (chan->flag & (HOSTAPD_CHAN_DISABLED |
  1720. HOSTAPD_CHAN_NO_IR |
  1721. HOSTAPD_CHAN_RADAR))
  1722. continue;
  1723. /* Found a suitable second segment for 80+80 */
  1724. chwidth = VHT_CHANWIDTH_80P80MHZ;
  1725. vht_caps |=
  1726. VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
  1727. seg1 = vht80[k] + 6;
  1728. }
  1729. if (chwidth == VHT_CHANWIDTH_80P80MHZ)
  1730. break;
  1731. }
  1732. }
  1733. if (hostapd_set_freq_params(&vht_freq, mode->mode, freq->freq,
  1734. freq->channel, freq->ht_enabled,
  1735. vht_freq.vht_enabled,
  1736. freq->sec_channel_offset,
  1737. chwidth, seg0, seg1, vht_caps) != 0)
  1738. return;
  1739. *freq = vht_freq;
  1740. wpa_printf(MSG_DEBUG, "IBSS: VHT setup freq cf1 %d, cf2 %d, bw %d",
  1741. freq->center_freq1, freq->center_freq2, freq->bandwidth);
  1742. }
  1743. static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit)
  1744. {
  1745. struct wpa_connect_work *cwork = work->ctx;
  1746. struct wpa_bss *bss = cwork->bss;
  1747. struct wpa_ssid *ssid = cwork->ssid;
  1748. struct wpa_supplicant *wpa_s = work->wpa_s;
  1749. u8 wpa_ie[200];
  1750. size_t wpa_ie_len;
  1751. int use_crypt, ret, i, bssid_changed;
  1752. int algs = WPA_AUTH_ALG_OPEN;
  1753. unsigned int cipher_pairwise, cipher_group;
  1754. struct wpa_driver_associate_params params;
  1755. int wep_keys_set = 0;
  1756. int assoc_failed = 0;
  1757. struct wpa_ssid *old_ssid;
  1758. #ifdef CONFIG_HT_OVERRIDES
  1759. struct ieee80211_ht_capabilities htcaps;
  1760. struct ieee80211_ht_capabilities htcaps_mask;
  1761. #endif /* CONFIG_HT_OVERRIDES */
  1762. #ifdef CONFIG_VHT_OVERRIDES
  1763. struct ieee80211_vht_capabilities vhtcaps;
  1764. struct ieee80211_vht_capabilities vhtcaps_mask;
  1765. #endif /* CONFIG_VHT_OVERRIDES */
  1766. if (deinit) {
  1767. if (work->started) {
  1768. wpa_s->connect_work = NULL;
  1769. /* cancel possible auth. timeout */
  1770. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s,
  1771. NULL);
  1772. }
  1773. wpas_connect_work_free(cwork);
  1774. return;
  1775. }
  1776. wpa_s->connect_work = work;
  1777. if (cwork->bss_removed || !wpas_valid_bss_ssid(wpa_s, bss, ssid) ||
  1778. wpas_network_disabled(wpa_s, ssid)) {
  1779. wpa_dbg(wpa_s, MSG_DEBUG, "BSS/SSID entry for association not valid anymore - drop connection attempt");
  1780. wpas_connect_work_done(wpa_s);
  1781. return;
  1782. }
  1783. os_memset(&params, 0, sizeof(params));
  1784. wpa_s->reassociate = 0;
  1785. wpa_s->eap_expected_failure = 0;
  1786. if (bss &&
  1787. (!wpas_driver_bss_selection(wpa_s) || wpas_wps_searching(wpa_s))) {
  1788. #ifdef CONFIG_IEEE80211R
  1789. const u8 *ie, *md = NULL;
  1790. #endif /* CONFIG_IEEE80211R */
  1791. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
  1792. " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
  1793. wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
  1794. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  1795. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  1796. os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
  1797. if (bssid_changed)
  1798. wpas_notify_bssid_changed(wpa_s);
  1799. #ifdef CONFIG_IEEE80211R
  1800. ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  1801. if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
  1802. md = ie + 2;
  1803. wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
  1804. if (md) {
  1805. /* Prepare for the next transition */
  1806. wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
  1807. }
  1808. #endif /* CONFIG_IEEE80211R */
  1809. #ifdef CONFIG_WPS
  1810. } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
  1811. wpa_s->conf->ap_scan == 2 &&
  1812. (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  1813. /* Use ap_scan==1 style network selection to find the network
  1814. */
  1815. wpas_connect_work_done(wpa_s);
  1816. wpa_s->scan_req = MANUAL_SCAN_REQ;
  1817. wpa_s->reassociate = 1;
  1818. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1819. return;
  1820. #endif /* CONFIG_WPS */
  1821. } else {
  1822. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
  1823. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1824. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1825. }
  1826. if (!wpa_s->pno)
  1827. wpa_supplicant_cancel_sched_scan(wpa_s);
  1828. wpa_supplicant_cancel_scan(wpa_s);
  1829. /* Starting new association, so clear the possibly used WPA IE from the
  1830. * previous association. */
  1831. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  1832. #ifdef IEEE8021X_EAPOL
  1833. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1834. if (ssid->leap) {
  1835. if (ssid->non_leap == 0)
  1836. algs = WPA_AUTH_ALG_LEAP;
  1837. else
  1838. algs |= WPA_AUTH_ALG_LEAP;
  1839. }
  1840. }
  1841. #endif /* IEEE8021X_EAPOL */
  1842. wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
  1843. if (ssid->auth_alg) {
  1844. algs = ssid->auth_alg;
  1845. wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
  1846. "0x%x", algs);
  1847. }
  1848. if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
  1849. wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
  1850. wpa_key_mgmt_wpa(ssid->key_mgmt)) {
  1851. int try_opportunistic;
  1852. try_opportunistic = (ssid->proactive_key_caching < 0 ?
  1853. wpa_s->conf->okc :
  1854. ssid->proactive_key_caching) &&
  1855. (ssid->proto & WPA_PROTO_RSN);
  1856. if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
  1857. ssid, try_opportunistic) == 0)
  1858. eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
  1859. wpa_ie_len = sizeof(wpa_ie);
  1860. if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
  1861. wpa_ie, &wpa_ie_len)) {
  1862. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1863. "key management and encryption suites");
  1864. wpas_connect_work_done(wpa_s);
  1865. return;
  1866. }
  1867. } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && bss &&
  1868. wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
  1869. /*
  1870. * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
  1871. * use non-WPA since the scan results did not indicate that the
  1872. * AP is using WPA or WPA2.
  1873. */
  1874. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1875. wpa_ie_len = 0;
  1876. wpa_s->wpa_proto = 0;
  1877. } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
  1878. wpa_ie_len = sizeof(wpa_ie);
  1879. if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  1880. wpa_ie, &wpa_ie_len)) {
  1881. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1882. "key management and encryption suites (no "
  1883. "scan results)");
  1884. wpas_connect_work_done(wpa_s);
  1885. return;
  1886. }
  1887. #ifdef CONFIG_WPS
  1888. } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  1889. struct wpabuf *wps_ie;
  1890. wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
  1891. if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
  1892. wpa_ie_len = wpabuf_len(wps_ie);
  1893. os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
  1894. } else
  1895. wpa_ie_len = 0;
  1896. wpabuf_free(wps_ie);
  1897. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1898. if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
  1899. params.wps = WPS_MODE_PRIVACY;
  1900. else
  1901. params.wps = WPS_MODE_OPEN;
  1902. wpa_s->wpa_proto = 0;
  1903. #endif /* CONFIG_WPS */
  1904. } else {
  1905. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1906. wpa_ie_len = 0;
  1907. wpa_s->wpa_proto = 0;
  1908. }
  1909. #ifdef CONFIG_P2P
  1910. if (wpa_s->global->p2p) {
  1911. u8 *pos;
  1912. size_t len;
  1913. int res;
  1914. pos = wpa_ie + wpa_ie_len;
  1915. len = sizeof(wpa_ie) - wpa_ie_len;
  1916. res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
  1917. ssid->p2p_group);
  1918. if (res >= 0)
  1919. wpa_ie_len += res;
  1920. }
  1921. wpa_s->cross_connect_disallowed = 0;
  1922. if (bss) {
  1923. struct wpabuf *p2p;
  1924. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  1925. if (p2p) {
  1926. wpa_s->cross_connect_disallowed =
  1927. p2p_get_cross_connect_disallowed(p2p);
  1928. wpabuf_free(p2p);
  1929. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
  1930. "connection",
  1931. wpa_s->cross_connect_disallowed ?
  1932. "disallows" : "allows");
  1933. }
  1934. }
  1935. os_memset(wpa_s->p2p_ip_addr_info, 0, sizeof(wpa_s->p2p_ip_addr_info));
  1936. #endif /* CONFIG_P2P */
  1937. #ifdef CONFIG_HS20
  1938. if (is_hs20_network(wpa_s, ssid, bss)) {
  1939. struct wpabuf *hs20;
  1940. hs20 = wpabuf_alloc(20);
  1941. if (hs20) {
  1942. int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
  1943. size_t len;
  1944. wpas_hs20_add_indication(hs20, pps_mo_id);
  1945. len = sizeof(wpa_ie) - wpa_ie_len;
  1946. if (wpabuf_len(hs20) <= len) {
  1947. os_memcpy(wpa_ie + wpa_ie_len,
  1948. wpabuf_head(hs20), wpabuf_len(hs20));
  1949. wpa_ie_len += wpabuf_len(hs20);
  1950. }
  1951. wpabuf_free(hs20);
  1952. }
  1953. }
  1954. #endif /* CONFIG_HS20 */
  1955. /*
  1956. * Workaround: Add Extended Capabilities element only if the AP
  1957. * included this element in Beacon/Probe Response frames. Some older
  1958. * APs seem to have interoperability issues if this element is
  1959. * included, so while the standard may require us to include the
  1960. * element in all cases, it is justifiable to skip it to avoid
  1961. * interoperability issues.
  1962. */
  1963. if (!bss || wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB)) {
  1964. u8 ext_capab[18];
  1965. int ext_capab_len;
  1966. ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
  1967. sizeof(ext_capab));
  1968. if (ext_capab_len > 0) {
  1969. u8 *pos = wpa_ie;
  1970. if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
  1971. pos += 2 + pos[1];
  1972. os_memmove(pos + ext_capab_len, pos,
  1973. wpa_ie_len - (pos - wpa_ie));
  1974. wpa_ie_len += ext_capab_len;
  1975. os_memcpy(pos, ext_capab, ext_capab_len);
  1976. }
  1977. }
  1978. if (wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ]) {
  1979. struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ];
  1980. size_t len;
  1981. len = sizeof(wpa_ie) - wpa_ie_len;
  1982. if (wpabuf_len(buf) <= len) {
  1983. os_memcpy(wpa_ie + wpa_ie_len,
  1984. wpabuf_head(buf), wpabuf_len(buf));
  1985. wpa_ie_len += wpabuf_len(buf);
  1986. }
  1987. }
  1988. #ifdef CONFIG_FST
  1989. if (wpa_s->fst_ies) {
  1990. int fst_ies_len = wpabuf_len(wpa_s->fst_ies);
  1991. if (wpa_ie_len + fst_ies_len <= sizeof(wpa_ie)) {
  1992. os_memcpy(wpa_ie + wpa_ie_len,
  1993. wpabuf_head(wpa_s->fst_ies), fst_ies_len);
  1994. wpa_ie_len += fst_ies_len;
  1995. }
  1996. }
  1997. #endif /* CONFIG_FST */
  1998. wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
  1999. use_crypt = 1;
  2000. cipher_pairwise = wpa_s->pairwise_cipher;
  2001. cipher_group = wpa_s->group_cipher;
  2002. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  2003. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  2004. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
  2005. use_crypt = 0;
  2006. if (wpa_set_wep_keys(wpa_s, ssid)) {
  2007. use_crypt = 1;
  2008. wep_keys_set = 1;
  2009. }
  2010. }
  2011. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
  2012. use_crypt = 0;
  2013. #ifdef IEEE8021X_EAPOL
  2014. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  2015. if ((ssid->eapol_flags &
  2016. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  2017. EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
  2018. !wep_keys_set) {
  2019. use_crypt = 0;
  2020. } else {
  2021. /* Assume that dynamic WEP-104 keys will be used and
  2022. * set cipher suites in order for drivers to expect
  2023. * encryption. */
  2024. cipher_pairwise = cipher_group = WPA_CIPHER_WEP104;
  2025. }
  2026. }
  2027. #endif /* IEEE8021X_EAPOL */
  2028. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  2029. /* Set the key before (and later after) association */
  2030. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  2031. }
  2032. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
  2033. if (bss) {
  2034. params.ssid = bss->ssid;
  2035. params.ssid_len = bss->ssid_len;
  2036. if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set) {
  2037. wpa_printf(MSG_DEBUG, "Limit connection to BSSID "
  2038. MACSTR " freq=%u MHz based on scan results "
  2039. "(bssid_set=%d)",
  2040. MAC2STR(bss->bssid), bss->freq,
  2041. ssid->bssid_set);
  2042. params.bssid = bss->bssid;
  2043. params.freq.freq = bss->freq;
  2044. }
  2045. params.bssid_hint = bss->bssid;
  2046. params.freq_hint = bss->freq;
  2047. params.pbss = bss_is_pbss(bss);
  2048. } else {
  2049. params.ssid = ssid->ssid;
  2050. params.ssid_len = ssid->ssid_len;
  2051. params.pbss = ssid->pbss;
  2052. }
  2053. if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
  2054. wpa_s->conf->ap_scan == 2) {
  2055. params.bssid = ssid->bssid;
  2056. params.fixed_bssid = 1;
  2057. }
  2058. /* Initial frequency for IBSS/mesh */
  2059. if ((ssid->mode == WPAS_MODE_IBSS || ssid->mode == WPAS_MODE_MESH) &&
  2060. ssid->frequency > 0 && params.freq.freq == 0)
  2061. ibss_mesh_setup_freq(wpa_s, ssid, &params.freq);
  2062. if (ssid->mode == WPAS_MODE_IBSS) {
  2063. params.fixed_freq = ssid->fixed_freq;
  2064. if (ssid->beacon_int)
  2065. params.beacon_int = ssid->beacon_int;
  2066. else
  2067. params.beacon_int = wpa_s->conf->beacon_int;
  2068. }
  2069. params.wpa_ie = wpa_ie;
  2070. params.wpa_ie_len = wpa_ie_len;
  2071. params.pairwise_suite = cipher_pairwise;
  2072. params.group_suite = cipher_group;
  2073. params.key_mgmt_suite = wpa_s->key_mgmt;
  2074. params.wpa_proto = wpa_s->wpa_proto;
  2075. params.auth_alg = algs;
  2076. params.mode = ssid->mode;
  2077. params.bg_scan_period = ssid->bg_scan_period;
  2078. for (i = 0; i < NUM_WEP_KEYS; i++) {
  2079. if (ssid->wep_key_len[i])
  2080. params.wep_key[i] = ssid->wep_key[i];
  2081. params.wep_key_len[i] = ssid->wep_key_len[i];
  2082. }
  2083. params.wep_tx_keyidx = ssid->wep_tx_keyidx;
  2084. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  2085. (params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
  2086. params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
  2087. params.passphrase = ssid->passphrase;
  2088. if (ssid->psk_set)
  2089. params.psk = ssid->psk;
  2090. }
  2091. if (wpa_s->conf->key_mgmt_offload) {
  2092. if (params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
  2093. params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
  2094. params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
  2095. params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
  2096. params.req_key_mgmt_offload =
  2097. ssid->proactive_key_caching < 0 ?
  2098. wpa_s->conf->okc : ssid->proactive_key_caching;
  2099. else
  2100. params.req_key_mgmt_offload = 1;
  2101. if ((params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
  2102. params.key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
  2103. params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK) &&
  2104. ssid->psk_set)
  2105. params.psk = ssid->psk;
  2106. }
  2107. params.drop_unencrypted = use_crypt;
  2108. #ifdef CONFIG_IEEE80211W
  2109. params.mgmt_frame_protection = wpas_get_ssid_pmf(wpa_s, ssid);
  2110. if (params.mgmt_frame_protection != NO_MGMT_FRAME_PROTECTION && bss) {
  2111. const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  2112. struct wpa_ie_data ie;
  2113. if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
  2114. ie.capabilities &
  2115. (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
  2116. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
  2117. "MFP: require MFP");
  2118. params.mgmt_frame_protection =
  2119. MGMT_FRAME_PROTECTION_REQUIRED;
  2120. }
  2121. }
  2122. #endif /* CONFIG_IEEE80211W */
  2123. params.p2p = ssid->p2p_group;
  2124. if (wpa_s->parent->set_sta_uapsd)
  2125. params.uapsd = wpa_s->parent->sta_uapsd;
  2126. else
  2127. params.uapsd = -1;
  2128. #ifdef CONFIG_HT_OVERRIDES
  2129. os_memset(&htcaps, 0, sizeof(htcaps));
  2130. os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
  2131. params.htcaps = (u8 *) &htcaps;
  2132. params.htcaps_mask = (u8 *) &htcaps_mask;
  2133. wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
  2134. #endif /* CONFIG_HT_OVERRIDES */
  2135. #ifdef CONFIG_VHT_OVERRIDES
  2136. os_memset(&vhtcaps, 0, sizeof(vhtcaps));
  2137. os_memset(&vhtcaps_mask, 0, sizeof(vhtcaps_mask));
  2138. params.vhtcaps = &vhtcaps;
  2139. params.vhtcaps_mask = &vhtcaps_mask;
  2140. wpa_supplicant_apply_vht_overrides(wpa_s, ssid, &params);
  2141. #endif /* CONFIG_VHT_OVERRIDES */
  2142. #ifdef CONFIG_P2P
  2143. /*
  2144. * If multi-channel concurrency is not supported, check for any
  2145. * frequency conflict. In case of any frequency conflict, remove the
  2146. * least prioritized connection.
  2147. */
  2148. if (wpa_s->num_multichan_concurrent < 2) {
  2149. int freq, num;
  2150. num = get_shared_radio_freqs(wpa_s, &freq, 1);
  2151. if (num > 0 && freq > 0 && freq != params.freq.freq) {
  2152. wpa_printf(MSG_DEBUG,
  2153. "Assoc conflicting freq found (%d != %d)",
  2154. freq, params.freq.freq);
  2155. if (wpas_p2p_handle_frequency_conflicts(
  2156. wpa_s, params.freq.freq, ssid) < 0) {
  2157. wpas_connect_work_done(wpa_s);
  2158. return;
  2159. }
  2160. }
  2161. }
  2162. #endif /* CONFIG_P2P */
  2163. ret = wpa_drv_associate(wpa_s, &params);
  2164. if (ret < 0) {
  2165. wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
  2166. "failed");
  2167. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
  2168. /*
  2169. * The driver is known to mean what is saying, so we
  2170. * can stop right here; the association will not
  2171. * succeed.
  2172. */
  2173. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  2174. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  2175. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  2176. return;
  2177. }
  2178. /* try to continue anyway; new association will be tried again
  2179. * after timeout */
  2180. assoc_failed = 1;
  2181. }
  2182. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  2183. /* Set the key after the association just in case association
  2184. * cleared the previously configured key. */
  2185. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  2186. /* No need to timeout authentication since there is no key
  2187. * management. */
  2188. wpa_supplicant_cancel_auth_timeout(wpa_s);
  2189. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  2190. #ifdef CONFIG_IBSS_RSN
  2191. } else if (ssid->mode == WPAS_MODE_IBSS &&
  2192. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  2193. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  2194. /*
  2195. * RSN IBSS authentication is per-STA and we can disable the
  2196. * per-BSSID authentication.
  2197. */
  2198. wpa_supplicant_cancel_auth_timeout(wpa_s);
  2199. #endif /* CONFIG_IBSS_RSN */
  2200. } else {
  2201. /* Timeout for IEEE 802.11 authentication and association */
  2202. int timeout = 60;
  2203. if (assoc_failed) {
  2204. /* give IBSS a bit more time */
  2205. timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
  2206. } else if (wpa_s->conf->ap_scan == 1) {
  2207. /* give IBSS a bit more time */
  2208. timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
  2209. }
  2210. wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
  2211. }
  2212. if (wep_keys_set &&
  2213. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC)) {
  2214. /* Set static WEP keys again */
  2215. wpa_set_wep_keys(wpa_s, ssid);
  2216. }
  2217. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
  2218. /*
  2219. * Do not allow EAP session resumption between different
  2220. * network configurations.
  2221. */
  2222. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2223. }
  2224. old_ssid = wpa_s->current_ssid;
  2225. wpa_s->current_ssid = ssid;
  2226. if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set)
  2227. wpa_s->current_bss = bss;
  2228. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  2229. wpa_supplicant_initiate_eapol(wpa_s);
  2230. if (old_ssid != wpa_s->current_ssid)
  2231. wpas_notify_network_changed(wpa_s);
  2232. }
  2233. static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
  2234. const u8 *addr)
  2235. {
  2236. struct wpa_ssid *old_ssid;
  2237. wpas_connect_work_done(wpa_s);
  2238. wpa_clear_keys(wpa_s, addr);
  2239. old_ssid = wpa_s->current_ssid;
  2240. wpa_supplicant_mark_disassoc(wpa_s);
  2241. wpa_sm_set_config(wpa_s->wpa, NULL);
  2242. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2243. if (old_ssid != wpa_s->current_ssid)
  2244. wpas_notify_network_changed(wpa_s);
  2245. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  2246. }
  2247. /**
  2248. * wpa_supplicant_deauthenticate - Deauthenticate the current connection
  2249. * @wpa_s: Pointer to wpa_supplicant data
  2250. * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
  2251. *
  2252. * This function is used to request %wpa_supplicant to deauthenticate from the
  2253. * current AP.
  2254. */
  2255. void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
  2256. int reason_code)
  2257. {
  2258. u8 *addr = NULL;
  2259. union wpa_event_data event;
  2260. int zero_addr = 0;
  2261. wpa_dbg(wpa_s, MSG_DEBUG, "Request to deauthenticate - bssid=" MACSTR
  2262. " pending_bssid=" MACSTR " reason=%d state=%s",
  2263. MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
  2264. reason_code, wpa_supplicant_state_txt(wpa_s->wpa_state));
  2265. if (!is_zero_ether_addr(wpa_s->bssid))
  2266. addr = wpa_s->bssid;
  2267. else if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
  2268. (wpa_s->wpa_state == WPA_AUTHENTICATING ||
  2269. wpa_s->wpa_state == WPA_ASSOCIATING))
  2270. addr = wpa_s->pending_bssid;
  2271. else if (wpa_s->wpa_state == WPA_ASSOCIATING) {
  2272. /*
  2273. * When using driver-based BSS selection, we may not know the
  2274. * BSSID with which we are currently trying to associate. We
  2275. * need to notify the driver of this disconnection even in such
  2276. * a case, so use the all zeros address here.
  2277. */
  2278. addr = wpa_s->bssid;
  2279. zero_addr = 1;
  2280. }
  2281. #ifdef CONFIG_TDLS
  2282. wpa_tdls_teardown_peers(wpa_s->wpa);
  2283. #endif /* CONFIG_TDLS */
  2284. #ifdef CONFIG_MESH
  2285. if (wpa_s->ifmsh) {
  2286. wpa_msg(wpa_s, MSG_INFO, MESH_GROUP_REMOVED "%s",
  2287. wpa_s->ifname);
  2288. wpa_supplicant_leave_mesh(wpa_s);
  2289. }
  2290. #endif /* CONFIG_MESH */
  2291. if (addr) {
  2292. wpa_drv_deauthenticate(wpa_s, addr, reason_code);
  2293. os_memset(&event, 0, sizeof(event));
  2294. event.deauth_info.reason_code = (u16) reason_code;
  2295. event.deauth_info.locally_generated = 1;
  2296. wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
  2297. if (zero_addr)
  2298. addr = NULL;
  2299. }
  2300. wpa_supplicant_clear_connection(wpa_s, addr);
  2301. }
  2302. static void wpa_supplicant_enable_one_network(struct wpa_supplicant *wpa_s,
  2303. struct wpa_ssid *ssid)
  2304. {
  2305. if (!ssid || !ssid->disabled || ssid->disabled == 2)
  2306. return;
  2307. ssid->disabled = 0;
  2308. wpas_clear_temp_disabled(wpa_s, ssid, 1);
  2309. wpas_notify_network_enabled_changed(wpa_s, ssid);
  2310. /*
  2311. * Try to reassociate since there is no current configuration and a new
  2312. * network was made available.
  2313. */
  2314. if (!wpa_s->current_ssid && !wpa_s->disconnected)
  2315. wpa_s->reassociate = 1;
  2316. }
  2317. /**
  2318. * wpa_supplicant_enable_network - Mark a configured network as enabled
  2319. * @wpa_s: wpa_supplicant structure for a network interface
  2320. * @ssid: wpa_ssid structure for a configured network or %NULL
  2321. *
  2322. * Enables the specified network or all networks if no network specified.
  2323. */
  2324. void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
  2325. struct wpa_ssid *ssid)
  2326. {
  2327. if (ssid == NULL) {
  2328. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  2329. wpa_supplicant_enable_one_network(wpa_s, ssid);
  2330. } else
  2331. wpa_supplicant_enable_one_network(wpa_s, ssid);
  2332. if (wpa_s->reassociate && !wpa_s->disconnected &&
  2333. (!wpa_s->current_ssid ||
  2334. wpa_s->wpa_state == WPA_DISCONNECTED ||
  2335. wpa_s->wpa_state == WPA_SCANNING)) {
  2336. if (wpa_s->sched_scanning) {
  2337. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to add "
  2338. "new network to scan filters");
  2339. wpa_supplicant_cancel_sched_scan(wpa_s);
  2340. }
  2341. if (wpa_supplicant_fast_associate(wpa_s) != 1) {
  2342. wpa_s->scan_req = NORMAL_SCAN_REQ;
  2343. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2344. }
  2345. }
  2346. }
  2347. /**
  2348. * wpa_supplicant_disable_network - Mark a configured network as disabled
  2349. * @wpa_s: wpa_supplicant structure for a network interface
  2350. * @ssid: wpa_ssid structure for a configured network or %NULL
  2351. *
  2352. * Disables the specified network or all networks if no network specified.
  2353. */
  2354. void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
  2355. struct wpa_ssid *ssid)
  2356. {
  2357. struct wpa_ssid *other_ssid;
  2358. int was_disabled;
  2359. if (ssid == NULL) {
  2360. if (wpa_s->sched_scanning)
  2361. wpa_supplicant_cancel_sched_scan(wpa_s);
  2362. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  2363. other_ssid = other_ssid->next) {
  2364. was_disabled = other_ssid->disabled;
  2365. if (was_disabled == 2)
  2366. continue; /* do not change persistent P2P group
  2367. * data */
  2368. other_ssid->disabled = 1;
  2369. if (was_disabled != other_ssid->disabled)
  2370. wpas_notify_network_enabled_changed(
  2371. wpa_s, other_ssid);
  2372. }
  2373. if (wpa_s->current_ssid)
  2374. wpa_supplicant_deauthenticate(
  2375. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2376. } else if (ssid->disabled != 2) {
  2377. if (ssid == wpa_s->current_ssid)
  2378. wpa_supplicant_deauthenticate(
  2379. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2380. was_disabled = ssid->disabled;
  2381. ssid->disabled = 1;
  2382. if (was_disabled != ssid->disabled) {
  2383. wpas_notify_network_enabled_changed(wpa_s, ssid);
  2384. if (wpa_s->sched_scanning) {
  2385. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan "
  2386. "to remove network from filters");
  2387. wpa_supplicant_cancel_sched_scan(wpa_s);
  2388. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2389. }
  2390. }
  2391. }
  2392. }
  2393. /**
  2394. * wpa_supplicant_select_network - Attempt association with a network
  2395. * @wpa_s: wpa_supplicant structure for a network interface
  2396. * @ssid: wpa_ssid structure for a configured network or %NULL for any network
  2397. */
  2398. void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
  2399. struct wpa_ssid *ssid)
  2400. {
  2401. struct wpa_ssid *other_ssid;
  2402. int disconnected = 0;
  2403. if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
  2404. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2405. wpa_s->own_disconnect_req = 1;
  2406. wpa_supplicant_deauthenticate(
  2407. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2408. disconnected = 1;
  2409. }
  2410. if (ssid)
  2411. wpas_clear_temp_disabled(wpa_s, ssid, 1);
  2412. /*
  2413. * Mark all other networks disabled or mark all networks enabled if no
  2414. * network specified.
  2415. */
  2416. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  2417. other_ssid = other_ssid->next) {
  2418. int was_disabled = other_ssid->disabled;
  2419. if (was_disabled == 2)
  2420. continue; /* do not change persistent P2P group data */
  2421. other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
  2422. if (was_disabled && !other_ssid->disabled)
  2423. wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
  2424. if (was_disabled != other_ssid->disabled)
  2425. wpas_notify_network_enabled_changed(wpa_s, other_ssid);
  2426. }
  2427. if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid &&
  2428. wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  2429. /* We are already associated with the selected network */
  2430. wpa_printf(MSG_DEBUG, "Already associated with the "
  2431. "selected network - do nothing");
  2432. return;
  2433. }
  2434. if (ssid) {
  2435. wpa_s->current_ssid = ssid;
  2436. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2437. wpa_s->connect_without_scan =
  2438. (ssid->mode == WPAS_MODE_MESH) ? ssid : NULL;
  2439. /*
  2440. * Don't optimize next scan freqs since a new ESS has been
  2441. * selected.
  2442. */
  2443. os_free(wpa_s->next_scan_freqs);
  2444. wpa_s->next_scan_freqs = NULL;
  2445. } else {
  2446. wpa_s->connect_without_scan = NULL;
  2447. }
  2448. wpa_s->disconnected = 0;
  2449. wpa_s->reassociate = 1;
  2450. if (wpa_s->connect_without_scan ||
  2451. wpa_supplicant_fast_associate(wpa_s) != 1) {
  2452. wpa_s->scan_req = NORMAL_SCAN_REQ;
  2453. wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
  2454. }
  2455. if (ssid)
  2456. wpas_notify_network_selected(wpa_s, ssid);
  2457. }
  2458. /**
  2459. * wpas_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
  2460. * @wpa_s: wpa_supplicant structure for a network interface
  2461. * @pkcs11_engine_path: PKCS #11 engine path or NULL
  2462. * @pkcs11_module_path: PKCS #11 module path or NULL
  2463. * Returns: 0 on success; -1 on failure
  2464. *
  2465. * Sets the PKCS #11 engine and module path. Both have to be NULL or a valid
  2466. * path. If resetting the EAPOL state machine with the new PKCS #11 engine and
  2467. * module path fails the paths will be reset to the default value (NULL).
  2468. */
  2469. int wpas_set_pkcs11_engine_and_module_path(struct wpa_supplicant *wpa_s,
  2470. const char *pkcs11_engine_path,
  2471. const char *pkcs11_module_path)
  2472. {
  2473. char *pkcs11_engine_path_copy = NULL;
  2474. char *pkcs11_module_path_copy = NULL;
  2475. if (pkcs11_engine_path != NULL) {
  2476. pkcs11_engine_path_copy = os_strdup(pkcs11_engine_path);
  2477. if (pkcs11_engine_path_copy == NULL)
  2478. return -1;
  2479. }
  2480. if (pkcs11_module_path != NULL) {
  2481. pkcs11_module_path_copy = os_strdup(pkcs11_module_path);
  2482. if (pkcs11_module_path_copy == NULL) {
  2483. os_free(pkcs11_engine_path_copy);
  2484. return -1;
  2485. }
  2486. }
  2487. os_free(wpa_s->conf->pkcs11_engine_path);
  2488. os_free(wpa_s->conf->pkcs11_module_path);
  2489. wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path_copy;
  2490. wpa_s->conf->pkcs11_module_path = pkcs11_module_path_copy;
  2491. wpa_sm_set_eapol(wpa_s->wpa, NULL);
  2492. eapol_sm_deinit(wpa_s->eapol);
  2493. wpa_s->eapol = NULL;
  2494. if (wpa_supplicant_init_eapol(wpa_s)) {
  2495. /* Error -> Reset paths to the default value (NULL) once. */
  2496. if (pkcs11_engine_path != NULL && pkcs11_module_path != NULL)
  2497. wpas_set_pkcs11_engine_and_module_path(wpa_s, NULL,
  2498. NULL);
  2499. return -1;
  2500. }
  2501. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  2502. return 0;
  2503. }
  2504. /**
  2505. * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
  2506. * @wpa_s: wpa_supplicant structure for a network interface
  2507. * @ap_scan: AP scan mode
  2508. * Returns: 0 if succeed or -1 if ap_scan has an invalid value
  2509. *
  2510. */
  2511. int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
  2512. {
  2513. int old_ap_scan;
  2514. if (ap_scan < 0 || ap_scan > 2)
  2515. return -1;
  2516. if (ap_scan == 2 && os_strcmp(wpa_s->driver->name, "nl80211") == 0) {
  2517. wpa_printf(MSG_INFO,
  2518. "Note: nl80211 driver interface is not designed to be used with ap_scan=2; this can result in connection failures");
  2519. }
  2520. #ifdef ANDROID
  2521. if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
  2522. wpa_s->wpa_state >= WPA_ASSOCIATING &&
  2523. wpa_s->wpa_state < WPA_COMPLETED) {
  2524. wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
  2525. "associating", wpa_s->conf->ap_scan, ap_scan);
  2526. return 0;
  2527. }
  2528. #endif /* ANDROID */
  2529. old_ap_scan = wpa_s->conf->ap_scan;
  2530. wpa_s->conf->ap_scan = ap_scan;
  2531. if (old_ap_scan != wpa_s->conf->ap_scan)
  2532. wpas_notify_ap_scan_changed(wpa_s);
  2533. return 0;
  2534. }
  2535. /**
  2536. * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
  2537. * @wpa_s: wpa_supplicant structure for a network interface
  2538. * @expire_age: Expiration age in seconds
  2539. * Returns: 0 if succeed or -1 if expire_age has an invalid value
  2540. *
  2541. */
  2542. int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
  2543. unsigned int bss_expire_age)
  2544. {
  2545. if (bss_expire_age < 10) {
  2546. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
  2547. bss_expire_age);
  2548. return -1;
  2549. }
  2550. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
  2551. bss_expire_age);
  2552. wpa_s->conf->bss_expiration_age = bss_expire_age;
  2553. return 0;
  2554. }
  2555. /**
  2556. * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
  2557. * @wpa_s: wpa_supplicant structure for a network interface
  2558. * @expire_count: number of scans after which an unseen BSS is reclaimed
  2559. * Returns: 0 if succeed or -1 if expire_count has an invalid value
  2560. *
  2561. */
  2562. int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
  2563. unsigned int bss_expire_count)
  2564. {
  2565. if (bss_expire_count < 1) {
  2566. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
  2567. bss_expire_count);
  2568. return -1;
  2569. }
  2570. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
  2571. bss_expire_count);
  2572. wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
  2573. return 0;
  2574. }
  2575. /**
  2576. * wpa_supplicant_set_scan_interval - Set scan interval
  2577. * @wpa_s: wpa_supplicant structure for a network interface
  2578. * @scan_interval: scan interval in seconds
  2579. * Returns: 0 if succeed or -1 if scan_interval has an invalid value
  2580. *
  2581. */
  2582. int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
  2583. int scan_interval)
  2584. {
  2585. if (scan_interval < 0) {
  2586. wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
  2587. scan_interval);
  2588. return -1;
  2589. }
  2590. wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
  2591. scan_interval);
  2592. wpa_supplicant_update_scan_int(wpa_s, scan_interval);
  2593. return 0;
  2594. }
  2595. /**
  2596. * wpa_supplicant_set_debug_params - Set global debug params
  2597. * @global: wpa_global structure
  2598. * @debug_level: debug level
  2599. * @debug_timestamp: determines if show timestamp in debug data
  2600. * @debug_show_keys: determines if show keys in debug data
  2601. * Returns: 0 if succeed or -1 if debug_level has wrong value
  2602. */
  2603. int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
  2604. int debug_timestamp, int debug_show_keys)
  2605. {
  2606. int old_level, old_timestamp, old_show_keys;
  2607. /* check for allowed debuglevels */
  2608. if (debug_level != MSG_EXCESSIVE &&
  2609. debug_level != MSG_MSGDUMP &&
  2610. debug_level != MSG_DEBUG &&
  2611. debug_level != MSG_INFO &&
  2612. debug_level != MSG_WARNING &&
  2613. debug_level != MSG_ERROR)
  2614. return -1;
  2615. old_level = wpa_debug_level;
  2616. old_timestamp = wpa_debug_timestamp;
  2617. old_show_keys = wpa_debug_show_keys;
  2618. wpa_debug_level = debug_level;
  2619. wpa_debug_timestamp = debug_timestamp ? 1 : 0;
  2620. wpa_debug_show_keys = debug_show_keys ? 1 : 0;
  2621. if (wpa_debug_level != old_level)
  2622. wpas_notify_debug_level_changed(global);
  2623. if (wpa_debug_timestamp != old_timestamp)
  2624. wpas_notify_debug_timestamp_changed(global);
  2625. if (wpa_debug_show_keys != old_show_keys)
  2626. wpas_notify_debug_show_keys_changed(global);
  2627. return 0;
  2628. }
  2629. /**
  2630. * wpa_supplicant_get_ssid - Get a pointer to the current network structure
  2631. * @wpa_s: Pointer to wpa_supplicant data
  2632. * Returns: A pointer to the current network structure or %NULL on failure
  2633. */
  2634. struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
  2635. {
  2636. struct wpa_ssid *entry;
  2637. u8 ssid[SSID_MAX_LEN];
  2638. int res;
  2639. size_t ssid_len;
  2640. u8 bssid[ETH_ALEN];
  2641. int wired;
  2642. res = wpa_drv_get_ssid(wpa_s, ssid);
  2643. if (res < 0) {
  2644. wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
  2645. "driver");
  2646. return NULL;
  2647. }
  2648. ssid_len = res;
  2649. if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
  2650. wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
  2651. "driver");
  2652. return NULL;
  2653. }
  2654. wired = wpa_s->conf->ap_scan == 0 &&
  2655. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
  2656. entry = wpa_s->conf->ssid;
  2657. while (entry) {
  2658. if (!wpas_network_disabled(wpa_s, entry) &&
  2659. ((ssid_len == entry->ssid_len &&
  2660. os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
  2661. (!entry->bssid_set ||
  2662. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  2663. return entry;
  2664. #ifdef CONFIG_WPS
  2665. if (!wpas_network_disabled(wpa_s, entry) &&
  2666. (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
  2667. (entry->ssid == NULL || entry->ssid_len == 0) &&
  2668. (!entry->bssid_set ||
  2669. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  2670. return entry;
  2671. #endif /* CONFIG_WPS */
  2672. if (!wpas_network_disabled(wpa_s, entry) && entry->bssid_set &&
  2673. entry->ssid_len == 0 &&
  2674. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
  2675. return entry;
  2676. entry = entry->next;
  2677. }
  2678. return NULL;
  2679. }
  2680. static int select_driver(struct wpa_supplicant *wpa_s, int i)
  2681. {
  2682. struct wpa_global *global = wpa_s->global;
  2683. if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
  2684. global->drv_priv[i] = wpa_drivers[i]->global_init();
  2685. if (global->drv_priv[i] == NULL) {
  2686. wpa_printf(MSG_ERROR, "Failed to initialize driver "
  2687. "'%s'", wpa_drivers[i]->name);
  2688. return -1;
  2689. }
  2690. }
  2691. wpa_s->driver = wpa_drivers[i];
  2692. wpa_s->global_drv_priv = global->drv_priv[i];
  2693. return 0;
  2694. }
  2695. static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
  2696. const char *name)
  2697. {
  2698. int i;
  2699. size_t len;
  2700. const char *pos, *driver = name;
  2701. if (wpa_s == NULL)
  2702. return -1;
  2703. if (wpa_drivers[0] == NULL) {
  2704. wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
  2705. "wpa_supplicant");
  2706. return -1;
  2707. }
  2708. if (name == NULL) {
  2709. /* default to first driver in the list */
  2710. return select_driver(wpa_s, 0);
  2711. }
  2712. do {
  2713. pos = os_strchr(driver, ',');
  2714. if (pos)
  2715. len = pos - driver;
  2716. else
  2717. len = os_strlen(driver);
  2718. for (i = 0; wpa_drivers[i]; i++) {
  2719. if (os_strlen(wpa_drivers[i]->name) == len &&
  2720. os_strncmp(driver, wpa_drivers[i]->name, len) ==
  2721. 0) {
  2722. /* First driver that succeeds wins */
  2723. if (select_driver(wpa_s, i) == 0)
  2724. return 0;
  2725. }
  2726. }
  2727. driver = pos + 1;
  2728. } while (pos);
  2729. wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
  2730. return -1;
  2731. }
  2732. /**
  2733. * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
  2734. * @ctx: Context pointer (wpa_s); this is the ctx variable registered
  2735. * with struct wpa_driver_ops::init()
  2736. * @src_addr: Source address of the EAPOL frame
  2737. * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
  2738. * @len: Length of the EAPOL data
  2739. *
  2740. * This function is called for each received EAPOL frame. Most driver
  2741. * interfaces rely on more generic OS mechanism for receiving frames through
  2742. * l2_packet, but if such a mechanism is not available, the driver wrapper may
  2743. * take care of received EAPOL frames and deliver them to the core supplicant
  2744. * code by calling this function.
  2745. */
  2746. void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
  2747. const u8 *buf, size_t len)
  2748. {
  2749. struct wpa_supplicant *wpa_s = ctx;
  2750. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
  2751. wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
  2752. #ifdef CONFIG_PEERKEY
  2753. if (wpa_s->wpa_state > WPA_ASSOCIATED && wpa_s->current_ssid &&
  2754. wpa_s->current_ssid->peerkey &&
  2755. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  2756. wpa_sm_rx_eapol_peerkey(wpa_s->wpa, src_addr, buf, len) == 1) {
  2757. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: Processed PeerKey EAPOL-Key");
  2758. return;
  2759. }
  2760. #endif /* CONFIG_PEERKEY */
  2761. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  2762. (wpa_s->last_eapol_matches_bssid &&
  2763. #ifdef CONFIG_AP
  2764. !wpa_s->ap_iface &&
  2765. #endif /* CONFIG_AP */
  2766. os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) != 0)) {
  2767. /*
  2768. * There is possible race condition between receiving the
  2769. * association event and the EAPOL frame since they are coming
  2770. * through different paths from the driver. In order to avoid
  2771. * issues in trying to process the EAPOL frame before receiving
  2772. * association information, lets queue it for processing until
  2773. * the association event is received. This may also be needed in
  2774. * driver-based roaming case, so also use src_addr != BSSID as a
  2775. * trigger if we have previously confirmed that the
  2776. * Authenticator uses BSSID as the src_addr (which is not the
  2777. * case with wired IEEE 802.1X).
  2778. */
  2779. wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
  2780. "of received EAPOL frame (state=%s bssid=" MACSTR ")",
  2781. wpa_supplicant_state_txt(wpa_s->wpa_state),
  2782. MAC2STR(wpa_s->bssid));
  2783. wpabuf_free(wpa_s->pending_eapol_rx);
  2784. wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
  2785. if (wpa_s->pending_eapol_rx) {
  2786. os_get_reltime(&wpa_s->pending_eapol_rx_time);
  2787. os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
  2788. ETH_ALEN);
  2789. }
  2790. return;
  2791. }
  2792. wpa_s->last_eapol_matches_bssid =
  2793. os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) == 0;
  2794. #ifdef CONFIG_AP
  2795. if (wpa_s->ap_iface) {
  2796. wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
  2797. return;
  2798. }
  2799. #endif /* CONFIG_AP */
  2800. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
  2801. wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
  2802. "no key management is configured");
  2803. return;
  2804. }
  2805. if (wpa_s->eapol_received == 0 &&
  2806. (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
  2807. !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
  2808. wpa_s->wpa_state != WPA_COMPLETED) &&
  2809. (wpa_s->current_ssid == NULL ||
  2810. wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
  2811. /* Timeout for completing IEEE 802.1X and WPA authentication */
  2812. int timeout = 10;
  2813. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  2814. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
  2815. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
  2816. /* Use longer timeout for IEEE 802.1X/EAP */
  2817. timeout = 70;
  2818. }
  2819. #ifdef CONFIG_WPS
  2820. if (wpa_s->current_ssid && wpa_s->current_bss &&
  2821. (wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
  2822. eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
  2823. /*
  2824. * Use shorter timeout if going through WPS AP iteration
  2825. * for PIN config method with an AP that does not
  2826. * advertise Selected Registrar.
  2827. */
  2828. struct wpabuf *wps_ie;
  2829. wps_ie = wpa_bss_get_vendor_ie_multi(
  2830. wpa_s->current_bss, WPS_IE_VENDOR_TYPE);
  2831. if (wps_ie &&
  2832. !wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1))
  2833. timeout = 10;
  2834. wpabuf_free(wps_ie);
  2835. }
  2836. #endif /* CONFIG_WPS */
  2837. wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
  2838. }
  2839. wpa_s->eapol_received++;
  2840. if (wpa_s->countermeasures) {
  2841. wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
  2842. "EAPOL packet");
  2843. return;
  2844. }
  2845. #ifdef CONFIG_IBSS_RSN
  2846. if (wpa_s->current_ssid &&
  2847. wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
  2848. ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
  2849. return;
  2850. }
  2851. #endif /* CONFIG_IBSS_RSN */
  2852. /* Source address of the incoming EAPOL frame could be compared to the
  2853. * current BSSID. However, it is possible that a centralized
  2854. * Authenticator could be using another MAC address than the BSSID of
  2855. * an AP, so just allow any address to be used for now. The replies are
  2856. * still sent to the current BSSID (if available), though. */
  2857. os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
  2858. if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
  2859. eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
  2860. return;
  2861. wpa_drv_poll(wpa_s);
  2862. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
  2863. wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
  2864. else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  2865. /*
  2866. * Set portValid = TRUE here since we are going to skip 4-way
  2867. * handshake processing which would normally set portValid. We
  2868. * need this to allow the EAPOL state machines to be completed
  2869. * without going through EAPOL-Key handshake.
  2870. */
  2871. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  2872. }
  2873. }
  2874. int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
  2875. {
  2876. if ((!wpa_s->p2p_mgmt ||
  2877. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
  2878. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
  2879. l2_packet_deinit(wpa_s->l2);
  2880. wpa_s->l2 = l2_packet_init(wpa_s->ifname,
  2881. wpa_drv_get_mac_addr(wpa_s),
  2882. ETH_P_EAPOL,
  2883. wpa_supplicant_rx_eapol, wpa_s, 0);
  2884. if (wpa_s->l2 == NULL)
  2885. return -1;
  2886. } else {
  2887. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  2888. if (addr)
  2889. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  2890. }
  2891. if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
  2892. wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
  2893. return -1;
  2894. }
  2895. wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
  2896. return 0;
  2897. }
  2898. static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
  2899. const u8 *buf, size_t len)
  2900. {
  2901. struct wpa_supplicant *wpa_s = ctx;
  2902. const struct l2_ethhdr *eth;
  2903. if (len < sizeof(*eth))
  2904. return;
  2905. eth = (const struct l2_ethhdr *) buf;
  2906. if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
  2907. !(eth->h_dest[0] & 0x01)) {
  2908. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
  2909. " (bridge - not for this interface - ignore)",
  2910. MAC2STR(src_addr), MAC2STR(eth->h_dest));
  2911. return;
  2912. }
  2913. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
  2914. " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
  2915. wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
  2916. len - sizeof(*eth));
  2917. }
  2918. /**
  2919. * wpa_supplicant_driver_init - Initialize driver interface parameters
  2920. * @wpa_s: Pointer to wpa_supplicant data
  2921. * Returns: 0 on success, -1 on failure
  2922. *
  2923. * This function is called to initialize driver interface parameters.
  2924. * wpa_drv_init() must have been called before this function to initialize the
  2925. * driver interface.
  2926. */
  2927. int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
  2928. {
  2929. static int interface_count = 0;
  2930. if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
  2931. return -1;
  2932. wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
  2933. MAC2STR(wpa_s->own_addr));
  2934. os_memcpy(wpa_s->perm_addr, wpa_s->own_addr, ETH_ALEN);
  2935. wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
  2936. if (wpa_s->bridge_ifname[0]) {
  2937. wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
  2938. "interface '%s'", wpa_s->bridge_ifname);
  2939. wpa_s->l2_br = l2_packet_init_bridge(
  2940. wpa_s->bridge_ifname, wpa_s->ifname, wpa_s->own_addr,
  2941. ETH_P_EAPOL, wpa_supplicant_rx_eapol_bridge, wpa_s, 1);
  2942. if (wpa_s->l2_br == NULL) {
  2943. wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
  2944. "connection for the bridge interface '%s'",
  2945. wpa_s->bridge_ifname);
  2946. return -1;
  2947. }
  2948. }
  2949. if (wpa_s->conf->ap_scan == 2 &&
  2950. os_strcmp(wpa_s->driver->name, "nl80211") == 0) {
  2951. wpa_printf(MSG_INFO,
  2952. "Note: nl80211 driver interface is not designed to be used with ap_scan=2; this can result in connection failures");
  2953. }
  2954. wpa_clear_keys(wpa_s, NULL);
  2955. /* Make sure that TKIP countermeasures are not left enabled (could
  2956. * happen if wpa_supplicant is killed during countermeasures. */
  2957. wpa_drv_set_countermeasures(wpa_s, 0);
  2958. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
  2959. wpa_drv_flush_pmkid(wpa_s);
  2960. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  2961. wpa_s->prev_scan_wildcard = 0;
  2962. if (wpa_supplicant_enabled_networks(wpa_s)) {
  2963. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  2964. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  2965. interface_count = 0;
  2966. }
  2967. #ifndef ANDROID
  2968. if (!wpa_s->p2p_mgmt &&
  2969. wpa_supplicant_delayed_sched_scan(wpa_s,
  2970. interface_count % 3,
  2971. 100000))
  2972. wpa_supplicant_req_scan(wpa_s, interface_count % 3,
  2973. 100000);
  2974. #endif /* ANDROID */
  2975. interface_count++;
  2976. } else
  2977. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  2978. return 0;
  2979. }
  2980. static int wpa_supplicant_daemon(const char *pid_file)
  2981. {
  2982. wpa_printf(MSG_DEBUG, "Daemonize..");
  2983. return os_daemonize(pid_file);
  2984. }
  2985. static struct wpa_supplicant *
  2986. wpa_supplicant_alloc(struct wpa_supplicant *parent)
  2987. {
  2988. struct wpa_supplicant *wpa_s;
  2989. wpa_s = os_zalloc(sizeof(*wpa_s));
  2990. if (wpa_s == NULL)
  2991. return NULL;
  2992. wpa_s->scan_req = INITIAL_SCAN_REQ;
  2993. wpa_s->scan_interval = 5;
  2994. wpa_s->new_connection = 1;
  2995. wpa_s->parent = parent ? parent : wpa_s;
  2996. wpa_s->sched_scanning = 0;
  2997. return wpa_s;
  2998. }
  2999. #ifdef CONFIG_HT_OVERRIDES
  3000. static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
  3001. struct ieee80211_ht_capabilities *htcaps,
  3002. struct ieee80211_ht_capabilities *htcaps_mask,
  3003. const char *ht_mcs)
  3004. {
  3005. /* parse ht_mcs into hex array */
  3006. int i;
  3007. const char *tmp = ht_mcs;
  3008. char *end = NULL;
  3009. /* If ht_mcs is null, do not set anything */
  3010. if (!ht_mcs)
  3011. return 0;
  3012. /* This is what we are setting in the kernel */
  3013. os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
  3014. wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
  3015. for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
  3016. errno = 0;
  3017. long v = strtol(tmp, &end, 16);
  3018. if (errno == 0) {
  3019. wpa_msg(wpa_s, MSG_DEBUG,
  3020. "htcap value[%i]: %ld end: %p tmp: %p",
  3021. i, v, end, tmp);
  3022. if (end == tmp)
  3023. break;
  3024. htcaps->supported_mcs_set[i] = v;
  3025. tmp = end;
  3026. } else {
  3027. wpa_msg(wpa_s, MSG_ERROR,
  3028. "Failed to parse ht-mcs: %s, error: %s\n",
  3029. ht_mcs, strerror(errno));
  3030. return -1;
  3031. }
  3032. }
  3033. /*
  3034. * If we were able to parse any values, then set mask for the MCS set.
  3035. */
  3036. if (i) {
  3037. os_memset(&htcaps_mask->supported_mcs_set, 0xff,
  3038. IEEE80211_HT_MCS_MASK_LEN - 1);
  3039. /* skip the 3 reserved bits */
  3040. htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
  3041. 0x1f;
  3042. }
  3043. return 0;
  3044. }
  3045. static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
  3046. struct ieee80211_ht_capabilities *htcaps,
  3047. struct ieee80211_ht_capabilities *htcaps_mask,
  3048. int disabled)
  3049. {
  3050. le16 msk;
  3051. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
  3052. if (disabled == -1)
  3053. return 0;
  3054. msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
  3055. htcaps_mask->ht_capabilities_info |= msk;
  3056. if (disabled)
  3057. htcaps->ht_capabilities_info &= msk;
  3058. else
  3059. htcaps->ht_capabilities_info |= msk;
  3060. return 0;
  3061. }
  3062. static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
  3063. struct ieee80211_ht_capabilities *htcaps,
  3064. struct ieee80211_ht_capabilities *htcaps_mask,
  3065. int factor)
  3066. {
  3067. wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
  3068. if (factor == -1)
  3069. return 0;
  3070. if (factor < 0 || factor > 3) {
  3071. wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
  3072. "Must be 0-3 or -1", factor);
  3073. return -EINVAL;
  3074. }
  3075. htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
  3076. htcaps->a_mpdu_params &= ~0x3;
  3077. htcaps->a_mpdu_params |= factor & 0x3;
  3078. return 0;
  3079. }
  3080. static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
  3081. struct ieee80211_ht_capabilities *htcaps,
  3082. struct ieee80211_ht_capabilities *htcaps_mask,
  3083. int density)
  3084. {
  3085. wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
  3086. if (density == -1)
  3087. return 0;
  3088. if (density < 0 || density > 7) {
  3089. wpa_msg(wpa_s, MSG_ERROR,
  3090. "ampdu_density: %d out of range. Must be 0-7 or -1.",
  3091. density);
  3092. return -EINVAL;
  3093. }
  3094. htcaps_mask->a_mpdu_params |= 0x1C;
  3095. htcaps->a_mpdu_params &= ~(0x1C);
  3096. htcaps->a_mpdu_params |= (density << 2) & 0x1C;
  3097. return 0;
  3098. }
  3099. static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
  3100. struct ieee80211_ht_capabilities *htcaps,
  3101. struct ieee80211_ht_capabilities *htcaps_mask,
  3102. int disabled)
  3103. {
  3104. /* Masking these out disables HT40 */
  3105. le16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
  3106. HT_CAP_INFO_SHORT_GI40MHZ);
  3107. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
  3108. if (disabled)
  3109. htcaps->ht_capabilities_info &= ~msk;
  3110. else
  3111. htcaps->ht_capabilities_info |= msk;
  3112. htcaps_mask->ht_capabilities_info |= msk;
  3113. return 0;
  3114. }
  3115. static int wpa_set_disable_sgi(struct wpa_supplicant *wpa_s,
  3116. struct ieee80211_ht_capabilities *htcaps,
  3117. struct ieee80211_ht_capabilities *htcaps_mask,
  3118. int disabled)
  3119. {
  3120. /* Masking these out disables SGI */
  3121. le16 msk = host_to_le16(HT_CAP_INFO_SHORT_GI20MHZ |
  3122. HT_CAP_INFO_SHORT_GI40MHZ);
  3123. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_sgi: %d", disabled);
  3124. if (disabled)
  3125. htcaps->ht_capabilities_info &= ~msk;
  3126. else
  3127. htcaps->ht_capabilities_info |= msk;
  3128. htcaps_mask->ht_capabilities_info |= msk;
  3129. return 0;
  3130. }
  3131. static int wpa_set_disable_ldpc(struct wpa_supplicant *wpa_s,
  3132. struct ieee80211_ht_capabilities *htcaps,
  3133. struct ieee80211_ht_capabilities *htcaps_mask,
  3134. int disabled)
  3135. {
  3136. /* Masking these out disables LDPC */
  3137. le16 msk = host_to_le16(HT_CAP_INFO_LDPC_CODING_CAP);
  3138. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ldpc: %d", disabled);
  3139. if (disabled)
  3140. htcaps->ht_capabilities_info &= ~msk;
  3141. else
  3142. htcaps->ht_capabilities_info |= msk;
  3143. htcaps_mask->ht_capabilities_info |= msk;
  3144. return 0;
  3145. }
  3146. void wpa_supplicant_apply_ht_overrides(
  3147. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  3148. struct wpa_driver_associate_params *params)
  3149. {
  3150. struct ieee80211_ht_capabilities *htcaps;
  3151. struct ieee80211_ht_capabilities *htcaps_mask;
  3152. if (!ssid)
  3153. return;
  3154. params->disable_ht = ssid->disable_ht;
  3155. if (!params->htcaps || !params->htcaps_mask)
  3156. return;
  3157. htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
  3158. htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
  3159. wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
  3160. wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
  3161. ssid->disable_max_amsdu);
  3162. wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
  3163. wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
  3164. wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
  3165. wpa_set_disable_sgi(wpa_s, htcaps, htcaps_mask, ssid->disable_sgi);
  3166. wpa_set_disable_ldpc(wpa_s, htcaps, htcaps_mask, ssid->disable_ldpc);
  3167. if (ssid->ht40_intolerant) {
  3168. le16 bit = host_to_le16(HT_CAP_INFO_40MHZ_INTOLERANT);
  3169. htcaps->ht_capabilities_info |= bit;
  3170. htcaps_mask->ht_capabilities_info |= bit;
  3171. }
  3172. }
  3173. #endif /* CONFIG_HT_OVERRIDES */
  3174. #ifdef CONFIG_VHT_OVERRIDES
  3175. void wpa_supplicant_apply_vht_overrides(
  3176. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  3177. struct wpa_driver_associate_params *params)
  3178. {
  3179. struct ieee80211_vht_capabilities *vhtcaps;
  3180. struct ieee80211_vht_capabilities *vhtcaps_mask;
  3181. if (!ssid)
  3182. return;
  3183. params->disable_vht = ssid->disable_vht;
  3184. vhtcaps = (void *) params->vhtcaps;
  3185. vhtcaps_mask = (void *) params->vhtcaps_mask;
  3186. if (!vhtcaps || !vhtcaps_mask)
  3187. return;
  3188. vhtcaps->vht_capabilities_info = ssid->vht_capa;
  3189. vhtcaps_mask->vht_capabilities_info = ssid->vht_capa_mask;
  3190. #ifdef CONFIG_HT_OVERRIDES
  3191. /* if max ampdu is <= 3, we have to make the HT cap the same */
  3192. if (ssid->vht_capa_mask & VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) {
  3193. int max_ampdu;
  3194. max_ampdu = (ssid->vht_capa &
  3195. VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) >>
  3196. VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX_SHIFT;
  3197. max_ampdu = max_ampdu < 3 ? max_ampdu : 3;
  3198. wpa_set_ampdu_factor(wpa_s,
  3199. (void *) params->htcaps,
  3200. (void *) params->htcaps_mask,
  3201. max_ampdu);
  3202. }
  3203. #endif /* CONFIG_HT_OVERRIDES */
  3204. #define OVERRIDE_MCS(i) \
  3205. if (ssid->vht_tx_mcs_nss_ ##i >= 0) { \
  3206. vhtcaps_mask->vht_supported_mcs_set.tx_map |= \
  3207. 3 << 2 * (i - 1); \
  3208. vhtcaps->vht_supported_mcs_set.tx_map |= \
  3209. ssid->vht_tx_mcs_nss_ ##i << 2 * (i - 1); \
  3210. } \
  3211. if (ssid->vht_rx_mcs_nss_ ##i >= 0) { \
  3212. vhtcaps_mask->vht_supported_mcs_set.rx_map |= \
  3213. 3 << 2 * (i - 1); \
  3214. vhtcaps->vht_supported_mcs_set.rx_map |= \
  3215. ssid->vht_rx_mcs_nss_ ##i << 2 * (i - 1); \
  3216. }
  3217. OVERRIDE_MCS(1);
  3218. OVERRIDE_MCS(2);
  3219. OVERRIDE_MCS(3);
  3220. OVERRIDE_MCS(4);
  3221. OVERRIDE_MCS(5);
  3222. OVERRIDE_MCS(6);
  3223. OVERRIDE_MCS(7);
  3224. OVERRIDE_MCS(8);
  3225. }
  3226. #endif /* CONFIG_VHT_OVERRIDES */
  3227. static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
  3228. {
  3229. #ifdef PCSC_FUNCS
  3230. size_t len;
  3231. if (!wpa_s->conf->pcsc_reader)
  3232. return 0;
  3233. wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
  3234. if (!wpa_s->scard)
  3235. return 1;
  3236. if (wpa_s->conf->pcsc_pin &&
  3237. scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
  3238. scard_deinit(wpa_s->scard);
  3239. wpa_s->scard = NULL;
  3240. wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
  3241. return -1;
  3242. }
  3243. len = sizeof(wpa_s->imsi) - 1;
  3244. if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
  3245. scard_deinit(wpa_s->scard);
  3246. wpa_s->scard = NULL;
  3247. wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
  3248. return -1;
  3249. }
  3250. wpa_s->imsi[len] = '\0';
  3251. wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
  3252. wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
  3253. wpa_s->imsi, wpa_s->mnc_len);
  3254. wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
  3255. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  3256. #endif /* PCSC_FUNCS */
  3257. return 0;
  3258. }
  3259. int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
  3260. {
  3261. char *val, *pos;
  3262. ext_password_deinit(wpa_s->ext_pw);
  3263. wpa_s->ext_pw = NULL;
  3264. eapol_sm_set_ext_pw_ctx(wpa_s->eapol, NULL);
  3265. if (!wpa_s->conf->ext_password_backend)
  3266. return 0;
  3267. val = os_strdup(wpa_s->conf->ext_password_backend);
  3268. if (val == NULL)
  3269. return -1;
  3270. pos = os_strchr(val, ':');
  3271. if (pos)
  3272. *pos++ = '\0';
  3273. wpa_printf(MSG_DEBUG, "EXT PW: Initialize backend '%s'", val);
  3274. wpa_s->ext_pw = ext_password_init(val, pos);
  3275. os_free(val);
  3276. if (wpa_s->ext_pw == NULL) {
  3277. wpa_printf(MSG_DEBUG, "EXT PW: Failed to initialize backend");
  3278. return -1;
  3279. }
  3280. eapol_sm_set_ext_pw_ctx(wpa_s->eapol, wpa_s->ext_pw);
  3281. return 0;
  3282. }
  3283. #ifdef CONFIG_FST
  3284. static const u8 * wpas_fst_get_bssid_cb(void *ctx)
  3285. {
  3286. struct wpa_supplicant *wpa_s = ctx;
  3287. return (is_zero_ether_addr(wpa_s->bssid) ||
  3288. wpa_s->wpa_state != WPA_COMPLETED) ? NULL : wpa_s->bssid;
  3289. }
  3290. static void wpas_fst_get_channel_info_cb(void *ctx,
  3291. enum hostapd_hw_mode *hw_mode,
  3292. u8 *channel)
  3293. {
  3294. struct wpa_supplicant *wpa_s = ctx;
  3295. if (wpa_s->current_bss) {
  3296. *hw_mode = ieee80211_freq_to_chan(wpa_s->current_bss->freq,
  3297. channel);
  3298. } else if (wpa_s->hw.num_modes) {
  3299. *hw_mode = wpa_s->hw.modes[0].mode;
  3300. } else {
  3301. WPA_ASSERT(0);
  3302. *hw_mode = 0;
  3303. }
  3304. }
  3305. static int wpas_fst_get_hw_modes(void *ctx, struct hostapd_hw_modes **modes)
  3306. {
  3307. struct wpa_supplicant *wpa_s = ctx;
  3308. *modes = wpa_s->hw.modes;
  3309. return wpa_s->hw.num_modes;
  3310. }
  3311. static void wpas_fst_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
  3312. {
  3313. struct wpa_supplicant *wpa_s = ctx;
  3314. wpa_hexdump_buf(MSG_DEBUG, "FST: Set IEs", fst_ies);
  3315. wpa_s->fst_ies = fst_ies;
  3316. }
  3317. static int wpas_fst_send_action_cb(void *ctx, const u8 *da, struct wpabuf *data)
  3318. {
  3319. struct wpa_supplicant *wpa_s = ctx;
  3320. WPA_ASSERT(os_memcmp(wpa_s->bssid, da, ETH_ALEN) == 0);
  3321. return wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  3322. wpa_s->own_addr, wpa_s->bssid,
  3323. wpabuf_head(data), wpabuf_len(data),
  3324. 0);
  3325. }
  3326. static const struct wpabuf * wpas_fst_get_mb_ie_cb(void *ctx, const u8 *addr)
  3327. {
  3328. struct wpa_supplicant *wpa_s = ctx;
  3329. WPA_ASSERT(os_memcmp(wpa_s->bssid, addr, ETH_ALEN) == 0);
  3330. return wpa_s->received_mb_ies;
  3331. }
  3332. static void wpas_fst_update_mb_ie_cb(void *ctx, const u8 *addr,
  3333. const u8 *buf, size_t size)
  3334. {
  3335. struct wpa_supplicant *wpa_s = ctx;
  3336. struct mb_ies_info info;
  3337. WPA_ASSERT(os_memcmp(wpa_s->bssid, addr, ETH_ALEN) == 0);
  3338. if (!mb_ies_info_by_ies(&info, buf, size)) {
  3339. wpabuf_free(wpa_s->received_mb_ies);
  3340. wpa_s->received_mb_ies = mb_ies_by_info(&info);
  3341. }
  3342. }
  3343. const u8 * wpas_fst_get_peer_first(void *ctx, struct fst_get_peer_ctx **get_ctx,
  3344. Boolean mb_only)
  3345. {
  3346. struct wpa_supplicant *wpa_s = ctx;
  3347. *get_ctx = NULL;
  3348. if (!is_zero_ether_addr(wpa_s->bssid))
  3349. return (wpa_s->received_mb_ies || !mb_only) ?
  3350. wpa_s->bssid : NULL;
  3351. return NULL;
  3352. }
  3353. const u8 * wpas_fst_get_peer_next(void *ctx, struct fst_get_peer_ctx **get_ctx,
  3354. Boolean mb_only)
  3355. {
  3356. return NULL;
  3357. }
  3358. void fst_wpa_supplicant_fill_iface_obj(struct wpa_supplicant *wpa_s,
  3359. struct fst_wpa_obj *iface_obj)
  3360. {
  3361. iface_obj->ctx = wpa_s;
  3362. iface_obj->get_bssid = wpas_fst_get_bssid_cb;
  3363. iface_obj->get_channel_info = wpas_fst_get_channel_info_cb;
  3364. iface_obj->get_hw_modes = wpas_fst_get_hw_modes;
  3365. iface_obj->set_ies = wpas_fst_set_ies_cb;
  3366. iface_obj->send_action = wpas_fst_send_action_cb;
  3367. iface_obj->get_mb_ie = wpas_fst_get_mb_ie_cb;
  3368. iface_obj->update_mb_ie = wpas_fst_update_mb_ie_cb;
  3369. iface_obj->get_peer_first = wpas_fst_get_peer_first;
  3370. iface_obj->get_peer_next = wpas_fst_get_peer_next;
  3371. }
  3372. #endif /* CONFIG_FST */
  3373. static int wpas_set_wowlan_triggers(struct wpa_supplicant *wpa_s,
  3374. const struct wpa_driver_capa *capa)
  3375. {
  3376. struct wowlan_triggers *triggers;
  3377. int ret = 0;
  3378. if (!wpa_s->conf->wowlan_triggers)
  3379. return 0;
  3380. triggers = wpa_get_wowlan_triggers(wpa_s->conf->wowlan_triggers, capa);
  3381. if (triggers) {
  3382. ret = wpa_drv_wowlan(wpa_s, triggers);
  3383. os_free(triggers);
  3384. }
  3385. return ret;
  3386. }
  3387. static enum wpa_radio_work_band wpas_freq_to_band(int freq)
  3388. {
  3389. if (freq < 3000)
  3390. return BAND_2_4_GHZ;
  3391. if (freq > 50000)
  3392. return BAND_60_GHZ;
  3393. return BAND_5_GHZ;
  3394. }
  3395. static unsigned int wpas_get_bands(struct wpa_supplicant *wpa_s,
  3396. const int *freqs)
  3397. {
  3398. int i;
  3399. unsigned int band = 0;
  3400. if (freqs) {
  3401. /* freqs are specified for the radio work */
  3402. for (i = 0; freqs[i]; i++)
  3403. band |= wpas_freq_to_band(freqs[i]);
  3404. } else {
  3405. /*
  3406. * freqs are not specified, implies all
  3407. * the supported freqs by HW
  3408. */
  3409. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  3410. if (wpa_s->hw.modes[i].num_channels != 0) {
  3411. if (wpa_s->hw.modes[i].mode ==
  3412. HOSTAPD_MODE_IEEE80211B ||
  3413. wpa_s->hw.modes[i].mode ==
  3414. HOSTAPD_MODE_IEEE80211G)
  3415. band |= BAND_2_4_GHZ;
  3416. else if (wpa_s->hw.modes[i].mode ==
  3417. HOSTAPD_MODE_IEEE80211A)
  3418. band |= BAND_5_GHZ;
  3419. else if (wpa_s->hw.modes[i].mode ==
  3420. HOSTAPD_MODE_IEEE80211AD)
  3421. band |= BAND_60_GHZ;
  3422. else if (wpa_s->hw.modes[i].mode ==
  3423. HOSTAPD_MODE_IEEE80211ANY)
  3424. band = BAND_2_4_GHZ | BAND_5_GHZ |
  3425. BAND_60_GHZ;
  3426. }
  3427. }
  3428. }
  3429. return band;
  3430. }
  3431. static struct wpa_radio * radio_add_interface(struct wpa_supplicant *wpa_s,
  3432. const char *rn)
  3433. {
  3434. struct wpa_supplicant *iface = wpa_s->global->ifaces;
  3435. struct wpa_radio *radio;
  3436. while (rn && iface) {
  3437. radio = iface->radio;
  3438. if (radio && os_strcmp(rn, radio->name) == 0) {
  3439. wpa_printf(MSG_DEBUG, "Add interface %s to existing radio %s",
  3440. wpa_s->ifname, rn);
  3441. dl_list_add(&radio->ifaces, &wpa_s->radio_list);
  3442. return radio;
  3443. }
  3444. iface = iface->next;
  3445. }
  3446. wpa_printf(MSG_DEBUG, "Add interface %s to a new radio %s",
  3447. wpa_s->ifname, rn ? rn : "N/A");
  3448. radio = os_zalloc(sizeof(*radio));
  3449. if (radio == NULL)
  3450. return NULL;
  3451. if (rn)
  3452. os_strlcpy(radio->name, rn, sizeof(radio->name));
  3453. dl_list_init(&radio->ifaces);
  3454. dl_list_init(&radio->work);
  3455. dl_list_add(&radio->ifaces, &wpa_s->radio_list);
  3456. return radio;
  3457. }
  3458. static void radio_work_free(struct wpa_radio_work *work)
  3459. {
  3460. if (work->wpa_s->scan_work == work) {
  3461. /* This should not really happen. */
  3462. wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as scan_work",
  3463. work->type, work, work->started);
  3464. work->wpa_s->scan_work = NULL;
  3465. }
  3466. #ifdef CONFIG_P2P
  3467. if (work->wpa_s->p2p_scan_work == work) {
  3468. /* This should not really happen. */
  3469. wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as p2p_scan_work",
  3470. work->type, work, work->started);
  3471. work->wpa_s->p2p_scan_work = NULL;
  3472. }
  3473. #endif /* CONFIG_P2P */
  3474. if (work->started) {
  3475. work->wpa_s->radio->num_active_works--;
  3476. wpa_dbg(work->wpa_s, MSG_DEBUG,
  3477. "radio_work_free('%s'@%p: num_active_works --> %u",
  3478. work->type, work,
  3479. work->wpa_s->radio->num_active_works);
  3480. }
  3481. dl_list_del(&work->list);
  3482. os_free(work);
  3483. }
  3484. static struct wpa_radio_work * radio_work_get_next_work(struct wpa_radio *radio)
  3485. {
  3486. struct wpa_radio_work *active_work = NULL;
  3487. struct wpa_radio_work *tmp;
  3488. /* Get the active work to know the type and band. */
  3489. dl_list_for_each(tmp, &radio->work, struct wpa_radio_work, list) {
  3490. if (tmp->started) {
  3491. active_work = tmp;
  3492. break;
  3493. }
  3494. }
  3495. if (!active_work) {
  3496. /* No active work, start one */
  3497. radio->num_active_works = 0;
  3498. dl_list_for_each(tmp, &radio->work, struct wpa_radio_work,
  3499. list) {
  3500. if (os_strcmp(tmp->type, "scan") == 0 &&
  3501. radio->external_scan_running &&
  3502. (((struct wpa_driver_scan_params *)
  3503. tmp->ctx)->only_new_results ||
  3504. tmp->wpa_s->clear_driver_scan_cache))
  3505. continue;
  3506. return tmp;
  3507. }
  3508. return NULL;
  3509. }
  3510. if (os_strcmp(active_work->type, "sme-connect") == 0 ||
  3511. os_strcmp(active_work->type, "connect") == 0) {
  3512. /*
  3513. * If the active work is either connect or sme-connect,
  3514. * do not parallelize them with other radio works.
  3515. */
  3516. wpa_dbg(active_work->wpa_s, MSG_DEBUG,
  3517. "Do not parallelize radio work with %s",
  3518. active_work->type);
  3519. return NULL;
  3520. }
  3521. dl_list_for_each(tmp, &radio->work, struct wpa_radio_work, list) {
  3522. if (tmp->started)
  3523. continue;
  3524. /*
  3525. * If connect or sme-connect are enqueued, parallelize only
  3526. * those operations ahead of them in the queue.
  3527. */
  3528. if (os_strcmp(tmp->type, "connect") == 0 ||
  3529. os_strcmp(tmp->type, "sme-connect") == 0)
  3530. break;
  3531. /*
  3532. * Check that the radio works are distinct and
  3533. * on different bands.
  3534. */
  3535. if (os_strcmp(active_work->type, tmp->type) != 0 &&
  3536. (active_work->bands != tmp->bands)) {
  3537. /*
  3538. * If a scan has to be scheduled through nl80211 scan
  3539. * interface and if an external scan is already running,
  3540. * do not schedule the scan since it is likely to get
  3541. * rejected by kernel.
  3542. */
  3543. if (os_strcmp(tmp->type, "scan") == 0 &&
  3544. radio->external_scan_running &&
  3545. (((struct wpa_driver_scan_params *)
  3546. tmp->ctx)->only_new_results ||
  3547. tmp->wpa_s->clear_driver_scan_cache))
  3548. continue;
  3549. wpa_dbg(active_work->wpa_s, MSG_DEBUG,
  3550. "active_work:%s new_work:%s",
  3551. active_work->type, tmp->type);
  3552. return tmp;
  3553. }
  3554. }
  3555. /* Did not find a radio work to schedule in parallel. */
  3556. return NULL;
  3557. }
  3558. static void radio_start_next_work(void *eloop_ctx, void *timeout_ctx)
  3559. {
  3560. struct wpa_radio *radio = eloop_ctx;
  3561. struct wpa_radio_work *work;
  3562. struct os_reltime now, diff;
  3563. struct wpa_supplicant *wpa_s;
  3564. work = dl_list_first(&radio->work, struct wpa_radio_work, list);
  3565. if (work == NULL) {
  3566. radio->num_active_works = 0;
  3567. return;
  3568. }
  3569. wpa_s = dl_list_first(&radio->ifaces, struct wpa_supplicant,
  3570. radio_list);
  3571. if (!(wpa_s &&
  3572. wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS)) {
  3573. if (work->started)
  3574. return; /* already started and still in progress */
  3575. if (wpa_s && wpa_s->radio->external_scan_running) {
  3576. wpa_printf(MSG_DEBUG, "Delay radio work start until externally triggered scan completes");
  3577. return;
  3578. }
  3579. } else {
  3580. work = NULL;
  3581. if (radio->num_active_works < MAX_ACTIVE_WORKS) {
  3582. /* get the work to schedule next */
  3583. work = radio_work_get_next_work(radio);
  3584. }
  3585. if (!work)
  3586. return;
  3587. }
  3588. wpa_s = work->wpa_s;
  3589. os_get_reltime(&now);
  3590. os_reltime_sub(&now, &work->time, &diff);
  3591. wpa_dbg(wpa_s, MSG_DEBUG,
  3592. "Starting radio work '%s'@%p after %ld.%06ld second wait",
  3593. work->type, work, diff.sec, diff.usec);
  3594. work->started = 1;
  3595. work->time = now;
  3596. radio->num_active_works++;
  3597. work->cb(work, 0);
  3598. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS) &&
  3599. radio->num_active_works < MAX_ACTIVE_WORKS)
  3600. radio_work_check_next(wpa_s);
  3601. }
  3602. /*
  3603. * This function removes both started and pending radio works running on
  3604. * the provided interface's radio.
  3605. * Prior to the removal of the radio work, its callback (cb) is called with
  3606. * deinit set to be 1. Each work's callback is responsible for clearing its
  3607. * internal data and restoring to a correct state.
  3608. * @wpa_s: wpa_supplicant data
  3609. * @type: type of works to be removed
  3610. * @remove_all: 1 to remove all the works on this radio, 0 to remove only
  3611. * this interface's works.
  3612. */
  3613. void radio_remove_works(struct wpa_supplicant *wpa_s,
  3614. const char *type, int remove_all)
  3615. {
  3616. struct wpa_radio_work *work, *tmp;
  3617. struct wpa_radio *radio = wpa_s->radio;
  3618. dl_list_for_each_safe(work, tmp, &radio->work, struct wpa_radio_work,
  3619. list) {
  3620. if (type && os_strcmp(type, work->type) != 0)
  3621. continue;
  3622. /* skip other ifaces' works */
  3623. if (!remove_all && work->wpa_s != wpa_s)
  3624. continue;
  3625. wpa_dbg(wpa_s, MSG_DEBUG, "Remove radio work '%s'@%p%s",
  3626. work->type, work, work->started ? " (started)" : "");
  3627. work->cb(work, 1);
  3628. radio_work_free(work);
  3629. }
  3630. /* in case we removed the started work */
  3631. radio_work_check_next(wpa_s);
  3632. }
  3633. static void radio_remove_interface(struct wpa_supplicant *wpa_s)
  3634. {
  3635. struct wpa_radio *radio = wpa_s->radio;
  3636. if (!radio)
  3637. return;
  3638. wpa_printf(MSG_DEBUG, "Remove interface %s from radio %s",
  3639. wpa_s->ifname, radio->name);
  3640. dl_list_del(&wpa_s->radio_list);
  3641. radio_remove_works(wpa_s, NULL, 0);
  3642. wpa_s->radio = NULL;
  3643. if (!dl_list_empty(&radio->ifaces))
  3644. return; /* Interfaces remain for this radio */
  3645. wpa_printf(MSG_DEBUG, "Remove radio %s", radio->name);
  3646. eloop_cancel_timeout(radio_start_next_work, radio, NULL);
  3647. os_free(radio);
  3648. }
  3649. void radio_work_check_next(struct wpa_supplicant *wpa_s)
  3650. {
  3651. struct wpa_radio *radio = wpa_s->radio;
  3652. if (dl_list_empty(&radio->work))
  3653. return;
  3654. if (wpa_s->ext_work_in_progress) {
  3655. wpa_printf(MSG_DEBUG,
  3656. "External radio work in progress - delay start of pending item");
  3657. return;
  3658. }
  3659. eloop_cancel_timeout(radio_start_next_work, radio, NULL);
  3660. eloop_register_timeout(0, 0, radio_start_next_work, radio, NULL);
  3661. }
  3662. /**
  3663. * radio_add_work - Add a radio work item
  3664. * @wpa_s: Pointer to wpa_supplicant data
  3665. * @freq: Frequency of the offchannel operation in MHz or 0
  3666. * @type: Unique identifier for each type of work
  3667. * @next: Force as the next work to be executed
  3668. * @cb: Callback function for indicating when radio is available
  3669. * @ctx: Context pointer for the work (work->ctx in cb())
  3670. * Returns: 0 on success, -1 on failure
  3671. *
  3672. * This function is used to request time for an operation that requires
  3673. * exclusive radio control. Once the radio is available, the registered callback
  3674. * function will be called. radio_work_done() must be called once the exclusive
  3675. * radio operation has been completed, so that the radio is freed for other
  3676. * operations. The special case of deinit=1 is used to free the context data
  3677. * during interface removal. That does not allow the callback function to start
  3678. * the radio operation, i.e., it must free any resources allocated for the radio
  3679. * work and return.
  3680. *
  3681. * The @freq parameter can be used to indicate a single channel on which the
  3682. * offchannel operation will occur. This may allow multiple radio work
  3683. * operations to be performed in parallel if they apply for the same channel.
  3684. * Setting this to 0 indicates that the work item may use multiple channels or
  3685. * requires exclusive control of the radio.
  3686. */
  3687. int radio_add_work(struct wpa_supplicant *wpa_s, unsigned int freq,
  3688. const char *type, int next,
  3689. void (*cb)(struct wpa_radio_work *work, int deinit),
  3690. void *ctx)
  3691. {
  3692. struct wpa_radio *radio = wpa_s->radio;
  3693. struct wpa_radio_work *work;
  3694. int was_empty;
  3695. work = os_zalloc(sizeof(*work));
  3696. if (work == NULL)
  3697. return -1;
  3698. wpa_dbg(wpa_s, MSG_DEBUG, "Add radio work '%s'@%p", type, work);
  3699. os_get_reltime(&work->time);
  3700. work->freq = freq;
  3701. work->type = type;
  3702. work->wpa_s = wpa_s;
  3703. work->cb = cb;
  3704. work->ctx = ctx;
  3705. if (freq)
  3706. work->bands = wpas_freq_to_band(freq);
  3707. else if (os_strcmp(type, "scan") == 0 ||
  3708. os_strcmp(type, "p2p-scan") == 0)
  3709. work->bands = wpas_get_bands(wpa_s,
  3710. ((struct wpa_driver_scan_params *)
  3711. ctx)->freqs);
  3712. else
  3713. work->bands = wpas_get_bands(wpa_s, NULL);
  3714. was_empty = dl_list_empty(&wpa_s->radio->work);
  3715. if (next)
  3716. dl_list_add(&wpa_s->radio->work, &work->list);
  3717. else
  3718. dl_list_add_tail(&wpa_s->radio->work, &work->list);
  3719. if (was_empty) {
  3720. wpa_dbg(wpa_s, MSG_DEBUG, "First radio work item in the queue - schedule start immediately");
  3721. radio_work_check_next(wpa_s);
  3722. } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS)
  3723. && radio->num_active_works < MAX_ACTIVE_WORKS) {
  3724. wpa_dbg(wpa_s, MSG_DEBUG,
  3725. "Try to schedule a radio work (num_active_works=%u)",
  3726. radio->num_active_works);
  3727. radio_work_check_next(wpa_s);
  3728. }
  3729. return 0;
  3730. }
  3731. /**
  3732. * radio_work_done - Indicate that a radio work item has been completed
  3733. * @work: Completed work
  3734. *
  3735. * This function is called once the callback function registered with
  3736. * radio_add_work() has completed its work.
  3737. */
  3738. void radio_work_done(struct wpa_radio_work *work)
  3739. {
  3740. struct wpa_supplicant *wpa_s = work->wpa_s;
  3741. struct os_reltime now, diff;
  3742. unsigned int started = work->started;
  3743. os_get_reltime(&now);
  3744. os_reltime_sub(&now, &work->time, &diff);
  3745. wpa_dbg(wpa_s, MSG_DEBUG, "Radio work '%s'@%p %s in %ld.%06ld seconds",
  3746. work->type, work, started ? "done" : "canceled",
  3747. diff.sec, diff.usec);
  3748. radio_work_free(work);
  3749. if (started)
  3750. radio_work_check_next(wpa_s);
  3751. }
  3752. struct wpa_radio_work *
  3753. radio_work_pending(struct wpa_supplicant *wpa_s, const char *type)
  3754. {
  3755. struct wpa_radio_work *work;
  3756. struct wpa_radio *radio = wpa_s->radio;
  3757. dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
  3758. if (work->wpa_s == wpa_s && os_strcmp(work->type, type) == 0)
  3759. return work;
  3760. }
  3761. return NULL;
  3762. }
  3763. static int wpas_init_driver(struct wpa_supplicant *wpa_s,
  3764. struct wpa_interface *iface)
  3765. {
  3766. const char *ifname, *driver, *rn;
  3767. driver = iface->driver;
  3768. next_driver:
  3769. if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
  3770. return -1;
  3771. wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
  3772. if (wpa_s->drv_priv == NULL) {
  3773. const char *pos;
  3774. pos = driver ? os_strchr(driver, ',') : NULL;
  3775. if (pos) {
  3776. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  3777. "driver interface - try next driver wrapper");
  3778. driver = pos + 1;
  3779. goto next_driver;
  3780. }
  3781. wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
  3782. "interface");
  3783. return -1;
  3784. }
  3785. if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
  3786. wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
  3787. "driver_param '%s'", wpa_s->conf->driver_param);
  3788. return -1;
  3789. }
  3790. ifname = wpa_drv_get_ifname(wpa_s);
  3791. if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
  3792. wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
  3793. "interface name with '%s'", ifname);
  3794. os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
  3795. }
  3796. rn = wpa_driver_get_radio_name(wpa_s);
  3797. if (rn && rn[0] == '\0')
  3798. rn = NULL;
  3799. wpa_s->radio = radio_add_interface(wpa_s, rn);
  3800. if (wpa_s->radio == NULL)
  3801. return -1;
  3802. return 0;
  3803. }
  3804. static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
  3805. struct wpa_interface *iface)
  3806. {
  3807. struct wpa_driver_capa capa;
  3808. int capa_res;
  3809. wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
  3810. "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
  3811. iface->confname ? iface->confname : "N/A",
  3812. iface->driver ? iface->driver : "default",
  3813. iface->ctrl_interface ? iface->ctrl_interface : "N/A",
  3814. iface->bridge_ifname ? iface->bridge_ifname : "N/A");
  3815. if (iface->confname) {
  3816. #ifdef CONFIG_BACKEND_FILE
  3817. wpa_s->confname = os_rel2abs_path(iface->confname);
  3818. if (wpa_s->confname == NULL) {
  3819. wpa_printf(MSG_ERROR, "Failed to get absolute path "
  3820. "for configuration file '%s'.",
  3821. iface->confname);
  3822. return -1;
  3823. }
  3824. wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
  3825. iface->confname, wpa_s->confname);
  3826. #else /* CONFIG_BACKEND_FILE */
  3827. wpa_s->confname = os_strdup(iface->confname);
  3828. #endif /* CONFIG_BACKEND_FILE */
  3829. wpa_s->conf = wpa_config_read(wpa_s->confname, NULL);
  3830. if (wpa_s->conf == NULL) {
  3831. wpa_printf(MSG_ERROR, "Failed to read or parse "
  3832. "configuration '%s'.", wpa_s->confname);
  3833. return -1;
  3834. }
  3835. wpa_s->confanother = os_rel2abs_path(iface->confanother);
  3836. wpa_config_read(wpa_s->confanother, wpa_s->conf);
  3837. /*
  3838. * Override ctrl_interface and driver_param if set on command
  3839. * line.
  3840. */
  3841. if (iface->ctrl_interface) {
  3842. os_free(wpa_s->conf->ctrl_interface);
  3843. wpa_s->conf->ctrl_interface =
  3844. os_strdup(iface->ctrl_interface);
  3845. }
  3846. if (iface->driver_param) {
  3847. os_free(wpa_s->conf->driver_param);
  3848. wpa_s->conf->driver_param =
  3849. os_strdup(iface->driver_param);
  3850. }
  3851. if (iface->p2p_mgmt && !iface->ctrl_interface) {
  3852. os_free(wpa_s->conf->ctrl_interface);
  3853. wpa_s->conf->ctrl_interface = NULL;
  3854. }
  3855. } else
  3856. wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
  3857. iface->driver_param);
  3858. if (wpa_s->conf == NULL) {
  3859. wpa_printf(MSG_ERROR, "\nNo configuration found.");
  3860. return -1;
  3861. }
  3862. if (iface->ifname == NULL) {
  3863. wpa_printf(MSG_ERROR, "\nInterface name is required.");
  3864. return -1;
  3865. }
  3866. if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
  3867. wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
  3868. iface->ifname);
  3869. return -1;
  3870. }
  3871. os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
  3872. if (iface->bridge_ifname) {
  3873. if (os_strlen(iface->bridge_ifname) >=
  3874. sizeof(wpa_s->bridge_ifname)) {
  3875. wpa_printf(MSG_ERROR, "\nToo long bridge interface "
  3876. "name '%s'.", iface->bridge_ifname);
  3877. return -1;
  3878. }
  3879. os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
  3880. sizeof(wpa_s->bridge_ifname));
  3881. }
  3882. /* RSNA Supplicant Key Management - INITIALIZE */
  3883. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  3884. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  3885. /* Initialize driver interface and register driver event handler before
  3886. * L2 receive handler so that association events are processed before
  3887. * EAPOL-Key packets if both become available for the same select()
  3888. * call. */
  3889. if (wpas_init_driver(wpa_s, iface) < 0)
  3890. return -1;
  3891. if (wpa_supplicant_init_wpa(wpa_s) < 0)
  3892. return -1;
  3893. wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
  3894. wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
  3895. NULL);
  3896. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  3897. if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
  3898. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  3899. wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
  3900. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  3901. "dot11RSNAConfigPMKLifetime");
  3902. return -1;
  3903. }
  3904. if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
  3905. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  3906. wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
  3907. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  3908. "dot11RSNAConfigPMKReauthThreshold");
  3909. return -1;
  3910. }
  3911. if (wpa_s->conf->dot11RSNAConfigSATimeout &&
  3912. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
  3913. wpa_s->conf->dot11RSNAConfigSATimeout)) {
  3914. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  3915. "dot11RSNAConfigSATimeout");
  3916. return -1;
  3917. }
  3918. wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
  3919. &wpa_s->hw.num_modes,
  3920. &wpa_s->hw.flags);
  3921. if (wpa_s->hw.modes) {
  3922. u16 i;
  3923. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  3924. if (wpa_s->hw.modes[i].vht_capab) {
  3925. wpa_s->hw_capab = CAPAB_VHT;
  3926. break;
  3927. }
  3928. if (wpa_s->hw.modes[i].ht_capab &
  3929. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
  3930. wpa_s->hw_capab = CAPAB_HT40;
  3931. else if (wpa_s->hw.modes[i].ht_capab &&
  3932. wpa_s->hw_capab == CAPAB_NO_HT_VHT)
  3933. wpa_s->hw_capab = CAPAB_HT;
  3934. }
  3935. }
  3936. capa_res = wpa_drv_get_capa(wpa_s, &capa);
  3937. if (capa_res == 0) {
  3938. wpa_s->drv_capa_known = 1;
  3939. wpa_s->drv_flags = capa.flags;
  3940. wpa_s->drv_enc = capa.enc;
  3941. wpa_s->drv_smps_modes = capa.smps_modes;
  3942. wpa_s->drv_rrm_flags = capa.rrm_flags;
  3943. wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
  3944. wpa_s->max_scan_ssids = capa.max_scan_ssids;
  3945. wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
  3946. wpa_s->max_sched_scan_plans = capa.max_sched_scan_plans;
  3947. wpa_s->max_sched_scan_plan_interval =
  3948. capa.max_sched_scan_plan_interval;
  3949. wpa_s->max_sched_scan_plan_iterations =
  3950. capa.max_sched_scan_plan_iterations;
  3951. wpa_s->sched_scan_supported = capa.sched_scan_supported;
  3952. wpa_s->max_match_sets = capa.max_match_sets;
  3953. wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
  3954. wpa_s->max_stations = capa.max_stations;
  3955. wpa_s->extended_capa = capa.extended_capa;
  3956. wpa_s->extended_capa_mask = capa.extended_capa_mask;
  3957. wpa_s->extended_capa_len = capa.extended_capa_len;
  3958. wpa_s->num_multichan_concurrent =
  3959. capa.num_multichan_concurrent;
  3960. wpa_s->wmm_ac_supported = capa.wmm_ac_supported;
  3961. if (capa.mac_addr_rand_scan_supported)
  3962. wpa_s->mac_addr_rand_supported |= MAC_ADDR_RAND_SCAN;
  3963. if (wpa_s->sched_scan_supported &&
  3964. capa.mac_addr_rand_sched_scan_supported)
  3965. wpa_s->mac_addr_rand_supported |=
  3966. (MAC_ADDR_RAND_SCHED_SCAN | MAC_ADDR_RAND_PNO);
  3967. }
  3968. if (wpa_s->max_remain_on_chan == 0)
  3969. wpa_s->max_remain_on_chan = 1000;
  3970. /*
  3971. * Only take p2p_mgmt parameters when P2P Device is supported.
  3972. * Doing it here as it determines whether l2_packet_init() will be done
  3973. * during wpa_supplicant_driver_init().
  3974. */
  3975. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
  3976. wpa_s->p2p_mgmt = iface->p2p_mgmt;
  3977. else
  3978. iface->p2p_mgmt = 1;
  3979. if (wpa_s->num_multichan_concurrent == 0)
  3980. wpa_s->num_multichan_concurrent = 1;
  3981. if (wpa_supplicant_driver_init(wpa_s) < 0)
  3982. return -1;
  3983. #ifdef CONFIG_TDLS
  3984. if ((!iface->p2p_mgmt ||
  3985. !(wpa_s->drv_flags &
  3986. WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
  3987. wpa_tdls_init(wpa_s->wpa))
  3988. return -1;
  3989. #endif /* CONFIG_TDLS */
  3990. if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
  3991. wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
  3992. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
  3993. return -1;
  3994. }
  3995. #ifdef CONFIG_FST
  3996. if (wpa_s->conf->fst_group_id) {
  3997. struct fst_iface_cfg cfg;
  3998. struct fst_wpa_obj iface_obj;
  3999. fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
  4000. os_strlcpy(cfg.group_id, wpa_s->conf->fst_group_id,
  4001. sizeof(cfg.group_id));
  4002. cfg.priority = wpa_s->conf->fst_priority;
  4003. cfg.llt = wpa_s->conf->fst_llt;
  4004. wpa_s->fst = fst_attach(wpa_s->ifname, wpa_s->own_addr,
  4005. &iface_obj, &cfg);
  4006. if (!wpa_s->fst) {
  4007. wpa_msg(wpa_s, MSG_ERROR,
  4008. "FST: Cannot attach iface %s to group %s",
  4009. wpa_s->ifname, cfg.group_id);
  4010. return -1;
  4011. }
  4012. }
  4013. #endif /* CONFIG_FST */
  4014. if (wpas_wps_init(wpa_s))
  4015. return -1;
  4016. if (wpa_supplicant_init_eapol(wpa_s) < 0)
  4017. return -1;
  4018. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  4019. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  4020. if (wpa_s->ctrl_iface == NULL) {
  4021. wpa_printf(MSG_ERROR,
  4022. "Failed to initialize control interface '%s'.\n"
  4023. "You may have another wpa_supplicant process "
  4024. "already running or the file was\n"
  4025. "left by an unclean termination of wpa_supplicant "
  4026. "in which case you will need\n"
  4027. "to manually remove this file before starting "
  4028. "wpa_supplicant again.\n",
  4029. wpa_s->conf->ctrl_interface);
  4030. return -1;
  4031. }
  4032. wpa_s->gas = gas_query_init(wpa_s);
  4033. if (wpa_s->gas == NULL) {
  4034. wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
  4035. return -1;
  4036. }
  4037. if (iface->p2p_mgmt && wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
  4038. wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
  4039. return -1;
  4040. }
  4041. if (wpa_bss_init(wpa_s) < 0)
  4042. return -1;
  4043. /*
  4044. * Set Wake-on-WLAN triggers, if configured.
  4045. * Note: We don't restore/remove the triggers on shutdown (it doesn't
  4046. * have effect anyway when the interface is down).
  4047. */
  4048. if (capa_res == 0 && wpas_set_wowlan_triggers(wpa_s, &capa) < 0)
  4049. return -1;
  4050. #ifdef CONFIG_EAP_PROXY
  4051. {
  4052. size_t len;
  4053. wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, wpa_s->imsi,
  4054. &len);
  4055. if (wpa_s->mnc_len > 0) {
  4056. wpa_s->imsi[len] = '\0';
  4057. wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
  4058. wpa_s->imsi, wpa_s->mnc_len);
  4059. } else {
  4060. wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
  4061. }
  4062. }
  4063. #endif /* CONFIG_EAP_PROXY */
  4064. if (pcsc_reader_init(wpa_s) < 0)
  4065. return -1;
  4066. if (wpas_init_ext_pw(wpa_s) < 0)
  4067. return -1;
  4068. wpas_rrm_reset(wpa_s);
  4069. wpas_sched_scan_plans_set(wpa_s, wpa_s->conf->sched_scan_plans);
  4070. #ifdef CONFIG_HS20
  4071. hs20_init(wpa_s);
  4072. #endif /* CONFIG_HS20 */
  4073. return 0;
  4074. }
  4075. static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
  4076. int notify, int terminate)
  4077. {
  4078. struct wpa_global *global = wpa_s->global;
  4079. struct wpa_supplicant *iface, *prev;
  4080. if (wpa_s == wpa_s->parent)
  4081. wpas_p2p_group_remove(wpa_s, "*");
  4082. iface = global->ifaces;
  4083. while (iface) {
  4084. if (iface == wpa_s || iface->parent != wpa_s) {
  4085. iface = iface->next;
  4086. continue;
  4087. }
  4088. wpa_printf(MSG_DEBUG,
  4089. "Remove remaining child interface %s from parent %s",
  4090. iface->ifname, wpa_s->ifname);
  4091. prev = iface;
  4092. iface = iface->next;
  4093. wpa_supplicant_remove_iface(global, prev, terminate);
  4094. }
  4095. wpa_s->disconnected = 1;
  4096. if (wpa_s->drv_priv) {
  4097. wpa_supplicant_deauthenticate(wpa_s,
  4098. WLAN_REASON_DEAUTH_LEAVING);
  4099. wpa_drv_set_countermeasures(wpa_s, 0);
  4100. wpa_clear_keys(wpa_s, NULL);
  4101. }
  4102. wpa_supplicant_cleanup(wpa_s);
  4103. wpas_p2p_deinit_iface(wpa_s);
  4104. wpas_ctrl_radio_work_flush(wpa_s);
  4105. radio_remove_interface(wpa_s);
  4106. #ifdef CONFIG_FST
  4107. if (wpa_s->fst) {
  4108. fst_detach(wpa_s->fst);
  4109. wpa_s->fst = NULL;
  4110. }
  4111. if (wpa_s->received_mb_ies) {
  4112. wpabuf_free(wpa_s->received_mb_ies);
  4113. wpa_s->received_mb_ies = NULL;
  4114. }
  4115. #endif /* CONFIG_FST */
  4116. if (wpa_s->drv_priv)
  4117. wpa_drv_deinit(wpa_s);
  4118. if (notify)
  4119. wpas_notify_iface_removed(wpa_s);
  4120. if (terminate)
  4121. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
  4122. if (wpa_s->ctrl_iface) {
  4123. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  4124. wpa_s->ctrl_iface = NULL;
  4125. }
  4126. #ifdef CONFIG_MESH
  4127. if (wpa_s->ifmsh) {
  4128. wpa_supplicant_mesh_iface_deinit(wpa_s, wpa_s->ifmsh);
  4129. wpa_s->ifmsh = NULL;
  4130. }
  4131. #endif /* CONFIG_MESH */
  4132. if (wpa_s->conf != NULL) {
  4133. wpa_config_free(wpa_s->conf);
  4134. wpa_s->conf = NULL;
  4135. }
  4136. os_free(wpa_s->ssids_from_scan_req);
  4137. os_free(wpa_s);
  4138. }
  4139. /**
  4140. * wpa_supplicant_add_iface - Add a new network interface
  4141. * @global: Pointer to global data from wpa_supplicant_init()
  4142. * @iface: Interface configuration options
  4143. * @parent: Parent interface or %NULL to assign new interface as parent
  4144. * Returns: Pointer to the created interface or %NULL on failure
  4145. *
  4146. * This function is used to add new network interfaces for %wpa_supplicant.
  4147. * This can be called before wpa_supplicant_run() to add interfaces before the
  4148. * main event loop has been started. In addition, new interfaces can be added
  4149. * dynamically while %wpa_supplicant is already running. This could happen,
  4150. * e.g., when a hotplug network adapter is inserted.
  4151. */
  4152. struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
  4153. struct wpa_interface *iface,
  4154. struct wpa_supplicant *parent)
  4155. {
  4156. struct wpa_supplicant *wpa_s;
  4157. struct wpa_interface t_iface;
  4158. struct wpa_ssid *ssid;
  4159. if (global == NULL || iface == NULL)
  4160. return NULL;
  4161. wpa_s = wpa_supplicant_alloc(parent);
  4162. if (wpa_s == NULL)
  4163. return NULL;
  4164. wpa_s->global = global;
  4165. t_iface = *iface;
  4166. if (global->params.override_driver) {
  4167. wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
  4168. "('%s' -> '%s')",
  4169. iface->driver, global->params.override_driver);
  4170. t_iface.driver = global->params.override_driver;
  4171. }
  4172. if (global->params.override_ctrl_interface) {
  4173. wpa_printf(MSG_DEBUG, "Override interface parameter: "
  4174. "ctrl_interface ('%s' -> '%s')",
  4175. iface->ctrl_interface,
  4176. global->params.override_ctrl_interface);
  4177. t_iface.ctrl_interface =
  4178. global->params.override_ctrl_interface;
  4179. }
  4180. if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
  4181. wpa_printf(MSG_DEBUG, "Failed to add interface %s",
  4182. iface->ifname);
  4183. wpa_supplicant_deinit_iface(wpa_s, 0, 0);
  4184. return NULL;
  4185. }
  4186. if (iface->p2p_mgmt == 0) {
  4187. /* Notify the control interfaces about new iface */
  4188. if (wpas_notify_iface_added(wpa_s)) {
  4189. wpa_supplicant_deinit_iface(wpa_s, 1, 0);
  4190. return NULL;
  4191. }
  4192. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  4193. wpas_notify_network_added(wpa_s, ssid);
  4194. }
  4195. wpa_s->next = global->ifaces;
  4196. global->ifaces = wpa_s;
  4197. wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
  4198. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  4199. #ifdef CONFIG_P2P
  4200. if (wpa_s->global->p2p == NULL &&
  4201. !wpa_s->global->p2p_disabled && !wpa_s->conf->p2p_disabled &&
  4202. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
  4203. wpas_p2p_add_p2pdev_interface(
  4204. wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
  4205. wpa_printf(MSG_INFO,
  4206. "P2P: Failed to enable P2P Device interface");
  4207. /* Try to continue without. P2P will be disabled. */
  4208. }
  4209. #endif /* CONFIG_P2P */
  4210. return wpa_s;
  4211. }
  4212. /**
  4213. * wpa_supplicant_remove_iface - Remove a network interface
  4214. * @global: Pointer to global data from wpa_supplicant_init()
  4215. * @wpa_s: Pointer to the network interface to be removed
  4216. * Returns: 0 if interface was removed, -1 if interface was not found
  4217. *
  4218. * This function can be used to dynamically remove network interfaces from
  4219. * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
  4220. * addition, this function is used to remove all remaining interfaces when
  4221. * %wpa_supplicant is terminated.
  4222. */
  4223. int wpa_supplicant_remove_iface(struct wpa_global *global,
  4224. struct wpa_supplicant *wpa_s,
  4225. int terminate)
  4226. {
  4227. struct wpa_supplicant *prev;
  4228. #ifdef CONFIG_MESH
  4229. unsigned int mesh_if_created = wpa_s->mesh_if_created;
  4230. char *ifname = NULL;
  4231. #endif /* CONFIG_MESH */
  4232. /* Remove interface from the global list of interfaces */
  4233. prev = global->ifaces;
  4234. if (prev == wpa_s) {
  4235. global->ifaces = wpa_s->next;
  4236. } else {
  4237. while (prev && prev->next != wpa_s)
  4238. prev = prev->next;
  4239. if (prev == NULL)
  4240. return -1;
  4241. prev->next = wpa_s->next;
  4242. }
  4243. wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
  4244. #ifdef CONFIG_MESH
  4245. if (mesh_if_created) {
  4246. ifname = os_strdup(wpa_s->ifname);
  4247. if (ifname == NULL) {
  4248. wpa_dbg(wpa_s, MSG_ERROR,
  4249. "mesh: Failed to malloc ifname");
  4250. return -1;
  4251. }
  4252. }
  4253. #endif /* CONFIG_MESH */
  4254. if (global->p2p_group_formation == wpa_s)
  4255. global->p2p_group_formation = NULL;
  4256. if (global->p2p_invite_group == wpa_s)
  4257. global->p2p_invite_group = NULL;
  4258. wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
  4259. #ifdef CONFIG_MESH
  4260. if (mesh_if_created) {
  4261. wpa_drv_if_remove(global->ifaces, WPA_IF_MESH, ifname);
  4262. os_free(ifname);
  4263. }
  4264. #endif /* CONFIG_MESH */
  4265. return 0;
  4266. }
  4267. /**
  4268. * wpa_supplicant_get_eap_mode - Get the current EAP mode
  4269. * @wpa_s: Pointer to the network interface
  4270. * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
  4271. */
  4272. const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
  4273. {
  4274. const char *eapol_method;
  4275. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
  4276. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  4277. return "NO-EAP";
  4278. }
  4279. eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
  4280. if (eapol_method == NULL)
  4281. return "UNKNOWN-EAP";
  4282. return eapol_method;
  4283. }
  4284. /**
  4285. * wpa_supplicant_get_iface - Get a new network interface
  4286. * @global: Pointer to global data from wpa_supplicant_init()
  4287. * @ifname: Interface name
  4288. * Returns: Pointer to the interface or %NULL if not found
  4289. */
  4290. struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
  4291. const char *ifname)
  4292. {
  4293. struct wpa_supplicant *wpa_s;
  4294. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4295. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  4296. return wpa_s;
  4297. }
  4298. return NULL;
  4299. }
  4300. #ifndef CONFIG_NO_WPA_MSG
  4301. static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
  4302. {
  4303. struct wpa_supplicant *wpa_s = ctx;
  4304. if (wpa_s == NULL)
  4305. return NULL;
  4306. return wpa_s->ifname;
  4307. }
  4308. #endif /* CONFIG_NO_WPA_MSG */
  4309. #ifndef WPA_SUPPLICANT_CLEANUP_INTERVAL
  4310. #define WPA_SUPPLICANT_CLEANUP_INTERVAL 10
  4311. #endif /* WPA_SUPPLICANT_CLEANUP_INTERVAL */
  4312. /* Periodic cleanup tasks */
  4313. static void wpas_periodic(void *eloop_ctx, void *timeout_ctx)
  4314. {
  4315. struct wpa_global *global = eloop_ctx;
  4316. struct wpa_supplicant *wpa_s;
  4317. eloop_register_timeout(WPA_SUPPLICANT_CLEANUP_INTERVAL, 0,
  4318. wpas_periodic, global, NULL);
  4319. #ifdef CONFIG_P2P
  4320. if (global->p2p)
  4321. p2p_expire_peers(global->p2p);
  4322. #endif /* CONFIG_P2P */
  4323. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4324. wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
  4325. #ifdef CONFIG_AP
  4326. ap_periodic(wpa_s);
  4327. #endif /* CONFIG_AP */
  4328. }
  4329. }
  4330. /**
  4331. * wpa_supplicant_init - Initialize %wpa_supplicant
  4332. * @params: Parameters for %wpa_supplicant
  4333. * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
  4334. *
  4335. * This function is used to initialize %wpa_supplicant. After successful
  4336. * initialization, the returned data pointer can be used to add and remove
  4337. * network interfaces, and eventually, to deinitialize %wpa_supplicant.
  4338. */
  4339. struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
  4340. {
  4341. struct wpa_global *global;
  4342. int ret, i;
  4343. if (params == NULL)
  4344. return NULL;
  4345. #ifdef CONFIG_DRIVER_NDIS
  4346. {
  4347. void driver_ndis_init_ops(void);
  4348. driver_ndis_init_ops();
  4349. }
  4350. #endif /* CONFIG_DRIVER_NDIS */
  4351. #ifndef CONFIG_NO_WPA_MSG
  4352. wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
  4353. #endif /* CONFIG_NO_WPA_MSG */
  4354. if (params->wpa_debug_file_path)
  4355. wpa_debug_open_file(params->wpa_debug_file_path);
  4356. else
  4357. wpa_debug_setup_stdout();
  4358. if (params->wpa_debug_syslog)
  4359. wpa_debug_open_syslog();
  4360. if (params->wpa_debug_tracing) {
  4361. ret = wpa_debug_open_linux_tracing();
  4362. if (ret) {
  4363. wpa_printf(MSG_ERROR,
  4364. "Failed to enable trace logging");
  4365. return NULL;
  4366. }
  4367. }
  4368. ret = eap_register_methods();
  4369. if (ret) {
  4370. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  4371. if (ret == -2)
  4372. wpa_printf(MSG_ERROR, "Two or more EAP methods used "
  4373. "the same EAP type.");
  4374. return NULL;
  4375. }
  4376. global = os_zalloc(sizeof(*global));
  4377. if (global == NULL)
  4378. return NULL;
  4379. dl_list_init(&global->p2p_srv_bonjour);
  4380. dl_list_init(&global->p2p_srv_upnp);
  4381. global->params.daemonize = params->daemonize;
  4382. global->params.wait_for_monitor = params->wait_for_monitor;
  4383. global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
  4384. if (params->pid_file)
  4385. global->params.pid_file = os_strdup(params->pid_file);
  4386. if (params->ctrl_interface)
  4387. global->params.ctrl_interface =
  4388. os_strdup(params->ctrl_interface);
  4389. if (params->ctrl_interface_group)
  4390. global->params.ctrl_interface_group =
  4391. os_strdup(params->ctrl_interface_group);
  4392. if (params->override_driver)
  4393. global->params.override_driver =
  4394. os_strdup(params->override_driver);
  4395. if (params->override_ctrl_interface)
  4396. global->params.override_ctrl_interface =
  4397. os_strdup(params->override_ctrl_interface);
  4398. #ifdef CONFIG_P2P
  4399. if (params->conf_p2p_dev)
  4400. global->params.conf_p2p_dev =
  4401. os_strdup(params->conf_p2p_dev);
  4402. #endif /* CONFIG_P2P */
  4403. wpa_debug_level = global->params.wpa_debug_level =
  4404. params->wpa_debug_level;
  4405. wpa_debug_show_keys = global->params.wpa_debug_show_keys =
  4406. params->wpa_debug_show_keys;
  4407. wpa_debug_timestamp = global->params.wpa_debug_timestamp =
  4408. params->wpa_debug_timestamp;
  4409. wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
  4410. if (eloop_init()) {
  4411. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  4412. wpa_supplicant_deinit(global);
  4413. return NULL;
  4414. }
  4415. random_init(params->entropy_file);
  4416. global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
  4417. if (global->ctrl_iface == NULL) {
  4418. wpa_supplicant_deinit(global);
  4419. return NULL;
  4420. }
  4421. if (wpas_notify_supplicant_initialized(global)) {
  4422. wpa_supplicant_deinit(global);
  4423. return NULL;
  4424. }
  4425. for (i = 0; wpa_drivers[i]; i++)
  4426. global->drv_count++;
  4427. if (global->drv_count == 0) {
  4428. wpa_printf(MSG_ERROR, "No drivers enabled");
  4429. wpa_supplicant_deinit(global);
  4430. return NULL;
  4431. }
  4432. global->drv_priv = os_calloc(global->drv_count, sizeof(void *));
  4433. if (global->drv_priv == NULL) {
  4434. wpa_supplicant_deinit(global);
  4435. return NULL;
  4436. }
  4437. #ifdef CONFIG_WIFI_DISPLAY
  4438. if (wifi_display_init(global) < 0) {
  4439. wpa_printf(MSG_ERROR, "Failed to initialize Wi-Fi Display");
  4440. wpa_supplicant_deinit(global);
  4441. return NULL;
  4442. }
  4443. #endif /* CONFIG_WIFI_DISPLAY */
  4444. eloop_register_timeout(WPA_SUPPLICANT_CLEANUP_INTERVAL, 0,
  4445. wpas_periodic, global, NULL);
  4446. return global;
  4447. }
  4448. /**
  4449. * wpa_supplicant_run - Run the %wpa_supplicant main event loop
  4450. * @global: Pointer to global data from wpa_supplicant_init()
  4451. * Returns: 0 after successful event loop run, -1 on failure
  4452. *
  4453. * This function starts the main event loop and continues running as long as
  4454. * there are any remaining events. In most cases, this function is running as
  4455. * long as the %wpa_supplicant process in still in use.
  4456. */
  4457. int wpa_supplicant_run(struct wpa_global *global)
  4458. {
  4459. struct wpa_supplicant *wpa_s;
  4460. if (global->params.daemonize &&
  4461. (wpa_supplicant_daemon(global->params.pid_file) ||
  4462. eloop_sock_requeue()))
  4463. return -1;
  4464. if (global->params.wait_for_monitor) {
  4465. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
  4466. if (wpa_s->ctrl_iface && !wpa_s->p2p_mgmt)
  4467. wpa_supplicant_ctrl_iface_wait(
  4468. wpa_s->ctrl_iface);
  4469. }
  4470. eloop_register_signal_terminate(wpa_supplicant_terminate, global);
  4471. eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
  4472. eloop_run();
  4473. return 0;
  4474. }
  4475. /**
  4476. * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
  4477. * @global: Pointer to global data from wpa_supplicant_init()
  4478. *
  4479. * This function is called to deinitialize %wpa_supplicant and to free all
  4480. * allocated resources. Remaining network interfaces will also be removed.
  4481. */
  4482. void wpa_supplicant_deinit(struct wpa_global *global)
  4483. {
  4484. int i;
  4485. if (global == NULL)
  4486. return;
  4487. eloop_cancel_timeout(wpas_periodic, global, NULL);
  4488. #ifdef CONFIG_WIFI_DISPLAY
  4489. wifi_display_deinit(global);
  4490. #endif /* CONFIG_WIFI_DISPLAY */
  4491. while (global->ifaces)
  4492. wpa_supplicant_remove_iface(global, global->ifaces, 1);
  4493. if (global->ctrl_iface)
  4494. wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
  4495. wpas_notify_supplicant_deinitialized(global);
  4496. eap_peer_unregister_methods();
  4497. #ifdef CONFIG_AP
  4498. eap_server_unregister_methods();
  4499. #endif /* CONFIG_AP */
  4500. for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
  4501. if (!global->drv_priv[i])
  4502. continue;
  4503. wpa_drivers[i]->global_deinit(global->drv_priv[i]);
  4504. }
  4505. os_free(global->drv_priv);
  4506. random_deinit();
  4507. eloop_destroy();
  4508. if (global->params.pid_file) {
  4509. os_daemonize_terminate(global->params.pid_file);
  4510. os_free(global->params.pid_file);
  4511. }
  4512. os_free(global->params.ctrl_interface);
  4513. os_free(global->params.ctrl_interface_group);
  4514. os_free(global->params.override_driver);
  4515. os_free(global->params.override_ctrl_interface);
  4516. #ifdef CONFIG_P2P
  4517. os_free(global->params.conf_p2p_dev);
  4518. #endif /* CONFIG_P2P */
  4519. os_free(global->p2p_disallow_freq.range);
  4520. os_free(global->p2p_go_avoid_freq.range);
  4521. os_free(global->add_psk);
  4522. os_free(global);
  4523. wpa_debug_close_syslog();
  4524. wpa_debug_close_file();
  4525. wpa_debug_close_linux_tracing();
  4526. }
  4527. void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
  4528. {
  4529. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  4530. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  4531. char country[3];
  4532. country[0] = wpa_s->conf->country[0];
  4533. country[1] = wpa_s->conf->country[1];
  4534. country[2] = '\0';
  4535. if (wpa_drv_set_country(wpa_s, country) < 0) {
  4536. wpa_printf(MSG_ERROR, "Failed to set country code "
  4537. "'%s'", country);
  4538. }
  4539. }
  4540. if (wpa_s->conf->changed_parameters & CFG_CHANGED_EXT_PW_BACKEND)
  4541. wpas_init_ext_pw(wpa_s);
  4542. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SCHED_SCAN_PLANS)
  4543. wpas_sched_scan_plans_set(wpa_s, wpa_s->conf->sched_scan_plans);
  4544. #ifdef CONFIG_WPS
  4545. wpas_wps_update_config(wpa_s);
  4546. #endif /* CONFIG_WPS */
  4547. wpas_p2p_update_config(wpa_s);
  4548. wpa_s->conf->changed_parameters = 0;
  4549. }
  4550. void add_freq(int *freqs, int *num_freqs, int freq)
  4551. {
  4552. int i;
  4553. for (i = 0; i < *num_freqs; i++) {
  4554. if (freqs[i] == freq)
  4555. return;
  4556. }
  4557. freqs[*num_freqs] = freq;
  4558. (*num_freqs)++;
  4559. }
  4560. static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
  4561. {
  4562. struct wpa_bss *bss, *cbss;
  4563. const int max_freqs = 10;
  4564. int *freqs;
  4565. int num_freqs = 0;
  4566. freqs = os_calloc(max_freqs + 1, sizeof(int));
  4567. if (freqs == NULL)
  4568. return NULL;
  4569. cbss = wpa_s->current_bss;
  4570. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  4571. if (bss == cbss)
  4572. continue;
  4573. if (bss->ssid_len == cbss->ssid_len &&
  4574. os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
  4575. wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
  4576. add_freq(freqs, &num_freqs, bss->freq);
  4577. if (num_freqs == max_freqs)
  4578. break;
  4579. }
  4580. }
  4581. if (num_freqs == 0) {
  4582. os_free(freqs);
  4583. freqs = NULL;
  4584. }
  4585. return freqs;
  4586. }
  4587. void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
  4588. {
  4589. int timeout;
  4590. int count;
  4591. int *freqs = NULL;
  4592. wpas_connect_work_done(wpa_s);
  4593. /*
  4594. * Remove possible authentication timeout since the connection failed.
  4595. */
  4596. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  4597. /*
  4598. * There is no point in blacklisting the AP if this event is
  4599. * generated based on local request to disconnect.
  4600. */
  4601. if (wpa_s->own_disconnect_req) {
  4602. wpa_s->own_disconnect_req = 0;
  4603. wpa_dbg(wpa_s, MSG_DEBUG,
  4604. "Ignore connection failure due to local request to disconnect");
  4605. return;
  4606. }
  4607. if (wpa_s->disconnected) {
  4608. wpa_dbg(wpa_s, MSG_DEBUG, "Ignore connection failure "
  4609. "indication since interface has been put into "
  4610. "disconnected state");
  4611. return;
  4612. }
  4613. /*
  4614. * Add the failed BSSID into the blacklist and speed up next scan
  4615. * attempt if there could be other APs that could accept association.
  4616. * The current blacklist count indicates how many times we have tried
  4617. * connecting to this AP and multiple attempts mean that other APs are
  4618. * either not available or has already been tried, so that we can start
  4619. * increasing the delay here to avoid constant scanning.
  4620. */
  4621. count = wpa_blacklist_add(wpa_s, bssid);
  4622. if (count == 1 && wpa_s->current_bss) {
  4623. /*
  4624. * This BSS was not in the blacklist before. If there is
  4625. * another BSS available for the same ESS, we should try that
  4626. * next. Otherwise, we may as well try this one once more
  4627. * before allowing other, likely worse, ESSes to be considered.
  4628. */
  4629. freqs = get_bss_freqs_in_ess(wpa_s);
  4630. if (freqs) {
  4631. wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
  4632. "has been seen; try it next");
  4633. wpa_blacklist_add(wpa_s, bssid);
  4634. /*
  4635. * On the next scan, go through only the known channels
  4636. * used in this ESS based on previous scans to speed up
  4637. * common load balancing use case.
  4638. */
  4639. os_free(wpa_s->next_scan_freqs);
  4640. wpa_s->next_scan_freqs = freqs;
  4641. }
  4642. }
  4643. /*
  4644. * Add previous failure count in case the temporary blacklist was
  4645. * cleared due to no other BSSes being available.
  4646. */
  4647. count += wpa_s->extra_blacklist_count;
  4648. if (count > 3 && wpa_s->current_ssid) {
  4649. wpa_printf(MSG_DEBUG, "Continuous association failures - "
  4650. "consider temporary network disabling");
  4651. wpas_auth_failed(wpa_s, "CONN_FAILED");
  4652. }
  4653. switch (count) {
  4654. case 1:
  4655. timeout = 100;
  4656. break;
  4657. case 2:
  4658. timeout = 500;
  4659. break;
  4660. case 3:
  4661. timeout = 1000;
  4662. break;
  4663. case 4:
  4664. timeout = 5000;
  4665. break;
  4666. default:
  4667. timeout = 10000;
  4668. break;
  4669. }
  4670. wpa_dbg(wpa_s, MSG_DEBUG, "Blacklist count %d --> request scan in %d "
  4671. "ms", count, timeout);
  4672. /*
  4673. * TODO: if more than one possible AP is available in scan results,
  4674. * could try the other ones before requesting a new scan.
  4675. */
  4676. wpa_supplicant_req_scan(wpa_s, timeout / 1000,
  4677. 1000 * (timeout % 1000));
  4678. }
  4679. int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
  4680. {
  4681. return wpa_s->conf->ap_scan == 2 ||
  4682. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
  4683. }
  4684. #if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
  4685. int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
  4686. struct wpa_ssid *ssid,
  4687. const char *field,
  4688. const char *value)
  4689. {
  4690. #ifdef IEEE8021X_EAPOL
  4691. struct eap_peer_config *eap = &ssid->eap;
  4692. wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
  4693. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
  4694. (const u8 *) value, os_strlen(value));
  4695. switch (wpa_supplicant_ctrl_req_from_string(field)) {
  4696. case WPA_CTRL_REQ_EAP_IDENTITY:
  4697. os_free(eap->identity);
  4698. eap->identity = (u8 *) os_strdup(value);
  4699. eap->identity_len = os_strlen(value);
  4700. eap->pending_req_identity = 0;
  4701. if (ssid == wpa_s->current_ssid)
  4702. wpa_s->reassociate = 1;
  4703. break;
  4704. case WPA_CTRL_REQ_EAP_PASSWORD:
  4705. bin_clear_free(eap->password, eap->password_len);
  4706. eap->password = (u8 *) os_strdup(value);
  4707. eap->password_len = os_strlen(value);
  4708. eap->pending_req_password = 0;
  4709. if (ssid == wpa_s->current_ssid)
  4710. wpa_s->reassociate = 1;
  4711. break;
  4712. case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
  4713. bin_clear_free(eap->new_password, eap->new_password_len);
  4714. eap->new_password = (u8 *) os_strdup(value);
  4715. eap->new_password_len = os_strlen(value);
  4716. eap->pending_req_new_password = 0;
  4717. if (ssid == wpa_s->current_ssid)
  4718. wpa_s->reassociate = 1;
  4719. break;
  4720. case WPA_CTRL_REQ_EAP_PIN:
  4721. str_clear_free(eap->pin);
  4722. eap->pin = os_strdup(value);
  4723. eap->pending_req_pin = 0;
  4724. if (ssid == wpa_s->current_ssid)
  4725. wpa_s->reassociate = 1;
  4726. break;
  4727. case WPA_CTRL_REQ_EAP_OTP:
  4728. bin_clear_free(eap->otp, eap->otp_len);
  4729. eap->otp = (u8 *) os_strdup(value);
  4730. eap->otp_len = os_strlen(value);
  4731. os_free(eap->pending_req_otp);
  4732. eap->pending_req_otp = NULL;
  4733. eap->pending_req_otp_len = 0;
  4734. break;
  4735. case WPA_CTRL_REQ_EAP_PASSPHRASE:
  4736. str_clear_free(eap->private_key_passwd);
  4737. eap->private_key_passwd = os_strdup(value);
  4738. eap->pending_req_passphrase = 0;
  4739. if (ssid == wpa_s->current_ssid)
  4740. wpa_s->reassociate = 1;
  4741. break;
  4742. case WPA_CTRL_REQ_SIM:
  4743. str_clear_free(eap->external_sim_resp);
  4744. eap->external_sim_resp = os_strdup(value);
  4745. break;
  4746. case WPA_CTRL_REQ_PSK_PASSPHRASE:
  4747. if (wpa_config_set(ssid, "psk", value, 0) < 0)
  4748. return -1;
  4749. ssid->mem_only_psk = 1;
  4750. if (ssid->passphrase)
  4751. wpa_config_update_psk(ssid);
  4752. if (wpa_s->wpa_state == WPA_SCANNING && !wpa_s->scanning)
  4753. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4754. break;
  4755. case WPA_CTRL_REQ_EXT_CERT_CHECK:
  4756. if (eap->pending_ext_cert_check != PENDING_CHECK)
  4757. return -1;
  4758. if (os_strcmp(value, "good") == 0)
  4759. eap->pending_ext_cert_check = EXT_CERT_CHECK_GOOD;
  4760. else if (os_strcmp(value, "bad") == 0)
  4761. eap->pending_ext_cert_check = EXT_CERT_CHECK_BAD;
  4762. else
  4763. return -1;
  4764. break;
  4765. default:
  4766. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
  4767. return -1;
  4768. }
  4769. return 0;
  4770. #else /* IEEE8021X_EAPOL */
  4771. wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
  4772. return -1;
  4773. #endif /* IEEE8021X_EAPOL */
  4774. }
  4775. #endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */
  4776. int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  4777. {
  4778. int i;
  4779. unsigned int drv_enc;
  4780. if (wpa_s->p2p_mgmt)
  4781. return 1; /* no normal network profiles on p2p_mgmt interface */
  4782. if (ssid == NULL)
  4783. return 1;
  4784. if (ssid->disabled)
  4785. return 1;
  4786. if (wpa_s->drv_capa_known)
  4787. drv_enc = wpa_s->drv_enc;
  4788. else
  4789. drv_enc = (unsigned int) -1;
  4790. for (i = 0; i < NUM_WEP_KEYS; i++) {
  4791. size_t len = ssid->wep_key_len[i];
  4792. if (len == 0)
  4793. continue;
  4794. if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
  4795. continue;
  4796. if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
  4797. continue;
  4798. if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
  4799. continue;
  4800. return 1; /* invalid WEP key */
  4801. }
  4802. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
  4803. (!ssid->passphrase || ssid->ssid_len != 0) && !ssid->ext_psk &&
  4804. !ssid->mem_only_psk)
  4805. return 1;
  4806. return 0;
  4807. }
  4808. int wpas_get_ssid_pmf(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  4809. {
  4810. #ifdef CONFIG_IEEE80211W
  4811. if (ssid == NULL || ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT) {
  4812. if (wpa_s->conf->pmf == MGMT_FRAME_PROTECTION_OPTIONAL &&
  4813. !(wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_BIP)) {
  4814. /*
  4815. * Driver does not support BIP -- ignore pmf=1 default
  4816. * since the connection with PMF would fail and the
  4817. * configuration does not require PMF to be enabled.
  4818. */
  4819. return NO_MGMT_FRAME_PROTECTION;
  4820. }
  4821. return wpa_s->conf->pmf;
  4822. }
  4823. return ssid->ieee80211w;
  4824. #else /* CONFIG_IEEE80211W */
  4825. return NO_MGMT_FRAME_PROTECTION;
  4826. #endif /* CONFIG_IEEE80211W */
  4827. }
  4828. int wpas_is_p2p_prioritized(struct wpa_supplicant *wpa_s)
  4829. {
  4830. if (wpa_s->global->conc_pref == WPA_CONC_PREF_P2P)
  4831. return 1;
  4832. if (wpa_s->global->conc_pref == WPA_CONC_PREF_STA)
  4833. return 0;
  4834. return -1;
  4835. }
  4836. void wpas_auth_failed(struct wpa_supplicant *wpa_s, char *reason)
  4837. {
  4838. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4839. int dur;
  4840. struct os_reltime now;
  4841. if (ssid == NULL) {
  4842. wpa_printf(MSG_DEBUG, "Authentication failure but no known "
  4843. "SSID block");
  4844. return;
  4845. }
  4846. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
  4847. return;
  4848. ssid->auth_failures++;
  4849. #ifdef CONFIG_P2P
  4850. if (ssid->p2p_group &&
  4851. (wpa_s->p2p_in_provisioning || wpa_s->show_group_started)) {
  4852. /*
  4853. * Skip the wait time since there is a short timeout on the
  4854. * connection to a P2P group.
  4855. */
  4856. return;
  4857. }
  4858. #endif /* CONFIG_P2P */
  4859. if (ssid->auth_failures > 50)
  4860. dur = 300;
  4861. else if (ssid->auth_failures > 10)
  4862. dur = 120;
  4863. else if (ssid->auth_failures > 5)
  4864. dur = 90;
  4865. else if (ssid->auth_failures > 3)
  4866. dur = 60;
  4867. else if (ssid->auth_failures > 2)
  4868. dur = 30;
  4869. else if (ssid->auth_failures > 1)
  4870. dur = 20;
  4871. else
  4872. dur = 10;
  4873. if (ssid->auth_failures > 1 &&
  4874. wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt))
  4875. dur += os_random() % (ssid->auth_failures * 10);
  4876. os_get_reltime(&now);
  4877. if (now.sec + dur <= ssid->disabled_until.sec)
  4878. return;
  4879. ssid->disabled_until.sec = now.sec + dur;
  4880. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TEMP_DISABLED
  4881. "id=%d ssid=\"%s\" auth_failures=%u duration=%d reason=%s",
  4882. ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  4883. ssid->auth_failures, dur, reason);
  4884. }
  4885. void wpas_clear_temp_disabled(struct wpa_supplicant *wpa_s,
  4886. struct wpa_ssid *ssid, int clear_failures)
  4887. {
  4888. if (ssid == NULL)
  4889. return;
  4890. if (ssid->disabled_until.sec) {
  4891. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REENABLED
  4892. "id=%d ssid=\"%s\"",
  4893. ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  4894. }
  4895. ssid->disabled_until.sec = 0;
  4896. ssid->disabled_until.usec = 0;
  4897. if (clear_failures)
  4898. ssid->auth_failures = 0;
  4899. }
  4900. int disallowed_bssid(struct wpa_supplicant *wpa_s, const u8 *bssid)
  4901. {
  4902. size_t i;
  4903. if (wpa_s->disallow_aps_bssid == NULL)
  4904. return 0;
  4905. for (i = 0; i < wpa_s->disallow_aps_bssid_count; i++) {
  4906. if (os_memcmp(wpa_s->disallow_aps_bssid + i * ETH_ALEN,
  4907. bssid, ETH_ALEN) == 0)
  4908. return 1;
  4909. }
  4910. return 0;
  4911. }
  4912. int disallowed_ssid(struct wpa_supplicant *wpa_s, const u8 *ssid,
  4913. size_t ssid_len)
  4914. {
  4915. size_t i;
  4916. if (wpa_s->disallow_aps_ssid == NULL || ssid == NULL)
  4917. return 0;
  4918. for (i = 0; i < wpa_s->disallow_aps_ssid_count; i++) {
  4919. struct wpa_ssid_value *s = &wpa_s->disallow_aps_ssid[i];
  4920. if (ssid_len == s->ssid_len &&
  4921. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  4922. return 1;
  4923. }
  4924. return 0;
  4925. }
  4926. /**
  4927. * wpas_request_connection - Request a new connection
  4928. * @wpa_s: Pointer to the network interface
  4929. *
  4930. * This function is used to request a new connection to be found. It will mark
  4931. * the interface to allow reassociation and request a new scan to find a
  4932. * suitable network to connect to.
  4933. */
  4934. void wpas_request_connection(struct wpa_supplicant *wpa_s)
  4935. {
  4936. wpa_s->normal_scans = 0;
  4937. wpa_s->scan_req = NORMAL_SCAN_REQ;
  4938. wpa_supplicant_reinit_autoscan(wpa_s);
  4939. wpa_s->extra_blacklist_count = 0;
  4940. wpa_s->disconnected = 0;
  4941. wpa_s->reassociate = 1;
  4942. if (wpa_supplicant_fast_associate(wpa_s) != 1)
  4943. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4944. else
  4945. wpa_s->reattach = 0;
  4946. }
  4947. void dump_freq_data(struct wpa_supplicant *wpa_s, const char *title,
  4948. struct wpa_used_freq_data *freqs_data,
  4949. unsigned int len)
  4950. {
  4951. unsigned int i;
  4952. wpa_dbg(wpa_s, MSG_DEBUG, "Shared frequencies (len=%u): %s",
  4953. len, title);
  4954. for (i = 0; i < len; i++) {
  4955. struct wpa_used_freq_data *cur = &freqs_data[i];
  4956. wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d, flags=0x%X",
  4957. i, cur->freq, cur->flags);
  4958. }
  4959. }
  4960. /*
  4961. * Find the operating frequencies of any of the virtual interfaces that
  4962. * are using the same radio as the current interface, and in addition, get
  4963. * information about the interface types that are using the frequency.
  4964. */
  4965. int get_shared_radio_freqs_data(struct wpa_supplicant *wpa_s,
  4966. struct wpa_used_freq_data *freqs_data,
  4967. unsigned int len)
  4968. {
  4969. struct wpa_supplicant *ifs;
  4970. u8 bssid[ETH_ALEN];
  4971. int freq;
  4972. unsigned int idx = 0, i;
  4973. wpa_dbg(wpa_s, MSG_DEBUG,
  4974. "Determining shared radio frequencies (max len %u)", len);
  4975. os_memset(freqs_data, 0, sizeof(struct wpa_used_freq_data) * len);
  4976. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  4977. radio_list) {
  4978. if (idx == len)
  4979. break;
  4980. if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
  4981. continue;
  4982. if (ifs->current_ssid->mode == WPAS_MODE_AP ||
  4983. ifs->current_ssid->mode == WPAS_MODE_P2P_GO ||
  4984. ifs->current_ssid->mode == WPAS_MODE_MESH)
  4985. freq = ifs->current_ssid->frequency;
  4986. else if (wpa_drv_get_bssid(ifs, bssid) == 0)
  4987. freq = ifs->assoc_freq;
  4988. else
  4989. continue;
  4990. /* Hold only distinct freqs */
  4991. for (i = 0; i < idx; i++)
  4992. if (freqs_data[i].freq == freq)
  4993. break;
  4994. if (i == idx)
  4995. freqs_data[idx++].freq = freq;
  4996. if (ifs->current_ssid->mode == WPAS_MODE_INFRA) {
  4997. freqs_data[i].flags |= ifs->current_ssid->p2p_group ?
  4998. WPA_FREQ_USED_BY_P2P_CLIENT :
  4999. WPA_FREQ_USED_BY_INFRA_STATION;
  5000. }
  5001. }
  5002. dump_freq_data(wpa_s, "completed iteration", freqs_data, idx);
  5003. return idx;
  5004. }
  5005. /*
  5006. * Find the operating frequencies of any of the virtual interfaces that
  5007. * are using the same radio as the current interface.
  5008. */
  5009. int get_shared_radio_freqs(struct wpa_supplicant *wpa_s,
  5010. int *freq_array, unsigned int len)
  5011. {
  5012. struct wpa_used_freq_data *freqs_data;
  5013. int num, i;
  5014. os_memset(freq_array, 0, sizeof(int) * len);
  5015. freqs_data = os_calloc(len, sizeof(struct wpa_used_freq_data));
  5016. if (!freqs_data)
  5017. return -1;
  5018. num = get_shared_radio_freqs_data(wpa_s, freqs_data, len);
  5019. for (i = 0; i < num; i++)
  5020. freq_array[i] = freqs_data[i].freq;
  5021. os_free(freqs_data);
  5022. return num;
  5023. }
  5024. static void wpas_rrm_neighbor_rep_timeout_handler(void *data, void *user_ctx)
  5025. {
  5026. struct rrm_data *rrm = data;
  5027. if (!rrm->notify_neighbor_rep) {
  5028. wpa_printf(MSG_ERROR,
  5029. "RRM: Unexpected neighbor report timeout");
  5030. return;
  5031. }
  5032. wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report - NONE");
  5033. rrm->notify_neighbor_rep(rrm->neighbor_rep_cb_ctx, NULL);
  5034. rrm->notify_neighbor_rep = NULL;
  5035. rrm->neighbor_rep_cb_ctx = NULL;
  5036. }
  5037. /*
  5038. * wpas_rrm_reset - Clear and reset all RRM data in wpa_supplicant
  5039. * @wpa_s: Pointer to wpa_supplicant
  5040. */
  5041. void wpas_rrm_reset(struct wpa_supplicant *wpa_s)
  5042. {
  5043. wpa_s->rrm.rrm_used = 0;
  5044. eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
  5045. NULL);
  5046. if (wpa_s->rrm.notify_neighbor_rep)
  5047. wpas_rrm_neighbor_rep_timeout_handler(&wpa_s->rrm, NULL);
  5048. wpa_s->rrm.next_neighbor_rep_token = 1;
  5049. }
  5050. /*
  5051. * wpas_rrm_process_neighbor_rep - Handle incoming neighbor report
  5052. * @wpa_s: Pointer to wpa_supplicant
  5053. * @report: Neighbor report buffer, prefixed by a 1-byte dialog token
  5054. * @report_len: Length of neighbor report buffer
  5055. */
  5056. void wpas_rrm_process_neighbor_rep(struct wpa_supplicant *wpa_s,
  5057. const u8 *report, size_t report_len)
  5058. {
  5059. struct wpabuf *neighbor_rep;
  5060. wpa_hexdump(MSG_DEBUG, "RRM: New Neighbor Report", report, report_len);
  5061. if (report_len < 1)
  5062. return;
  5063. if (report[0] != wpa_s->rrm.next_neighbor_rep_token - 1) {
  5064. wpa_printf(MSG_DEBUG,
  5065. "RRM: Discarding neighbor report with token %d (expected %d)",
  5066. report[0], wpa_s->rrm.next_neighbor_rep_token - 1);
  5067. return;
  5068. }
  5069. eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
  5070. NULL);
  5071. if (!wpa_s->rrm.notify_neighbor_rep) {
  5072. wpa_printf(MSG_ERROR, "RRM: Unexpected neighbor report");
  5073. return;
  5074. }
  5075. /* skipping the first byte, which is only an id (dialog token) */
  5076. neighbor_rep = wpabuf_alloc(report_len - 1);
  5077. if (neighbor_rep == NULL)
  5078. return;
  5079. wpabuf_put_data(neighbor_rep, report + 1, report_len - 1);
  5080. wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report (token = %d)",
  5081. report[0]);
  5082. wpa_s->rrm.notify_neighbor_rep(wpa_s->rrm.neighbor_rep_cb_ctx,
  5083. neighbor_rep);
  5084. wpa_s->rrm.notify_neighbor_rep = NULL;
  5085. wpa_s->rrm.neighbor_rep_cb_ctx = NULL;
  5086. }
  5087. #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
  5088. /* Workaround different, undefined for Windows, error codes used here */
  5089. #define ENOTCONN -1
  5090. #define EOPNOTSUPP -1
  5091. #define ECANCELED -1
  5092. #endif
  5093. /**
  5094. * wpas_rrm_send_neighbor_rep_request - Request a neighbor report from our AP
  5095. * @wpa_s: Pointer to wpa_supplicant
  5096. * @ssid: if not null, this is sent in the request. Otherwise, no SSID IE
  5097. * is sent in the request.
  5098. * @cb: Callback function to be called once the requested report arrives, or
  5099. * timed out after RRM_NEIGHBOR_REPORT_TIMEOUT seconds.
  5100. * In the former case, 'neighbor_rep' is a newly allocated wpabuf, and it's
  5101. * the requester's responsibility to free it.
  5102. * In the latter case NULL will be sent in 'neighbor_rep'.
  5103. * @cb_ctx: Context value to send the callback function
  5104. * Returns: 0 in case of success, negative error code otherwise
  5105. *
  5106. * In case there is a previous request which has not been answered yet, the
  5107. * new request fails. The caller may retry after RRM_NEIGHBOR_REPORT_TIMEOUT.
  5108. * Request must contain a callback function.
  5109. */
  5110. int wpas_rrm_send_neighbor_rep_request(struct wpa_supplicant *wpa_s,
  5111. const struct wpa_ssid *ssid,
  5112. void (*cb)(void *ctx,
  5113. struct wpabuf *neighbor_rep),
  5114. void *cb_ctx)
  5115. {
  5116. struct wpabuf *buf;
  5117. const u8 *rrm_ie;
  5118. if (wpa_s->wpa_state != WPA_COMPLETED || wpa_s->current_ssid == NULL) {
  5119. wpa_printf(MSG_DEBUG, "RRM: No connection, no RRM.");
  5120. return -ENOTCONN;
  5121. }
  5122. if (!wpa_s->rrm.rrm_used) {
  5123. wpa_printf(MSG_DEBUG, "RRM: No RRM in current connection.");
  5124. return -EOPNOTSUPP;
  5125. }
  5126. rrm_ie = wpa_bss_get_ie(wpa_s->current_bss,
  5127. WLAN_EID_RRM_ENABLED_CAPABILITIES);
  5128. if (!rrm_ie || !(wpa_s->current_bss->caps & IEEE80211_CAP_RRM) ||
  5129. !(rrm_ie[2] & WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
  5130. wpa_printf(MSG_DEBUG,
  5131. "RRM: No network support for Neighbor Report.");
  5132. return -EOPNOTSUPP;
  5133. }
  5134. if (!cb) {
  5135. wpa_printf(MSG_DEBUG,
  5136. "RRM: Neighbor Report request must provide a callback.");
  5137. return -EINVAL;
  5138. }
  5139. /* Refuse if there's a live request */
  5140. if (wpa_s->rrm.notify_neighbor_rep) {
  5141. wpa_printf(MSG_DEBUG,
  5142. "RRM: Currently handling previous Neighbor Report.");
  5143. return -EBUSY;
  5144. }
  5145. /* 3 = action category + action code + dialog token */
  5146. buf = wpabuf_alloc(3 + (ssid ? 2 + ssid->ssid_len : 0));
  5147. if (buf == NULL) {
  5148. wpa_printf(MSG_DEBUG,
  5149. "RRM: Failed to allocate Neighbor Report Request");
  5150. return -ENOMEM;
  5151. }
  5152. wpa_printf(MSG_DEBUG, "RRM: Neighbor report request (for %s), token=%d",
  5153. (ssid ? wpa_ssid_txt(ssid->ssid, ssid->ssid_len) : ""),
  5154. wpa_s->rrm.next_neighbor_rep_token);
  5155. wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
  5156. wpabuf_put_u8(buf, WLAN_RRM_NEIGHBOR_REPORT_REQUEST);
  5157. wpabuf_put_u8(buf, wpa_s->rrm.next_neighbor_rep_token);
  5158. if (ssid) {
  5159. wpabuf_put_u8(buf, WLAN_EID_SSID);
  5160. wpabuf_put_u8(buf, ssid->ssid_len);
  5161. wpabuf_put_data(buf, ssid->ssid, ssid->ssid_len);
  5162. }
  5163. wpa_s->rrm.next_neighbor_rep_token++;
  5164. if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  5165. wpa_s->own_addr, wpa_s->bssid,
  5166. wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
  5167. wpa_printf(MSG_DEBUG,
  5168. "RRM: Failed to send Neighbor Report Request");
  5169. wpabuf_free(buf);
  5170. return -ECANCELED;
  5171. }
  5172. wpa_s->rrm.neighbor_rep_cb_ctx = cb_ctx;
  5173. wpa_s->rrm.notify_neighbor_rep = cb;
  5174. eloop_register_timeout(RRM_NEIGHBOR_REPORT_TIMEOUT, 0,
  5175. wpas_rrm_neighbor_rep_timeout_handler,
  5176. &wpa_s->rrm, NULL);
  5177. wpabuf_free(buf);
  5178. return 0;
  5179. }
  5180. void wpas_rrm_handle_link_measurement_request(struct wpa_supplicant *wpa_s,
  5181. const u8 *src,
  5182. const u8 *frame, size_t len,
  5183. int rssi)
  5184. {
  5185. struct wpabuf *buf;
  5186. const struct rrm_link_measurement_request *req;
  5187. struct rrm_link_measurement_report report;
  5188. if (wpa_s->wpa_state != WPA_COMPLETED) {
  5189. wpa_printf(MSG_INFO,
  5190. "RRM: Ignoring link measurement request. Not associated");
  5191. return;
  5192. }
  5193. if (!wpa_s->rrm.rrm_used) {
  5194. wpa_printf(MSG_INFO,
  5195. "RRM: Ignoring link measurement request. Not RRM network");
  5196. return;
  5197. }
  5198. if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)) {
  5199. wpa_printf(MSG_INFO,
  5200. "RRM: Measurement report failed. TX power insertion not supported");
  5201. return;
  5202. }
  5203. req = (const struct rrm_link_measurement_request *) frame;
  5204. if (len < sizeof(*req)) {
  5205. wpa_printf(MSG_INFO,
  5206. "RRM: Link measurement report failed. Request too short");
  5207. return;
  5208. }
  5209. os_memset(&report, 0, sizeof(report));
  5210. report.tpc.eid = WLAN_EID_TPC_REPORT;
  5211. report.tpc.len = 2;
  5212. report.rsni = 255; /* 255 indicates that RSNI is not available */
  5213. report.dialog_token = req->dialog_token;
  5214. /*
  5215. * It's possible to estimate RCPI based on RSSI in dBm. This
  5216. * calculation will not reflect the correct value for high rates,
  5217. * but it's good enough for Action frames which are transmitted
  5218. * with up to 24 Mbps rates.
  5219. */
  5220. if (!rssi)
  5221. report.rcpi = 255; /* not available */
  5222. else if (rssi < -110)
  5223. report.rcpi = 0;
  5224. else if (rssi > 0)
  5225. report.rcpi = 220;
  5226. else
  5227. report.rcpi = (rssi + 110) * 2;
  5228. /* action_category + action_code */
  5229. buf = wpabuf_alloc(2 + sizeof(report));
  5230. if (buf == NULL) {
  5231. wpa_printf(MSG_ERROR,
  5232. "RRM: Link measurement report failed. Buffer allocation failed");
  5233. return;
  5234. }
  5235. wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
  5236. wpabuf_put_u8(buf, WLAN_RRM_LINK_MEASUREMENT_REPORT);
  5237. wpabuf_put_data(buf, &report, sizeof(report));
  5238. wpa_hexdump(MSG_DEBUG, "RRM: Link measurement report:",
  5239. wpabuf_head(buf), wpabuf_len(buf));
  5240. if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, src,
  5241. wpa_s->own_addr, wpa_s->bssid,
  5242. wpabuf_head(buf), wpabuf_len(buf), 0)) {
  5243. wpa_printf(MSG_ERROR,
  5244. "RRM: Link measurement report failed. Send action failed");
  5245. }
  5246. wpabuf_free(buf);
  5247. }
  5248. struct wpa_supplicant *
  5249. wpas_vendor_elem(struct wpa_supplicant *wpa_s, enum wpa_vendor_elem_frame frame)
  5250. {
  5251. switch (frame) {
  5252. #ifdef CONFIG_P2P
  5253. case VENDOR_ELEM_PROBE_REQ_P2P:
  5254. case VENDOR_ELEM_PROBE_RESP_P2P:
  5255. case VENDOR_ELEM_PROBE_RESP_P2P_GO:
  5256. case VENDOR_ELEM_BEACON_P2P_GO:
  5257. case VENDOR_ELEM_P2P_PD_REQ:
  5258. case VENDOR_ELEM_P2P_PD_RESP:
  5259. case VENDOR_ELEM_P2P_GO_NEG_REQ:
  5260. case VENDOR_ELEM_P2P_GO_NEG_RESP:
  5261. case VENDOR_ELEM_P2P_GO_NEG_CONF:
  5262. case VENDOR_ELEM_P2P_INV_REQ:
  5263. case VENDOR_ELEM_P2P_INV_RESP:
  5264. case VENDOR_ELEM_P2P_ASSOC_REQ:
  5265. case VENDOR_ELEM_P2P_ASSOC_RESP:
  5266. return wpa_s->parent;
  5267. #endif /* CONFIG_P2P */
  5268. default:
  5269. return wpa_s;
  5270. }
  5271. }
  5272. void wpas_vendor_elem_update(struct wpa_supplicant *wpa_s)
  5273. {
  5274. unsigned int i;
  5275. char buf[30];
  5276. wpa_printf(MSG_DEBUG, "Update vendor elements");
  5277. for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
  5278. if (wpa_s->vendor_elem[i]) {
  5279. int res;
  5280. res = os_snprintf(buf, sizeof(buf), "frame[%u]", i);
  5281. if (!os_snprintf_error(sizeof(buf), res)) {
  5282. wpa_hexdump_buf(MSG_DEBUG, buf,
  5283. wpa_s->vendor_elem[i]);
  5284. }
  5285. }
  5286. }
  5287. #ifdef CONFIG_P2P
  5288. if (wpa_s->parent == wpa_s &&
  5289. wpa_s->global->p2p &&
  5290. !wpa_s->global->p2p_disabled)
  5291. p2p_set_vendor_elems(wpa_s->global->p2p, wpa_s->vendor_elem);
  5292. #endif /* CONFIG_P2P */
  5293. }
  5294. int wpas_vendor_elem_remove(struct wpa_supplicant *wpa_s, int frame,
  5295. const u8 *elem, size_t len)
  5296. {
  5297. u8 *ie, *end;
  5298. ie = wpabuf_mhead_u8(wpa_s->vendor_elem[frame]);
  5299. end = ie + wpabuf_len(wpa_s->vendor_elem[frame]);
  5300. for (; ie + 1 < end; ie += 2 + ie[1]) {
  5301. if (ie + len > end)
  5302. break;
  5303. if (os_memcmp(ie, elem, len) != 0)
  5304. continue;
  5305. if (wpabuf_len(wpa_s->vendor_elem[frame]) == len) {
  5306. wpabuf_free(wpa_s->vendor_elem[frame]);
  5307. wpa_s->vendor_elem[frame] = NULL;
  5308. } else {
  5309. os_memmove(ie, ie + len, end - (ie + len));
  5310. wpa_s->vendor_elem[frame]->used -= len;
  5311. }
  5312. wpas_vendor_elem_update(wpa_s);
  5313. return 0;
  5314. }
  5315. return -1;
  5316. }