wpa_supplicant.c 188 KB

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