wpa_supplicant.c 184 KB

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