wpa_supplicant.c 168 KB

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