ctrl_iface.c 149 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350
  1. /*
  2. * WPA Supplicant / Control interface (shared code for all backends)
  3. * Copyright (c) 2004-2013, 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. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "utils/uuid.h"
  12. #include "common/version.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "common/ieee802_11_common.h"
  15. #include "common/wpa_ctrl.h"
  16. #include "eap_peer/eap.h"
  17. #include "eapol_supp/eapol_supp_sm.h"
  18. #include "rsn_supp/wpa.h"
  19. #include "rsn_supp/preauth.h"
  20. #include "rsn_supp/pmksa_cache.h"
  21. #include "l2_packet/l2_packet.h"
  22. #include "wps/wps.h"
  23. #include "config.h"
  24. #include "wpa_supplicant_i.h"
  25. #include "driver_i.h"
  26. #include "wps_supplicant.h"
  27. #include "ibss_rsn.h"
  28. #include "ap.h"
  29. #include "p2p_supplicant.h"
  30. #include "p2p/p2p.h"
  31. #include "hs20_supplicant.h"
  32. #include "wifi_display.h"
  33. #include "notify.h"
  34. #include "bss.h"
  35. #include "scan.h"
  36. #include "ctrl_iface.h"
  37. #include "interworking.h"
  38. #include "blacklist.h"
  39. #include "autoscan.h"
  40. #include "wnm_sta.h"
  41. extern struct wpa_driver_ops *wpa_drivers[];
  42. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  43. char *buf, int len);
  44. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  45. char *buf, int len);
  46. static int pno_start(struct wpa_supplicant *wpa_s)
  47. {
  48. int ret, interval;
  49. size_t i, num_ssid;
  50. struct wpa_ssid *ssid;
  51. struct wpa_driver_scan_params params;
  52. if (wpa_s->pno)
  53. return 0;
  54. if ((wpa_s->wpa_state > WPA_SCANNING) &&
  55. (wpa_s->wpa_state <= WPA_COMPLETED)) {
  56. wpa_printf(MSG_ERROR, "PNO: In assoc process");
  57. return -EAGAIN;
  58. }
  59. if (wpa_s->wpa_state == WPA_SCANNING) {
  60. wpa_supplicant_cancel_sched_scan(wpa_s);
  61. wpa_supplicant_cancel_scan(wpa_s);
  62. }
  63. os_memset(&params, 0, sizeof(params));
  64. num_ssid = 0;
  65. ssid = wpa_s->conf->ssid;
  66. while (ssid) {
  67. if (!wpas_network_disabled(wpa_s, ssid))
  68. num_ssid++;
  69. ssid = ssid->next;
  70. }
  71. if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
  72. wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
  73. "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
  74. num_ssid = WPAS_MAX_SCAN_SSIDS;
  75. }
  76. if (num_ssid == 0) {
  77. wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
  78. return -1;
  79. }
  80. params.filter_ssids = os_malloc(sizeof(struct wpa_driver_scan_filter) *
  81. num_ssid);
  82. if (params.filter_ssids == NULL)
  83. return -1;
  84. i = 0;
  85. ssid = wpa_s->conf->ssid;
  86. while (ssid) {
  87. if (!wpas_network_disabled(wpa_s, ssid)) {
  88. params.ssids[i].ssid = ssid->ssid;
  89. params.ssids[i].ssid_len = ssid->ssid_len;
  90. params.num_ssids++;
  91. os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
  92. ssid->ssid_len);
  93. params.filter_ssids[i].ssid_len = ssid->ssid_len;
  94. params.num_filter_ssids++;
  95. i++;
  96. if (i == num_ssid)
  97. break;
  98. }
  99. ssid = ssid->next;
  100. }
  101. if (wpa_s->conf->filter_rssi)
  102. params.filter_rssi = wpa_s->conf->filter_rssi;
  103. interval = wpa_s->conf->sched_scan_interval ?
  104. wpa_s->conf->sched_scan_interval : 10;
  105. ret = wpa_supplicant_start_sched_scan(wpa_s, &params, interval);
  106. os_free(params.filter_ssids);
  107. if (ret == 0)
  108. wpa_s->pno = 1;
  109. return ret;
  110. }
  111. static int pno_stop(struct wpa_supplicant *wpa_s)
  112. {
  113. int ret = 0;
  114. if (wpa_s->pno) {
  115. wpa_s->pno = 0;
  116. ret = wpa_supplicant_stop_sched_scan(wpa_s);
  117. }
  118. if (wpa_s->wpa_state == WPA_SCANNING)
  119. wpa_supplicant_req_scan(wpa_s, 0, 0);
  120. return ret;
  121. }
  122. static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
  123. {
  124. char *pos;
  125. u8 addr[ETH_ALEN], *filter = NULL, *n;
  126. size_t count = 0;
  127. pos = val;
  128. while (pos) {
  129. if (*pos == '\0')
  130. break;
  131. if (hwaddr_aton(pos, addr)) {
  132. os_free(filter);
  133. return -1;
  134. }
  135. n = os_realloc_array(filter, count + 1, ETH_ALEN);
  136. if (n == NULL) {
  137. os_free(filter);
  138. return -1;
  139. }
  140. filter = n;
  141. os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
  142. count++;
  143. pos = os_strchr(pos, ' ');
  144. if (pos)
  145. pos++;
  146. }
  147. wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
  148. os_free(wpa_s->bssid_filter);
  149. wpa_s->bssid_filter = filter;
  150. wpa_s->bssid_filter_count = count;
  151. return 0;
  152. }
  153. static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
  154. {
  155. char *pos;
  156. u8 addr[ETH_ALEN], *bssid = NULL, *n;
  157. struct wpa_ssid_value *ssid = NULL, *ns;
  158. size_t count = 0, ssid_count = 0;
  159. struct wpa_ssid *c;
  160. /*
  161. * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
  162. * SSID_SPEC ::= ssid <SSID_HEX>
  163. * BSSID_SPEC ::= bssid <BSSID_HEX>
  164. */
  165. pos = val;
  166. while (pos) {
  167. if (*pos == '\0')
  168. break;
  169. if (os_strncmp(pos, "bssid ", 6) == 0) {
  170. int res;
  171. pos += 6;
  172. res = hwaddr_aton2(pos, addr);
  173. if (res < 0) {
  174. os_free(ssid);
  175. os_free(bssid);
  176. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  177. "BSSID value '%s'", pos);
  178. return -1;
  179. }
  180. pos += res;
  181. n = os_realloc_array(bssid, count + 1, ETH_ALEN);
  182. if (n == NULL) {
  183. os_free(ssid);
  184. os_free(bssid);
  185. return -1;
  186. }
  187. bssid = n;
  188. os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
  189. count++;
  190. } else if (os_strncmp(pos, "ssid ", 5) == 0) {
  191. char *end;
  192. pos += 5;
  193. end = pos;
  194. while (*end) {
  195. if (*end == '\0' || *end == ' ')
  196. break;
  197. end++;
  198. }
  199. ns = os_realloc_array(ssid, ssid_count + 1,
  200. sizeof(struct wpa_ssid_value));
  201. if (ns == NULL) {
  202. os_free(ssid);
  203. os_free(bssid);
  204. return -1;
  205. }
  206. ssid = ns;
  207. if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
  208. hexstr2bin(pos, ssid[ssid_count].ssid,
  209. (end - pos) / 2) < 0) {
  210. os_free(ssid);
  211. os_free(bssid);
  212. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  213. "SSID value '%s'", pos);
  214. return -1;
  215. }
  216. ssid[ssid_count].ssid_len = (end - pos) / 2;
  217. wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
  218. ssid[ssid_count].ssid,
  219. ssid[ssid_count].ssid_len);
  220. ssid_count++;
  221. pos = end;
  222. } else {
  223. wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
  224. "'%s'", pos);
  225. os_free(ssid);
  226. os_free(bssid);
  227. return -1;
  228. }
  229. pos = os_strchr(pos, ' ');
  230. if (pos)
  231. pos++;
  232. }
  233. wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
  234. os_free(wpa_s->disallow_aps_bssid);
  235. wpa_s->disallow_aps_bssid = bssid;
  236. wpa_s->disallow_aps_bssid_count = count;
  237. wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
  238. os_free(wpa_s->disallow_aps_ssid);
  239. wpa_s->disallow_aps_ssid = ssid;
  240. wpa_s->disallow_aps_ssid_count = ssid_count;
  241. if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
  242. return 0;
  243. c = wpa_s->current_ssid;
  244. if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
  245. return 0;
  246. if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
  247. !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
  248. return 0;
  249. wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
  250. "because current AP was marked disallowed");
  251. #ifdef CONFIG_SME
  252. wpa_s->sme.prev_bssid_set = 0;
  253. #endif /* CONFIG_SME */
  254. wpa_s->reassociate = 1;
  255. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  256. wpa_supplicant_req_scan(wpa_s, 0, 0);
  257. return 0;
  258. }
  259. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  260. char *cmd)
  261. {
  262. char *value;
  263. int ret = 0;
  264. value = os_strchr(cmd, ' ');
  265. if (value == NULL)
  266. return -1;
  267. *value++ = '\0';
  268. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  269. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  270. eapol_sm_configure(wpa_s->eapol,
  271. atoi(value), -1, -1, -1);
  272. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  273. eapol_sm_configure(wpa_s->eapol,
  274. -1, atoi(value), -1, -1);
  275. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  276. eapol_sm_configure(wpa_s->eapol,
  277. -1, -1, atoi(value), -1);
  278. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  279. eapol_sm_configure(wpa_s->eapol,
  280. -1, -1, -1, atoi(value));
  281. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  282. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  283. atoi(value)))
  284. ret = -1;
  285. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  286. 0) {
  287. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  288. atoi(value)))
  289. ret = -1;
  290. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  291. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
  292. ret = -1;
  293. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  294. wpa_s->wps_fragment_size = atoi(value);
  295. #ifdef CONFIG_WPS_TESTING
  296. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  297. long int val;
  298. val = strtol(value, NULL, 0);
  299. if (val < 0 || val > 0xff) {
  300. ret = -1;
  301. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  302. "wps_version_number %ld", val);
  303. } else {
  304. wps_version_number = val;
  305. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  306. "version %u.%u",
  307. (wps_version_number & 0xf0) >> 4,
  308. wps_version_number & 0x0f);
  309. }
  310. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  311. wps_testing_dummy_cred = atoi(value);
  312. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  313. wps_testing_dummy_cred);
  314. #endif /* CONFIG_WPS_TESTING */
  315. } else if (os_strcasecmp(cmd, "ampdu") == 0) {
  316. if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
  317. ret = -1;
  318. #ifdef CONFIG_TDLS
  319. #ifdef CONFIG_TDLS_TESTING
  320. } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
  321. extern unsigned int tdls_testing;
  322. tdls_testing = strtol(value, NULL, 0);
  323. wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
  324. #endif /* CONFIG_TDLS_TESTING */
  325. } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
  326. int disabled = atoi(value);
  327. wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
  328. if (disabled) {
  329. if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
  330. ret = -1;
  331. } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
  332. ret = -1;
  333. wpa_tdls_enable(wpa_s->wpa, !disabled);
  334. #endif /* CONFIG_TDLS */
  335. } else if (os_strcasecmp(cmd, "pno") == 0) {
  336. if (atoi(value))
  337. ret = pno_start(wpa_s);
  338. else
  339. ret = pno_stop(wpa_s);
  340. } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
  341. int disabled = atoi(value);
  342. if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
  343. ret = -1;
  344. else if (disabled)
  345. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  346. } else if (os_strcasecmp(cmd, "uapsd") == 0) {
  347. if (os_strcmp(value, "disable") == 0)
  348. wpa_s->set_sta_uapsd = 0;
  349. else {
  350. int be, bk, vi, vo;
  351. char *pos;
  352. /* format: BE,BK,VI,VO;max SP Length */
  353. be = atoi(value);
  354. pos = os_strchr(value, ',');
  355. if (pos == NULL)
  356. return -1;
  357. pos++;
  358. bk = atoi(pos);
  359. pos = os_strchr(pos, ',');
  360. if (pos == NULL)
  361. return -1;
  362. pos++;
  363. vi = atoi(pos);
  364. pos = os_strchr(pos, ',');
  365. if (pos == NULL)
  366. return -1;
  367. pos++;
  368. vo = atoi(pos);
  369. /* ignore max SP Length for now */
  370. wpa_s->set_sta_uapsd = 1;
  371. wpa_s->sta_uapsd = 0;
  372. if (be)
  373. wpa_s->sta_uapsd |= BIT(0);
  374. if (bk)
  375. wpa_s->sta_uapsd |= BIT(1);
  376. if (vi)
  377. wpa_s->sta_uapsd |= BIT(2);
  378. if (vo)
  379. wpa_s->sta_uapsd |= BIT(3);
  380. }
  381. } else if (os_strcasecmp(cmd, "ps") == 0) {
  382. ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
  383. #ifdef CONFIG_WIFI_DISPLAY
  384. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  385. wifi_display_enable(wpa_s->global, !!atoi(value));
  386. #endif /* CONFIG_WIFI_DISPLAY */
  387. } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
  388. ret = set_bssid_filter(wpa_s, value);
  389. } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
  390. ret = set_disallow_aps(wpa_s, value);
  391. } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
  392. wpa_s->no_keep_alive = !!atoi(value);
  393. } else {
  394. value[-1] = '=';
  395. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  396. if (ret == 0)
  397. wpa_supplicant_update_config(wpa_s);
  398. }
  399. return ret;
  400. }
  401. static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
  402. char *cmd, char *buf, size_t buflen)
  403. {
  404. int res = -1;
  405. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  406. if (os_strcmp(cmd, "version") == 0) {
  407. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  408. } else if (os_strcasecmp(cmd, "country") == 0) {
  409. if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
  410. res = os_snprintf(buf, buflen, "%c%c",
  411. wpa_s->conf->country[0],
  412. wpa_s->conf->country[1]);
  413. #ifdef CONFIG_WIFI_DISPLAY
  414. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  415. res = os_snprintf(buf, buflen, "%d",
  416. wpa_s->global->wifi_display);
  417. if (res < 0 || (unsigned int) res >= buflen)
  418. return -1;
  419. return res;
  420. #endif /* CONFIG_WIFI_DISPLAY */
  421. #ifdef CONFIG_TESTING_GET_GTK
  422. } else if (os_strcmp(cmd, "gtk") == 0) {
  423. if (wpa_s->last_gtk_len == 0)
  424. return -1;
  425. res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
  426. wpa_s->last_gtk_len);
  427. return res;
  428. #endif /* CONFIG_TESTING_GET_GTK */
  429. }
  430. if (res < 0 || (unsigned int) res >= buflen)
  431. return -1;
  432. return res;
  433. }
  434. #ifdef IEEE8021X_EAPOL
  435. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  436. char *addr)
  437. {
  438. u8 bssid[ETH_ALEN];
  439. struct wpa_ssid *ssid = wpa_s->current_ssid;
  440. if (hwaddr_aton(addr, bssid)) {
  441. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  442. "'%s'", addr);
  443. return -1;
  444. }
  445. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  446. rsn_preauth_deinit(wpa_s->wpa);
  447. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  448. return -1;
  449. return 0;
  450. }
  451. #endif /* IEEE8021X_EAPOL */
  452. #ifdef CONFIG_PEERKEY
  453. /* MLME-STKSTART.request(peer) */
  454. static int wpa_supplicant_ctrl_iface_stkstart(
  455. struct wpa_supplicant *wpa_s, char *addr)
  456. {
  457. u8 peer[ETH_ALEN];
  458. if (hwaddr_aton(addr, peer)) {
  459. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  460. "address '%s'", addr);
  461. return -1;
  462. }
  463. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  464. MAC2STR(peer));
  465. return wpa_sm_stkstart(wpa_s->wpa, peer);
  466. }
  467. #endif /* CONFIG_PEERKEY */
  468. #ifdef CONFIG_TDLS
  469. static int wpa_supplicant_ctrl_iface_tdls_discover(
  470. struct wpa_supplicant *wpa_s, char *addr)
  471. {
  472. u8 peer[ETH_ALEN];
  473. int ret;
  474. if (hwaddr_aton(addr, peer)) {
  475. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
  476. "address '%s'", addr);
  477. return -1;
  478. }
  479. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
  480. MAC2STR(peer));
  481. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  482. ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
  483. else
  484. ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
  485. return ret;
  486. }
  487. static int wpa_supplicant_ctrl_iface_tdls_setup(
  488. struct wpa_supplicant *wpa_s, char *addr)
  489. {
  490. u8 peer[ETH_ALEN];
  491. int ret;
  492. if (hwaddr_aton(addr, peer)) {
  493. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
  494. "address '%s'", addr);
  495. return -1;
  496. }
  497. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
  498. MAC2STR(peer));
  499. if ((wpa_s->conf->tdls_external_control) &&
  500. wpa_tdls_is_external_setup(wpa_s->wpa))
  501. return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  502. wpa_tdls_remove(wpa_s->wpa, peer);
  503. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  504. ret = wpa_tdls_start(wpa_s->wpa, peer);
  505. else
  506. ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  507. return ret;
  508. }
  509. static int wpa_supplicant_ctrl_iface_tdls_teardown(
  510. struct wpa_supplicant *wpa_s, char *addr)
  511. {
  512. u8 peer[ETH_ALEN];
  513. int ret;
  514. if (hwaddr_aton(addr, peer)) {
  515. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
  516. "address '%s'", addr);
  517. return -1;
  518. }
  519. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
  520. MAC2STR(peer));
  521. if ((wpa_s->conf->tdls_external_control) &&
  522. wpa_tdls_is_external_setup(wpa_s->wpa))
  523. return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  524. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  525. ret = wpa_tdls_teardown_link(
  526. wpa_s->wpa, peer,
  527. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  528. else
  529. ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  530. return ret;
  531. }
  532. #endif /* CONFIG_TDLS */
  533. #ifdef CONFIG_IEEE80211R
  534. static int wpa_supplicant_ctrl_iface_ft_ds(
  535. struct wpa_supplicant *wpa_s, char *addr)
  536. {
  537. u8 target_ap[ETH_ALEN];
  538. struct wpa_bss *bss;
  539. const u8 *mdie;
  540. if (hwaddr_aton(addr, target_ap)) {
  541. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  542. "address '%s'", addr);
  543. return -1;
  544. }
  545. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  546. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  547. if (bss)
  548. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  549. else
  550. mdie = NULL;
  551. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  552. }
  553. #endif /* CONFIG_IEEE80211R */
  554. #ifdef CONFIG_WPS
  555. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  556. char *cmd)
  557. {
  558. u8 bssid[ETH_ALEN], *_bssid = bssid;
  559. #ifdef CONFIG_P2P
  560. u8 p2p_dev_addr[ETH_ALEN];
  561. #endif /* CONFIG_P2P */
  562. #ifdef CONFIG_AP
  563. u8 *_p2p_dev_addr = NULL;
  564. #endif /* CONFIG_AP */
  565. if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
  566. _bssid = NULL;
  567. #ifdef CONFIG_P2P
  568. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  569. if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
  570. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
  571. "P2P Device Address '%s'",
  572. cmd + 13);
  573. return -1;
  574. }
  575. _p2p_dev_addr = p2p_dev_addr;
  576. #endif /* CONFIG_P2P */
  577. } else if (hwaddr_aton(cmd, bssid)) {
  578. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  579. cmd);
  580. return -1;
  581. }
  582. #ifdef CONFIG_AP
  583. if (wpa_s->ap_iface)
  584. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
  585. #endif /* CONFIG_AP */
  586. return wpas_wps_start_pbc(wpa_s, _bssid, 0);
  587. }
  588. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  589. char *cmd, char *buf,
  590. size_t buflen)
  591. {
  592. u8 bssid[ETH_ALEN], *_bssid = bssid;
  593. char *pin;
  594. int ret;
  595. pin = os_strchr(cmd, ' ');
  596. if (pin)
  597. *pin++ = '\0';
  598. if (os_strcmp(cmd, "any") == 0)
  599. _bssid = NULL;
  600. else if (os_strcmp(cmd, "get") == 0) {
  601. ret = wps_generate_pin();
  602. goto done;
  603. } else if (hwaddr_aton(cmd, bssid)) {
  604. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  605. cmd);
  606. return -1;
  607. }
  608. #ifdef CONFIG_AP
  609. if (wpa_s->ap_iface) {
  610. int timeout = 0;
  611. char *pos;
  612. if (pin) {
  613. pos = os_strchr(pin, ' ');
  614. if (pos) {
  615. *pos++ = '\0';
  616. timeout = atoi(pos);
  617. }
  618. }
  619. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  620. buf, buflen, timeout);
  621. }
  622. #endif /* CONFIG_AP */
  623. if (pin) {
  624. ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
  625. DEV_PW_DEFAULT);
  626. if (ret < 0)
  627. return -1;
  628. ret = os_snprintf(buf, buflen, "%s", pin);
  629. if (ret < 0 || (size_t) ret >= buflen)
  630. return -1;
  631. return ret;
  632. }
  633. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
  634. if (ret < 0)
  635. return -1;
  636. done:
  637. /* Return the generated PIN */
  638. ret = os_snprintf(buf, buflen, "%08d", ret);
  639. if (ret < 0 || (size_t) ret >= buflen)
  640. return -1;
  641. return ret;
  642. }
  643. static int wpa_supplicant_ctrl_iface_wps_check_pin(
  644. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  645. {
  646. char pin[9];
  647. size_t len;
  648. char *pos;
  649. int ret;
  650. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  651. (u8 *) cmd, os_strlen(cmd));
  652. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  653. if (*pos < '0' || *pos > '9')
  654. continue;
  655. pin[len++] = *pos;
  656. if (len == 9) {
  657. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  658. return -1;
  659. }
  660. }
  661. if (len != 4 && len != 8) {
  662. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  663. return -1;
  664. }
  665. pin[len] = '\0';
  666. if (len == 8) {
  667. unsigned int pin_val;
  668. pin_val = atoi(pin);
  669. if (!wps_pin_valid(pin_val)) {
  670. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  671. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  672. if (ret < 0 || (size_t) ret >= buflen)
  673. return -1;
  674. return ret;
  675. }
  676. }
  677. ret = os_snprintf(buf, buflen, "%s", pin);
  678. if (ret < 0 || (size_t) ret >= buflen)
  679. return -1;
  680. return ret;
  681. }
  682. #ifdef CONFIG_WPS_NFC
  683. static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
  684. char *cmd)
  685. {
  686. u8 bssid[ETH_ALEN], *_bssid = bssid;
  687. if (cmd == NULL || cmd[0] == '\0')
  688. _bssid = NULL;
  689. else if (hwaddr_aton(cmd, bssid))
  690. return -1;
  691. return wpas_wps_start_nfc(wpa_s, _bssid);
  692. }
  693. static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  694. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  695. {
  696. int ndef;
  697. struct wpabuf *buf;
  698. int res;
  699. char *pos;
  700. pos = os_strchr(cmd, ' ');
  701. if (pos)
  702. *pos++ = '\0';
  703. if (os_strcmp(cmd, "WPS") == 0)
  704. ndef = 0;
  705. else if (os_strcmp(cmd, "NDEF") == 0)
  706. ndef = 1;
  707. else
  708. return -1;
  709. buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
  710. if (buf == NULL)
  711. return -1;
  712. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  713. wpabuf_len(buf));
  714. reply[res++] = '\n';
  715. reply[res] = '\0';
  716. wpabuf_free(buf);
  717. return res;
  718. }
  719. static int wpa_supplicant_ctrl_iface_wps_nfc_token(
  720. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  721. {
  722. int ndef;
  723. struct wpabuf *buf;
  724. int res;
  725. if (os_strcmp(cmd, "WPS") == 0)
  726. ndef = 0;
  727. else if (os_strcmp(cmd, "NDEF") == 0)
  728. ndef = 1;
  729. else
  730. return -1;
  731. buf = wpas_wps_nfc_token(wpa_s, ndef);
  732. if (buf == NULL)
  733. return -1;
  734. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  735. wpabuf_len(buf));
  736. reply[res++] = '\n';
  737. reply[res] = '\0';
  738. wpabuf_free(buf);
  739. return res;
  740. }
  741. static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
  742. struct wpa_supplicant *wpa_s, char *pos)
  743. {
  744. size_t len;
  745. struct wpabuf *buf;
  746. int ret;
  747. len = os_strlen(pos);
  748. if (len & 0x01)
  749. return -1;
  750. len /= 2;
  751. buf = wpabuf_alloc(len);
  752. if (buf == NULL)
  753. return -1;
  754. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  755. wpabuf_free(buf);
  756. return -1;
  757. }
  758. ret = wpas_wps_nfc_tag_read(wpa_s, buf);
  759. wpabuf_free(buf);
  760. return ret;
  761. }
  762. static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
  763. char *reply, size_t max_len,
  764. int cr)
  765. {
  766. struct wpabuf *buf;
  767. int res;
  768. buf = wpas_wps_nfc_handover_req(wpa_s, cr);
  769. if (buf == NULL)
  770. return -1;
  771. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  772. wpabuf_len(buf));
  773. reply[res++] = '\n';
  774. reply[res] = '\0';
  775. wpabuf_free(buf);
  776. return res;
  777. }
  778. static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
  779. char *cmd, char *reply,
  780. size_t max_len)
  781. {
  782. char *pos;
  783. pos = os_strchr(cmd, ' ');
  784. if (pos == NULL)
  785. return -1;
  786. *pos++ = '\0';
  787. if (os_strcmp(cmd, "NDEF") != 0)
  788. return -1;
  789. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  790. return wpas_ctrl_nfc_get_handover_req_wps(
  791. wpa_s, reply, max_len, os_strcmp(pos, "WPS-CR") == 0);
  792. }
  793. return -1;
  794. }
  795. static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
  796. char *reply, size_t max_len,
  797. int ndef, int cr, char *uuid)
  798. {
  799. struct wpabuf *buf;
  800. int res;
  801. buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
  802. if (buf == NULL)
  803. return -1;
  804. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  805. wpabuf_len(buf));
  806. reply[res++] = '\n';
  807. reply[res] = '\0';
  808. wpabuf_free(buf);
  809. return res;
  810. }
  811. static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
  812. char *cmd, char *reply,
  813. size_t max_len)
  814. {
  815. char *pos, *pos2;
  816. int ndef;
  817. pos = os_strchr(cmd, ' ');
  818. if (pos == NULL)
  819. return -1;
  820. *pos++ = '\0';
  821. if (os_strcmp(cmd, "WPS") == 0)
  822. ndef = 0;
  823. else if (os_strcmp(cmd, "NDEF") == 0)
  824. ndef = 1;
  825. else
  826. return -1;
  827. pos2 = os_strchr(pos, ' ');
  828. if (pos2)
  829. *pos2++ = '\0';
  830. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  831. return wpas_ctrl_nfc_get_handover_sel_wps(
  832. wpa_s, reply, max_len, ndef,
  833. os_strcmp(pos, "WPS-CR") == 0, pos2);
  834. }
  835. return -1;
  836. }
  837. static int wpas_ctrl_nfc_rx_handover_req(struct wpa_supplicant *wpa_s,
  838. char *cmd, char *reply,
  839. size_t max_len)
  840. {
  841. size_t len;
  842. struct wpabuf *buf;
  843. int ret;
  844. len = os_strlen(cmd);
  845. if (len & 0x01)
  846. return -1;
  847. len /= 2;
  848. buf = wpabuf_alloc(len);
  849. if (buf == NULL)
  850. return -1;
  851. if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
  852. wpabuf_free(buf);
  853. return -1;
  854. }
  855. ret = wpas_wps_nfc_rx_handover_req(wpa_s, buf);
  856. wpabuf_free(buf);
  857. return ret;
  858. }
  859. static int wpas_ctrl_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
  860. char *cmd)
  861. {
  862. size_t len;
  863. struct wpabuf *buf;
  864. int ret;
  865. len = os_strlen(cmd);
  866. if (len & 0x01)
  867. return -1;
  868. len /= 2;
  869. buf = wpabuf_alloc(len);
  870. if (buf == NULL)
  871. return -1;
  872. if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
  873. wpabuf_free(buf);
  874. return -1;
  875. }
  876. ret = wpas_wps_nfc_rx_handover_sel(wpa_s, buf);
  877. wpabuf_free(buf);
  878. return ret;
  879. }
  880. static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
  881. char *cmd)
  882. {
  883. size_t len;
  884. struct wpabuf *req, *sel;
  885. int ret;
  886. char *pos, *role, *type, *pos2;
  887. role = cmd;
  888. pos = os_strchr(role, ' ');
  889. if (pos == NULL)
  890. return -1;
  891. *pos++ = '\0';
  892. type = pos;
  893. pos = os_strchr(type, ' ');
  894. if (pos == NULL)
  895. return -1;
  896. *pos++ = '\0';
  897. pos2 = os_strchr(pos, ' ');
  898. if (pos2 == NULL)
  899. return -1;
  900. *pos2++ = '\0';
  901. len = os_strlen(pos);
  902. if (len & 0x01)
  903. return -1;
  904. len /= 2;
  905. req = wpabuf_alloc(len);
  906. if (req == NULL)
  907. return -1;
  908. if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
  909. wpabuf_free(req);
  910. return -1;
  911. }
  912. len = os_strlen(pos2);
  913. if (len & 0x01) {
  914. wpabuf_free(req);
  915. return -1;
  916. }
  917. len /= 2;
  918. sel = wpabuf_alloc(len);
  919. if (sel == NULL) {
  920. wpabuf_free(req);
  921. return -1;
  922. }
  923. if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
  924. wpabuf_free(req);
  925. wpabuf_free(sel);
  926. return -1;
  927. }
  928. if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
  929. ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
  930. } else {
  931. wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
  932. "reported: role=%s type=%s", role, type);
  933. ret = -1;
  934. }
  935. wpabuf_free(req);
  936. wpabuf_free(sel);
  937. return ret;
  938. }
  939. #endif /* CONFIG_WPS_NFC */
  940. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  941. char *cmd)
  942. {
  943. u8 bssid[ETH_ALEN];
  944. char *pin;
  945. char *new_ssid;
  946. char *new_auth;
  947. char *new_encr;
  948. char *new_key;
  949. struct wps_new_ap_settings ap;
  950. pin = os_strchr(cmd, ' ');
  951. if (pin == NULL)
  952. return -1;
  953. *pin++ = '\0';
  954. if (hwaddr_aton(cmd, bssid)) {
  955. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  956. cmd);
  957. return -1;
  958. }
  959. new_ssid = os_strchr(pin, ' ');
  960. if (new_ssid == NULL)
  961. return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
  962. *new_ssid++ = '\0';
  963. new_auth = os_strchr(new_ssid, ' ');
  964. if (new_auth == NULL)
  965. return -1;
  966. *new_auth++ = '\0';
  967. new_encr = os_strchr(new_auth, ' ');
  968. if (new_encr == NULL)
  969. return -1;
  970. *new_encr++ = '\0';
  971. new_key = os_strchr(new_encr, ' ');
  972. if (new_key == NULL)
  973. return -1;
  974. *new_key++ = '\0';
  975. os_memset(&ap, 0, sizeof(ap));
  976. ap.ssid_hex = new_ssid;
  977. ap.auth = new_auth;
  978. ap.encr = new_encr;
  979. ap.key_hex = new_key;
  980. return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
  981. }
  982. #ifdef CONFIG_AP
  983. static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
  984. char *cmd, char *buf,
  985. size_t buflen)
  986. {
  987. int timeout = 300;
  988. char *pos;
  989. const char *pin_txt;
  990. if (!wpa_s->ap_iface)
  991. return -1;
  992. pos = os_strchr(cmd, ' ');
  993. if (pos)
  994. *pos++ = '\0';
  995. if (os_strcmp(cmd, "disable") == 0) {
  996. wpas_wps_ap_pin_disable(wpa_s);
  997. return os_snprintf(buf, buflen, "OK\n");
  998. }
  999. if (os_strcmp(cmd, "random") == 0) {
  1000. if (pos)
  1001. timeout = atoi(pos);
  1002. pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
  1003. if (pin_txt == NULL)
  1004. return -1;
  1005. return os_snprintf(buf, buflen, "%s", pin_txt);
  1006. }
  1007. if (os_strcmp(cmd, "get") == 0) {
  1008. pin_txt = wpas_wps_ap_pin_get(wpa_s);
  1009. if (pin_txt == NULL)
  1010. return -1;
  1011. return os_snprintf(buf, buflen, "%s", pin_txt);
  1012. }
  1013. if (os_strcmp(cmd, "set") == 0) {
  1014. char *pin;
  1015. if (pos == NULL)
  1016. return -1;
  1017. pin = pos;
  1018. pos = os_strchr(pos, ' ');
  1019. if (pos) {
  1020. *pos++ = '\0';
  1021. timeout = atoi(pos);
  1022. }
  1023. if (os_strlen(pin) > buflen)
  1024. return -1;
  1025. if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
  1026. return -1;
  1027. return os_snprintf(buf, buflen, "%s", pin);
  1028. }
  1029. return -1;
  1030. }
  1031. #endif /* CONFIG_AP */
  1032. #ifdef CONFIG_WPS_ER
  1033. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  1034. char *cmd)
  1035. {
  1036. char *uuid = cmd, *pin, *pos;
  1037. u8 addr_buf[ETH_ALEN], *addr = NULL;
  1038. pin = os_strchr(uuid, ' ');
  1039. if (pin == NULL)
  1040. return -1;
  1041. *pin++ = '\0';
  1042. pos = os_strchr(pin, ' ');
  1043. if (pos) {
  1044. *pos++ = '\0';
  1045. if (hwaddr_aton(pos, addr_buf) == 0)
  1046. addr = addr_buf;
  1047. }
  1048. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  1049. }
  1050. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  1051. char *cmd)
  1052. {
  1053. char *uuid = cmd, *pin;
  1054. pin = os_strchr(uuid, ' ');
  1055. if (pin == NULL)
  1056. return -1;
  1057. *pin++ = '\0';
  1058. return wpas_wps_er_learn(wpa_s, uuid, pin);
  1059. }
  1060. static int wpa_supplicant_ctrl_iface_wps_er_set_config(
  1061. struct wpa_supplicant *wpa_s, char *cmd)
  1062. {
  1063. char *uuid = cmd, *id;
  1064. id = os_strchr(uuid, ' ');
  1065. if (id == NULL)
  1066. return -1;
  1067. *id++ = '\0';
  1068. return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
  1069. }
  1070. static int wpa_supplicant_ctrl_iface_wps_er_config(
  1071. struct wpa_supplicant *wpa_s, char *cmd)
  1072. {
  1073. char *pin;
  1074. char *new_ssid;
  1075. char *new_auth;
  1076. char *new_encr;
  1077. char *new_key;
  1078. struct wps_new_ap_settings ap;
  1079. pin = os_strchr(cmd, ' ');
  1080. if (pin == NULL)
  1081. return -1;
  1082. *pin++ = '\0';
  1083. new_ssid = os_strchr(pin, ' ');
  1084. if (new_ssid == NULL)
  1085. return -1;
  1086. *new_ssid++ = '\0';
  1087. new_auth = os_strchr(new_ssid, ' ');
  1088. if (new_auth == NULL)
  1089. return -1;
  1090. *new_auth++ = '\0';
  1091. new_encr = os_strchr(new_auth, ' ');
  1092. if (new_encr == NULL)
  1093. return -1;
  1094. *new_encr++ = '\0';
  1095. new_key = os_strchr(new_encr, ' ');
  1096. if (new_key == NULL)
  1097. return -1;
  1098. *new_key++ = '\0';
  1099. os_memset(&ap, 0, sizeof(ap));
  1100. ap.ssid_hex = new_ssid;
  1101. ap.auth = new_auth;
  1102. ap.encr = new_encr;
  1103. ap.key_hex = new_key;
  1104. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  1105. }
  1106. #ifdef CONFIG_WPS_NFC
  1107. static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  1108. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1109. {
  1110. int ndef;
  1111. struct wpabuf *buf;
  1112. int res;
  1113. char *uuid;
  1114. uuid = os_strchr(cmd, ' ');
  1115. if (uuid == NULL)
  1116. return -1;
  1117. *uuid++ = '\0';
  1118. if (os_strcmp(cmd, "WPS") == 0)
  1119. ndef = 0;
  1120. else if (os_strcmp(cmd, "NDEF") == 0)
  1121. ndef = 1;
  1122. else
  1123. return -1;
  1124. buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
  1125. if (buf == NULL)
  1126. return -1;
  1127. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1128. wpabuf_len(buf));
  1129. reply[res++] = '\n';
  1130. reply[res] = '\0';
  1131. wpabuf_free(buf);
  1132. return res;
  1133. }
  1134. #endif /* CONFIG_WPS_NFC */
  1135. #endif /* CONFIG_WPS_ER */
  1136. #endif /* CONFIG_WPS */
  1137. #ifdef CONFIG_IBSS_RSN
  1138. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  1139. struct wpa_supplicant *wpa_s, char *addr)
  1140. {
  1141. u8 peer[ETH_ALEN];
  1142. if (hwaddr_aton(addr, peer)) {
  1143. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  1144. "address '%s'", addr);
  1145. return -1;
  1146. }
  1147. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  1148. MAC2STR(peer));
  1149. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  1150. }
  1151. #endif /* CONFIG_IBSS_RSN */
  1152. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  1153. char *rsp)
  1154. {
  1155. #ifdef IEEE8021X_EAPOL
  1156. char *pos, *id_pos;
  1157. int id;
  1158. struct wpa_ssid *ssid;
  1159. pos = os_strchr(rsp, '-');
  1160. if (pos == NULL)
  1161. return -1;
  1162. *pos++ = '\0';
  1163. id_pos = pos;
  1164. pos = os_strchr(pos, ':');
  1165. if (pos == NULL)
  1166. return -1;
  1167. *pos++ = '\0';
  1168. id = atoi(id_pos);
  1169. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  1170. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1171. (u8 *) pos, os_strlen(pos));
  1172. ssid = wpa_config_get_network(wpa_s->conf, id);
  1173. if (ssid == NULL) {
  1174. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1175. "to update", id);
  1176. return -1;
  1177. }
  1178. return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
  1179. pos);
  1180. #else /* IEEE8021X_EAPOL */
  1181. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  1182. return -1;
  1183. #endif /* IEEE8021X_EAPOL */
  1184. }
  1185. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  1186. const char *params,
  1187. char *buf, size_t buflen)
  1188. {
  1189. char *pos, *end, tmp[30];
  1190. int res, verbose, wps, ret;
  1191. if (os_strcmp(params, "-DRIVER") == 0)
  1192. return wpa_drv_status(wpa_s, buf, buflen);
  1193. verbose = os_strcmp(params, "-VERBOSE") == 0;
  1194. wps = os_strcmp(params, "-WPS") == 0;
  1195. pos = buf;
  1196. end = buf + buflen;
  1197. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  1198. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1199. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  1200. MAC2STR(wpa_s->bssid));
  1201. if (ret < 0 || ret >= end - pos)
  1202. return pos - buf;
  1203. pos += ret;
  1204. if (ssid) {
  1205. u8 *_ssid = ssid->ssid;
  1206. size_t ssid_len = ssid->ssid_len;
  1207. u8 ssid_buf[MAX_SSID_LEN];
  1208. if (ssid_len == 0) {
  1209. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  1210. if (_res < 0)
  1211. ssid_len = 0;
  1212. else
  1213. ssid_len = _res;
  1214. _ssid = ssid_buf;
  1215. }
  1216. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  1217. wpa_ssid_txt(_ssid, ssid_len),
  1218. ssid->id);
  1219. if (ret < 0 || ret >= end - pos)
  1220. return pos - buf;
  1221. pos += ret;
  1222. if (wps && ssid->passphrase &&
  1223. wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
  1224. (ssid->mode == WPAS_MODE_AP ||
  1225. ssid->mode == WPAS_MODE_P2P_GO)) {
  1226. ret = os_snprintf(pos, end - pos,
  1227. "passphrase=%s\n",
  1228. ssid->passphrase);
  1229. if (ret < 0 || ret >= end - pos)
  1230. return pos - buf;
  1231. pos += ret;
  1232. }
  1233. if (ssid->id_str) {
  1234. ret = os_snprintf(pos, end - pos,
  1235. "id_str=%s\n",
  1236. ssid->id_str);
  1237. if (ret < 0 || ret >= end - pos)
  1238. return pos - buf;
  1239. pos += ret;
  1240. }
  1241. switch (ssid->mode) {
  1242. case WPAS_MODE_INFRA:
  1243. ret = os_snprintf(pos, end - pos,
  1244. "mode=station\n");
  1245. break;
  1246. case WPAS_MODE_IBSS:
  1247. ret = os_snprintf(pos, end - pos,
  1248. "mode=IBSS\n");
  1249. break;
  1250. case WPAS_MODE_AP:
  1251. ret = os_snprintf(pos, end - pos,
  1252. "mode=AP\n");
  1253. break;
  1254. case WPAS_MODE_P2P_GO:
  1255. ret = os_snprintf(pos, end - pos,
  1256. "mode=P2P GO\n");
  1257. break;
  1258. case WPAS_MODE_P2P_GROUP_FORMATION:
  1259. ret = os_snprintf(pos, end - pos,
  1260. "mode=P2P GO - group "
  1261. "formation\n");
  1262. break;
  1263. default:
  1264. ret = 0;
  1265. break;
  1266. }
  1267. if (ret < 0 || ret >= end - pos)
  1268. return pos - buf;
  1269. pos += ret;
  1270. }
  1271. #ifdef CONFIG_AP
  1272. if (wpa_s->ap_iface) {
  1273. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  1274. end - pos,
  1275. verbose);
  1276. } else
  1277. #endif /* CONFIG_AP */
  1278. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1279. }
  1280. #ifdef CONFIG_SAE
  1281. if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
  1282. #ifdef CONFIG_AP
  1283. !wpa_s->ap_iface &&
  1284. #endif /* CONFIG_AP */
  1285. wpa_s->sme.sae.state == SAE_ACCEPTED) {
  1286. ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
  1287. wpa_s->sme.sae.group);
  1288. if (ret < 0 || ret >= end - pos)
  1289. return pos - buf;
  1290. pos += ret;
  1291. }
  1292. #endif /* CONFIG_SAE */
  1293. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  1294. wpa_supplicant_state_txt(wpa_s->wpa_state));
  1295. if (ret < 0 || ret >= end - pos)
  1296. return pos - buf;
  1297. pos += ret;
  1298. if (wpa_s->l2 &&
  1299. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  1300. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  1301. if (ret < 0 || ret >= end - pos)
  1302. return pos - buf;
  1303. pos += ret;
  1304. }
  1305. #ifdef CONFIG_P2P
  1306. if (wpa_s->global->p2p) {
  1307. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  1308. "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
  1309. if (ret < 0 || ret >= end - pos)
  1310. return pos - buf;
  1311. pos += ret;
  1312. }
  1313. #endif /* CONFIG_P2P */
  1314. ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
  1315. MAC2STR(wpa_s->own_addr));
  1316. if (ret < 0 || ret >= end - pos)
  1317. return pos - buf;
  1318. pos += ret;
  1319. #ifdef CONFIG_HS20
  1320. if (wpa_s->current_bss &&
  1321. wpa_bss_get_vendor_ie(wpa_s->current_bss, HS20_IE_VENDOR_TYPE) &&
  1322. wpa_s->wpa_proto == WPA_PROTO_RSN &&
  1323. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1324. ret = os_snprintf(pos, end - pos, "hs20=1\n");
  1325. if (ret < 0 || ret >= end - pos)
  1326. return pos - buf;
  1327. pos += ret;
  1328. }
  1329. if (wpa_s->current_ssid) {
  1330. struct wpa_cred *cred;
  1331. char *type;
  1332. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1333. size_t i;
  1334. if (wpa_s->current_ssid->parent_cred != cred)
  1335. continue;
  1336. for (i = 0; cred->domain && i < cred->num_domain; i++) {
  1337. ret = os_snprintf(pos, end - pos,
  1338. "home_sp=%s\n",
  1339. cred->domain[i]);
  1340. if (ret < 0 || ret >= end - pos)
  1341. return pos - buf;
  1342. pos += ret;
  1343. }
  1344. if (wpa_s->current_bss == NULL ||
  1345. wpa_s->current_bss->anqp == NULL)
  1346. res = -1;
  1347. else
  1348. res = interworking_home_sp_cred(
  1349. wpa_s, cred,
  1350. wpa_s->current_bss->anqp->domain_name);
  1351. if (res > 0)
  1352. type = "home";
  1353. else if (res == 0)
  1354. type = "roaming";
  1355. else
  1356. type = "unknown";
  1357. ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
  1358. if (ret < 0 || ret >= end - pos)
  1359. return pos - buf;
  1360. pos += ret;
  1361. break;
  1362. }
  1363. }
  1364. #endif /* CONFIG_HS20 */
  1365. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1366. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1367. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  1368. verbose);
  1369. if (res >= 0)
  1370. pos += res;
  1371. }
  1372. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1373. if (res >= 0)
  1374. pos += res;
  1375. #ifdef CONFIG_WPS
  1376. {
  1377. char uuid_str[100];
  1378. uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
  1379. ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
  1380. if (ret < 0 || ret >= end - pos)
  1381. return pos - buf;
  1382. pos += ret;
  1383. }
  1384. #endif /* CONFIG_WPS */
  1385. #ifdef ANDROID
  1386. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
  1387. "id=%d state=%d BSSID=" MACSTR " SSID=%s",
  1388. wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
  1389. wpa_s->wpa_state,
  1390. MAC2STR(wpa_s->bssid),
  1391. wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
  1392. wpa_ssid_txt(wpa_s->current_ssid->ssid,
  1393. wpa_s->current_ssid->ssid_len) : "");
  1394. if (wpa_s->wpa_state == WPA_COMPLETED) {
  1395. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1396. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
  1397. "- connection to " MACSTR
  1398. " completed %s [id=%d id_str=%s]",
  1399. MAC2STR(wpa_s->bssid), "(auth)",
  1400. ssid ? ssid->id : -1,
  1401. ssid && ssid->id_str ? ssid->id_str : "");
  1402. }
  1403. #endif /* ANDROID */
  1404. return pos - buf;
  1405. }
  1406. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  1407. char *cmd)
  1408. {
  1409. char *pos;
  1410. int id;
  1411. struct wpa_ssid *ssid;
  1412. u8 bssid[ETH_ALEN];
  1413. /* cmd: "<network id> <BSSID>" */
  1414. pos = os_strchr(cmd, ' ');
  1415. if (pos == NULL)
  1416. return -1;
  1417. *pos++ = '\0';
  1418. id = atoi(cmd);
  1419. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  1420. if (hwaddr_aton(pos, bssid)) {
  1421. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  1422. return -1;
  1423. }
  1424. ssid = wpa_config_get_network(wpa_s->conf, id);
  1425. if (ssid == NULL) {
  1426. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1427. "to update", id);
  1428. return -1;
  1429. }
  1430. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  1431. ssid->bssid_set = !is_zero_ether_addr(bssid);
  1432. return 0;
  1433. }
  1434. static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
  1435. char *cmd, char *buf,
  1436. size_t buflen)
  1437. {
  1438. u8 bssid[ETH_ALEN];
  1439. struct wpa_blacklist *e;
  1440. char *pos, *end;
  1441. int ret;
  1442. /* cmd: "BLACKLIST [<BSSID>]" */
  1443. if (*cmd == '\0') {
  1444. pos = buf;
  1445. end = buf + buflen;
  1446. e = wpa_s->blacklist;
  1447. while (e) {
  1448. ret = os_snprintf(pos, end - pos, MACSTR "\n",
  1449. MAC2STR(e->bssid));
  1450. if (ret < 0 || ret >= end - pos)
  1451. return pos - buf;
  1452. pos += ret;
  1453. e = e->next;
  1454. }
  1455. return pos - buf;
  1456. }
  1457. cmd++;
  1458. if (os_strncmp(cmd, "clear", 5) == 0) {
  1459. wpa_blacklist_clear(wpa_s);
  1460. os_memcpy(buf, "OK\n", 3);
  1461. return 3;
  1462. }
  1463. wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
  1464. if (hwaddr_aton(cmd, bssid)) {
  1465. wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
  1466. return -1;
  1467. }
  1468. /*
  1469. * Add the BSSID twice, so its count will be 2, causing it to be
  1470. * skipped when processing scan results.
  1471. */
  1472. ret = wpa_blacklist_add(wpa_s, bssid);
  1473. if (ret != 0)
  1474. return -1;
  1475. ret = wpa_blacklist_add(wpa_s, bssid);
  1476. if (ret != 0)
  1477. return -1;
  1478. os_memcpy(buf, "OK\n", 3);
  1479. return 3;
  1480. }
  1481. extern int wpa_debug_level;
  1482. extern int wpa_debug_timestamp;
  1483. static const char * debug_level_str(int level)
  1484. {
  1485. switch (level) {
  1486. case MSG_EXCESSIVE:
  1487. return "EXCESSIVE";
  1488. case MSG_MSGDUMP:
  1489. return "MSGDUMP";
  1490. case MSG_DEBUG:
  1491. return "DEBUG";
  1492. case MSG_INFO:
  1493. return "INFO";
  1494. case MSG_WARNING:
  1495. return "WARNING";
  1496. case MSG_ERROR:
  1497. return "ERROR";
  1498. default:
  1499. return "?";
  1500. }
  1501. }
  1502. static int str_to_debug_level(const char *s)
  1503. {
  1504. if (os_strcasecmp(s, "EXCESSIVE") == 0)
  1505. return MSG_EXCESSIVE;
  1506. if (os_strcasecmp(s, "MSGDUMP") == 0)
  1507. return MSG_MSGDUMP;
  1508. if (os_strcasecmp(s, "DEBUG") == 0)
  1509. return MSG_DEBUG;
  1510. if (os_strcasecmp(s, "INFO") == 0)
  1511. return MSG_INFO;
  1512. if (os_strcasecmp(s, "WARNING") == 0)
  1513. return MSG_WARNING;
  1514. if (os_strcasecmp(s, "ERROR") == 0)
  1515. return MSG_ERROR;
  1516. return -1;
  1517. }
  1518. static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
  1519. char *cmd, char *buf,
  1520. size_t buflen)
  1521. {
  1522. char *pos, *end, *stamp;
  1523. int ret;
  1524. if (cmd == NULL) {
  1525. return -1;
  1526. }
  1527. /* cmd: "LOG_LEVEL [<level>]" */
  1528. if (*cmd == '\0') {
  1529. pos = buf;
  1530. end = buf + buflen;
  1531. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  1532. "Timestamp: %d\n",
  1533. debug_level_str(wpa_debug_level),
  1534. wpa_debug_timestamp);
  1535. if (ret < 0 || ret >= end - pos)
  1536. ret = 0;
  1537. return ret;
  1538. }
  1539. while (*cmd == ' ')
  1540. cmd++;
  1541. stamp = os_strchr(cmd, ' ');
  1542. if (stamp) {
  1543. *stamp++ = '\0';
  1544. while (*stamp == ' ') {
  1545. stamp++;
  1546. }
  1547. }
  1548. if (cmd && os_strlen(cmd)) {
  1549. int level = str_to_debug_level(cmd);
  1550. if (level < 0)
  1551. return -1;
  1552. wpa_debug_level = level;
  1553. }
  1554. if (stamp && os_strlen(stamp))
  1555. wpa_debug_timestamp = atoi(stamp);
  1556. os_memcpy(buf, "OK\n", 3);
  1557. return 3;
  1558. }
  1559. static int wpa_supplicant_ctrl_iface_list_networks(
  1560. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1561. {
  1562. char *pos, *end;
  1563. struct wpa_ssid *ssid;
  1564. int ret;
  1565. pos = buf;
  1566. end = buf + buflen;
  1567. ret = os_snprintf(pos, end - pos,
  1568. "network id / ssid / bssid / flags\n");
  1569. if (ret < 0 || ret >= end - pos)
  1570. return pos - buf;
  1571. pos += ret;
  1572. ssid = wpa_s->conf->ssid;
  1573. while (ssid) {
  1574. ret = os_snprintf(pos, end - pos, "%d\t%s",
  1575. ssid->id,
  1576. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1577. if (ret < 0 || ret >= end - pos)
  1578. return pos - buf;
  1579. pos += ret;
  1580. if (ssid->bssid_set) {
  1581. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  1582. MAC2STR(ssid->bssid));
  1583. } else {
  1584. ret = os_snprintf(pos, end - pos, "\tany");
  1585. }
  1586. if (ret < 0 || ret >= end - pos)
  1587. return pos - buf;
  1588. pos += ret;
  1589. ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
  1590. ssid == wpa_s->current_ssid ?
  1591. "[CURRENT]" : "",
  1592. ssid->disabled ? "[DISABLED]" : "",
  1593. ssid->disabled_until.sec ?
  1594. "[TEMP-DISABLED]" : "",
  1595. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  1596. "");
  1597. if (ret < 0 || ret >= end - pos)
  1598. return pos - buf;
  1599. pos += ret;
  1600. ret = os_snprintf(pos, end - pos, "\n");
  1601. if (ret < 0 || ret >= end - pos)
  1602. return pos - buf;
  1603. pos += ret;
  1604. ssid = ssid->next;
  1605. }
  1606. return pos - buf;
  1607. }
  1608. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  1609. {
  1610. int ret;
  1611. ret = os_snprintf(pos, end - pos, "-");
  1612. if (ret < 0 || ret >= end - pos)
  1613. return pos;
  1614. pos += ret;
  1615. ret = wpa_write_ciphers(pos, end, cipher, "+");
  1616. if (ret < 0)
  1617. return pos;
  1618. pos += ret;
  1619. return pos;
  1620. }
  1621. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  1622. const u8 *ie, size_t ie_len)
  1623. {
  1624. struct wpa_ie_data data;
  1625. int first, ret;
  1626. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  1627. if (ret < 0 || ret >= end - pos)
  1628. return pos;
  1629. pos += ret;
  1630. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  1631. ret = os_snprintf(pos, end - pos, "?]");
  1632. if (ret < 0 || ret >= end - pos)
  1633. return pos;
  1634. pos += ret;
  1635. return pos;
  1636. }
  1637. first = 1;
  1638. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  1639. ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
  1640. if (ret < 0 || ret >= end - pos)
  1641. return pos;
  1642. pos += ret;
  1643. first = 0;
  1644. }
  1645. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  1646. ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
  1647. if (ret < 0 || ret >= end - pos)
  1648. return pos;
  1649. pos += ret;
  1650. first = 0;
  1651. }
  1652. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  1653. ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
  1654. if (ret < 0 || ret >= end - pos)
  1655. return pos;
  1656. pos += ret;
  1657. first = 0;
  1658. }
  1659. #ifdef CONFIG_IEEE80211R
  1660. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  1661. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  1662. first ? "" : "+");
  1663. if (ret < 0 || ret >= end - pos)
  1664. return pos;
  1665. pos += ret;
  1666. first = 0;
  1667. }
  1668. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  1669. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  1670. first ? "" : "+");
  1671. if (ret < 0 || ret >= end - pos)
  1672. return pos;
  1673. pos += ret;
  1674. first = 0;
  1675. }
  1676. #endif /* CONFIG_IEEE80211R */
  1677. #ifdef CONFIG_IEEE80211W
  1678. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  1679. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  1680. first ? "" : "+");
  1681. if (ret < 0 || ret >= end - pos)
  1682. return pos;
  1683. pos += ret;
  1684. first = 0;
  1685. }
  1686. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  1687. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  1688. first ? "" : "+");
  1689. if (ret < 0 || ret >= end - pos)
  1690. return pos;
  1691. pos += ret;
  1692. first = 0;
  1693. }
  1694. #endif /* CONFIG_IEEE80211W */
  1695. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  1696. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  1697. ret = os_snprintf(pos, end - pos, "-preauth");
  1698. if (ret < 0 || ret >= end - pos)
  1699. return pos;
  1700. pos += ret;
  1701. }
  1702. ret = os_snprintf(pos, end - pos, "]");
  1703. if (ret < 0 || ret >= end - pos)
  1704. return pos;
  1705. pos += ret;
  1706. return pos;
  1707. }
  1708. #ifdef CONFIG_WPS
  1709. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  1710. char *pos, char *end,
  1711. struct wpabuf *wps_ie)
  1712. {
  1713. int ret;
  1714. const char *txt;
  1715. if (wps_ie == NULL)
  1716. return pos;
  1717. if (wps_is_selected_pbc_registrar(wps_ie))
  1718. txt = "[WPS-PBC]";
  1719. #ifdef CONFIG_WPS2
  1720. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  1721. txt = "[WPS-AUTH]";
  1722. #endif /* CONFIG_WPS2 */
  1723. else if (wps_is_selected_pin_registrar(wps_ie))
  1724. txt = "[WPS-PIN]";
  1725. else
  1726. txt = "[WPS]";
  1727. ret = os_snprintf(pos, end - pos, "%s", txt);
  1728. if (ret >= 0 && ret < end - pos)
  1729. pos += ret;
  1730. wpabuf_free(wps_ie);
  1731. return pos;
  1732. }
  1733. #endif /* CONFIG_WPS */
  1734. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  1735. char *pos, char *end,
  1736. const struct wpa_bss *bss)
  1737. {
  1738. #ifdef CONFIG_WPS
  1739. struct wpabuf *wps_ie;
  1740. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1741. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  1742. #else /* CONFIG_WPS */
  1743. return pos;
  1744. #endif /* CONFIG_WPS */
  1745. }
  1746. /* Format one result on one text line into a buffer. */
  1747. static int wpa_supplicant_ctrl_iface_scan_result(
  1748. struct wpa_supplicant *wpa_s,
  1749. const struct wpa_bss *bss, char *buf, size_t buflen)
  1750. {
  1751. char *pos, *end;
  1752. int ret;
  1753. const u8 *ie, *ie2, *p2p;
  1754. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  1755. if (!p2p)
  1756. p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
  1757. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  1758. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  1759. 0)
  1760. return 0; /* Do not show P2P listen discovery results here */
  1761. pos = buf;
  1762. end = buf + buflen;
  1763. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  1764. MAC2STR(bss->bssid), bss->freq, bss->level);
  1765. if (ret < 0 || ret >= end - pos)
  1766. return -1;
  1767. pos += ret;
  1768. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1769. if (ie)
  1770. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1771. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1772. if (ie2)
  1773. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1774. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1775. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1776. ret = os_snprintf(pos, end - pos, "[WEP]");
  1777. if (ret < 0 || ret >= end - pos)
  1778. return -1;
  1779. pos += ret;
  1780. }
  1781. if (bss->caps & IEEE80211_CAP_IBSS) {
  1782. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1783. if (ret < 0 || ret >= end - pos)
  1784. return -1;
  1785. pos += ret;
  1786. }
  1787. if (bss->caps & IEEE80211_CAP_ESS) {
  1788. ret = os_snprintf(pos, end - pos, "[ESS]");
  1789. if (ret < 0 || ret >= end - pos)
  1790. return -1;
  1791. pos += ret;
  1792. }
  1793. if (p2p) {
  1794. ret = os_snprintf(pos, end - pos, "[P2P]");
  1795. if (ret < 0 || ret >= end - pos)
  1796. return -1;
  1797. pos += ret;
  1798. }
  1799. #ifdef CONFIG_HS20
  1800. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  1801. ret = os_snprintf(pos, end - pos, "[HS20]");
  1802. if (ret < 0 || ret >= end - pos)
  1803. return -1;
  1804. pos += ret;
  1805. }
  1806. #endif /* CONFIG_HS20 */
  1807. ret = os_snprintf(pos, end - pos, "\t%s",
  1808. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1809. if (ret < 0 || ret >= end - pos)
  1810. return -1;
  1811. pos += ret;
  1812. ret = os_snprintf(pos, end - pos, "\n");
  1813. if (ret < 0 || ret >= end - pos)
  1814. return -1;
  1815. pos += ret;
  1816. return pos - buf;
  1817. }
  1818. static int wpa_supplicant_ctrl_iface_scan_results(
  1819. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1820. {
  1821. char *pos, *end;
  1822. struct wpa_bss *bss;
  1823. int ret;
  1824. pos = buf;
  1825. end = buf + buflen;
  1826. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  1827. "flags / ssid\n");
  1828. if (ret < 0 || ret >= end - pos)
  1829. return pos - buf;
  1830. pos += ret;
  1831. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  1832. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  1833. end - pos);
  1834. if (ret < 0 || ret >= end - pos)
  1835. return pos - buf;
  1836. pos += ret;
  1837. }
  1838. return pos - buf;
  1839. }
  1840. static int wpa_supplicant_ctrl_iface_select_network(
  1841. struct wpa_supplicant *wpa_s, char *cmd)
  1842. {
  1843. int id;
  1844. struct wpa_ssid *ssid;
  1845. /* cmd: "<network id>" or "any" */
  1846. if (os_strcmp(cmd, "any") == 0) {
  1847. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  1848. ssid = NULL;
  1849. } else {
  1850. id = atoi(cmd);
  1851. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  1852. ssid = wpa_config_get_network(wpa_s->conf, id);
  1853. if (ssid == NULL) {
  1854. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1855. "network id=%d", id);
  1856. return -1;
  1857. }
  1858. if (ssid->disabled == 2) {
  1859. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1860. "SELECT_NETWORK with persistent P2P group");
  1861. return -1;
  1862. }
  1863. }
  1864. wpa_supplicant_select_network(wpa_s, ssid);
  1865. return 0;
  1866. }
  1867. static int wpa_supplicant_ctrl_iface_enable_network(
  1868. struct wpa_supplicant *wpa_s, char *cmd)
  1869. {
  1870. int id;
  1871. struct wpa_ssid *ssid;
  1872. /* cmd: "<network id>" or "all" */
  1873. if (os_strcmp(cmd, "all") == 0) {
  1874. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  1875. ssid = NULL;
  1876. } else {
  1877. id = atoi(cmd);
  1878. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  1879. ssid = wpa_config_get_network(wpa_s->conf, id);
  1880. if (ssid == NULL) {
  1881. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1882. "network id=%d", id);
  1883. return -1;
  1884. }
  1885. if (ssid->disabled == 2) {
  1886. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1887. "ENABLE_NETWORK with persistent P2P group");
  1888. return -1;
  1889. }
  1890. if (os_strstr(cmd, " no-connect")) {
  1891. ssid->disabled = 0;
  1892. return 0;
  1893. }
  1894. }
  1895. wpa_supplicant_enable_network(wpa_s, ssid);
  1896. return 0;
  1897. }
  1898. static int wpa_supplicant_ctrl_iface_disable_network(
  1899. struct wpa_supplicant *wpa_s, char *cmd)
  1900. {
  1901. int id;
  1902. struct wpa_ssid *ssid;
  1903. /* cmd: "<network id>" or "all" */
  1904. if (os_strcmp(cmd, "all") == 0) {
  1905. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  1906. ssid = NULL;
  1907. } else {
  1908. id = atoi(cmd);
  1909. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  1910. ssid = wpa_config_get_network(wpa_s->conf, id);
  1911. if (ssid == NULL) {
  1912. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1913. "network id=%d", id);
  1914. return -1;
  1915. }
  1916. if (ssid->disabled == 2) {
  1917. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1918. "DISABLE_NETWORK with persistent P2P "
  1919. "group");
  1920. return -1;
  1921. }
  1922. }
  1923. wpa_supplicant_disable_network(wpa_s, ssid);
  1924. return 0;
  1925. }
  1926. static int wpa_supplicant_ctrl_iface_add_network(
  1927. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1928. {
  1929. struct wpa_ssid *ssid;
  1930. int ret;
  1931. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  1932. ssid = wpa_config_add_network(wpa_s->conf);
  1933. if (ssid == NULL)
  1934. return -1;
  1935. wpas_notify_network_added(wpa_s, ssid);
  1936. ssid->disabled = 1;
  1937. wpa_config_set_network_defaults(ssid);
  1938. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  1939. if (ret < 0 || (size_t) ret >= buflen)
  1940. return -1;
  1941. return ret;
  1942. }
  1943. static int wpa_supplicant_ctrl_iface_remove_network(
  1944. struct wpa_supplicant *wpa_s, char *cmd)
  1945. {
  1946. int id;
  1947. struct wpa_ssid *ssid;
  1948. int was_disabled;
  1949. /* cmd: "<network id>" or "all" */
  1950. if (os_strcmp(cmd, "all") == 0) {
  1951. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  1952. if (wpa_s->sched_scanning)
  1953. wpa_supplicant_cancel_sched_scan(wpa_s);
  1954. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1955. if (wpa_s->current_ssid) {
  1956. #ifdef CONFIG_SME
  1957. wpa_s->sme.prev_bssid_set = 0;
  1958. #endif /* CONFIG_SME */
  1959. wpa_sm_set_config(wpa_s->wpa, NULL);
  1960. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1961. wpa_supplicant_deauthenticate(
  1962. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1963. }
  1964. ssid = wpa_s->conf->ssid;
  1965. while (ssid) {
  1966. struct wpa_ssid *remove_ssid = ssid;
  1967. id = ssid->id;
  1968. ssid = ssid->next;
  1969. wpas_notify_network_removed(wpa_s, remove_ssid);
  1970. wpa_config_remove_network(wpa_s->conf, id);
  1971. }
  1972. return 0;
  1973. }
  1974. id = atoi(cmd);
  1975. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  1976. ssid = wpa_config_get_network(wpa_s->conf, id);
  1977. if (ssid)
  1978. wpas_notify_network_removed(wpa_s, ssid);
  1979. if (ssid == NULL) {
  1980. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1981. "id=%d", id);
  1982. return -1;
  1983. }
  1984. if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
  1985. #ifdef CONFIG_SME
  1986. wpa_s->sme.prev_bssid_set = 0;
  1987. #endif /* CONFIG_SME */
  1988. /*
  1989. * Invalidate the EAP session cache if the current or
  1990. * previously used network is removed.
  1991. */
  1992. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1993. }
  1994. if (ssid == wpa_s->current_ssid) {
  1995. wpa_sm_set_config(wpa_s->wpa, NULL);
  1996. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1997. wpa_supplicant_deauthenticate(wpa_s,
  1998. WLAN_REASON_DEAUTH_LEAVING);
  1999. }
  2000. was_disabled = ssid->disabled;
  2001. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  2002. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
  2003. "network id=%d", id);
  2004. return -1;
  2005. }
  2006. if (!was_disabled && wpa_s->sched_scanning) {
  2007. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
  2008. "network from filters");
  2009. wpa_supplicant_cancel_sched_scan(wpa_s);
  2010. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2011. }
  2012. return 0;
  2013. }
  2014. static int wpa_supplicant_ctrl_iface_set_network(
  2015. struct wpa_supplicant *wpa_s, char *cmd)
  2016. {
  2017. int id;
  2018. struct wpa_ssid *ssid;
  2019. char *name, *value;
  2020. /* cmd: "<network id> <variable name> <value>" */
  2021. name = os_strchr(cmd, ' ');
  2022. if (name == NULL)
  2023. return -1;
  2024. *name++ = '\0';
  2025. value = os_strchr(name, ' ');
  2026. if (value == NULL)
  2027. return -1;
  2028. *value++ = '\0';
  2029. id = atoi(cmd);
  2030. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  2031. id, name);
  2032. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2033. (u8 *) value, os_strlen(value));
  2034. ssid = wpa_config_get_network(wpa_s->conf, id);
  2035. if (ssid == NULL) {
  2036. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2037. "id=%d", id);
  2038. return -1;
  2039. }
  2040. if (wpa_config_set(ssid, name, value, 0) < 0) {
  2041. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  2042. "variable '%s'", name);
  2043. return -1;
  2044. }
  2045. if (os_strcmp(name, "bssid") != 0 &&
  2046. os_strcmp(name, "priority") != 0)
  2047. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  2048. if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
  2049. /*
  2050. * Invalidate the EAP session cache if anything in the current
  2051. * or previously used configuration changes.
  2052. */
  2053. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2054. }
  2055. if ((os_strcmp(name, "psk") == 0 &&
  2056. value[0] == '"' && ssid->ssid_len) ||
  2057. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  2058. wpa_config_update_psk(ssid);
  2059. else if (os_strcmp(name, "priority") == 0)
  2060. wpa_config_update_prio_list(wpa_s->conf);
  2061. return 0;
  2062. }
  2063. static int wpa_supplicant_ctrl_iface_get_network(
  2064. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  2065. {
  2066. int id;
  2067. size_t res;
  2068. struct wpa_ssid *ssid;
  2069. char *name, *value;
  2070. /* cmd: "<network id> <variable name>" */
  2071. name = os_strchr(cmd, ' ');
  2072. if (name == NULL || buflen == 0)
  2073. return -1;
  2074. *name++ = '\0';
  2075. id = atoi(cmd);
  2076. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  2077. id, name);
  2078. ssid = wpa_config_get_network(wpa_s->conf, id);
  2079. if (ssid == NULL) {
  2080. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2081. "id=%d", id);
  2082. return -1;
  2083. }
  2084. value = wpa_config_get_no_key(ssid, name);
  2085. if (value == NULL) {
  2086. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2087. "variable '%s'", name);
  2088. return -1;
  2089. }
  2090. res = os_strlcpy(buf, value, buflen);
  2091. if (res >= buflen) {
  2092. os_free(value);
  2093. return -1;
  2094. }
  2095. os_free(value);
  2096. return res;
  2097. }
  2098. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  2099. char *buf, size_t buflen)
  2100. {
  2101. char *pos, *end;
  2102. struct wpa_cred *cred;
  2103. int ret;
  2104. pos = buf;
  2105. end = buf + buflen;
  2106. ret = os_snprintf(pos, end - pos,
  2107. "cred id / realm / username / domain / imsi\n");
  2108. if (ret < 0 || ret >= end - pos)
  2109. return pos - buf;
  2110. pos += ret;
  2111. cred = wpa_s->conf->cred;
  2112. while (cred) {
  2113. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  2114. cred->id, cred->realm ? cred->realm : "",
  2115. cred->username ? cred->username : "",
  2116. cred->domain ? cred->domain[0] : "",
  2117. cred->imsi ? cred->imsi : "");
  2118. if (ret < 0 || ret >= end - pos)
  2119. return pos - buf;
  2120. pos += ret;
  2121. cred = cred->next;
  2122. }
  2123. return pos - buf;
  2124. }
  2125. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  2126. char *buf, size_t buflen)
  2127. {
  2128. struct wpa_cred *cred;
  2129. int ret;
  2130. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  2131. cred = wpa_config_add_cred(wpa_s->conf);
  2132. if (cred == NULL)
  2133. return -1;
  2134. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  2135. if (ret < 0 || (size_t) ret >= buflen)
  2136. return -1;
  2137. return ret;
  2138. }
  2139. static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
  2140. struct wpa_cred *cred)
  2141. {
  2142. struct wpa_ssid *ssid;
  2143. char str[20];
  2144. if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
  2145. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2146. return -1;
  2147. }
  2148. /* Remove any network entry created based on the removed credential */
  2149. ssid = wpa_s->conf->ssid;
  2150. while (ssid) {
  2151. if (ssid->parent_cred == cred) {
  2152. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  2153. "used the removed credential", ssid->id);
  2154. os_snprintf(str, sizeof(str), "%d", ssid->id);
  2155. ssid = ssid->next;
  2156. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  2157. } else
  2158. ssid = ssid->next;
  2159. }
  2160. return 0;
  2161. }
  2162. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  2163. char *cmd)
  2164. {
  2165. int id;
  2166. struct wpa_cred *cred, *prev;
  2167. /* cmd: "<cred id>", "all", or "sp_fqdn=<FQDN>" */
  2168. if (os_strcmp(cmd, "all") == 0) {
  2169. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  2170. cred = wpa_s->conf->cred;
  2171. while (cred) {
  2172. prev = cred;
  2173. cred = cred->next;
  2174. wpas_ctrl_remove_cred(wpa_s, prev);
  2175. }
  2176. return 0;
  2177. }
  2178. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  2179. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  2180. cmd + 8);
  2181. cred = wpa_s->conf->cred;
  2182. while (cred) {
  2183. prev = cred;
  2184. cred = cred->next;
  2185. if (prev->domain) {
  2186. size_t i;
  2187. for (i = 0; i < prev->num_domain; i++) {
  2188. if (os_strcmp(prev->domain[i], cmd + 8)
  2189. != 0)
  2190. continue;
  2191. wpas_ctrl_remove_cred(wpa_s, prev);
  2192. break;
  2193. }
  2194. }
  2195. }
  2196. return 0;
  2197. }
  2198. id = atoi(cmd);
  2199. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  2200. cred = wpa_config_get_cred(wpa_s->conf, id);
  2201. return wpas_ctrl_remove_cred(wpa_s, cred);
  2202. }
  2203. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  2204. char *cmd)
  2205. {
  2206. int id;
  2207. struct wpa_cred *cred;
  2208. char *name, *value;
  2209. /* cmd: "<cred id> <variable name> <value>" */
  2210. name = os_strchr(cmd, ' ');
  2211. if (name == NULL)
  2212. return -1;
  2213. *name++ = '\0';
  2214. value = os_strchr(name, ' ');
  2215. if (value == NULL)
  2216. return -1;
  2217. *value++ = '\0';
  2218. id = atoi(cmd);
  2219. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  2220. id, name);
  2221. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2222. (u8 *) value, os_strlen(value));
  2223. cred = wpa_config_get_cred(wpa_s->conf, id);
  2224. if (cred == NULL) {
  2225. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2226. id);
  2227. return -1;
  2228. }
  2229. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  2230. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  2231. "variable '%s'", name);
  2232. return -1;
  2233. }
  2234. return 0;
  2235. }
  2236. #ifndef CONFIG_NO_CONFIG_WRITE
  2237. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  2238. {
  2239. int ret;
  2240. if (!wpa_s->conf->update_config) {
  2241. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  2242. "to update configuration (update_config=0)");
  2243. return -1;
  2244. }
  2245. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  2246. if (ret) {
  2247. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  2248. "update configuration");
  2249. } else {
  2250. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  2251. " updated");
  2252. }
  2253. return ret;
  2254. }
  2255. #endif /* CONFIG_NO_CONFIG_WRITE */
  2256. struct cipher_info {
  2257. unsigned int capa;
  2258. const char *name;
  2259. int group_only;
  2260. };
  2261. static const struct cipher_info ciphers[] = {
  2262. { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
  2263. { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
  2264. { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
  2265. { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
  2266. { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
  2267. { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
  2268. { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
  2269. { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
  2270. };
  2271. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  2272. struct wpa_driver_capa *capa,
  2273. char *buf, size_t buflen)
  2274. {
  2275. int ret, first = 1;
  2276. char *pos, *end;
  2277. size_t len;
  2278. unsigned int i;
  2279. pos = buf;
  2280. end = pos + buflen;
  2281. if (res < 0) {
  2282. if (strict)
  2283. return 0;
  2284. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  2285. if (len >= buflen)
  2286. return -1;
  2287. return len;
  2288. }
  2289. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2290. if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
  2291. ret = os_snprintf(pos, end - pos, "%s%s",
  2292. first ? "" : " ", ciphers[i].name);
  2293. if (ret < 0 || ret >= end - pos)
  2294. return pos - buf;
  2295. pos += ret;
  2296. first = 0;
  2297. }
  2298. }
  2299. return pos - buf;
  2300. }
  2301. static int ctrl_iface_get_capability_group(int res, char *strict,
  2302. struct wpa_driver_capa *capa,
  2303. char *buf, size_t buflen)
  2304. {
  2305. int ret, first = 1;
  2306. char *pos, *end;
  2307. size_t len;
  2308. unsigned int i;
  2309. pos = buf;
  2310. end = pos + buflen;
  2311. if (res < 0) {
  2312. if (strict)
  2313. return 0;
  2314. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  2315. if (len >= buflen)
  2316. return -1;
  2317. return len;
  2318. }
  2319. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2320. if (capa->enc & ciphers[i].capa) {
  2321. ret = os_snprintf(pos, end - pos, "%s%s",
  2322. first ? "" : " ", ciphers[i].name);
  2323. if (ret < 0 || ret >= end - pos)
  2324. return pos - buf;
  2325. pos += ret;
  2326. first = 0;
  2327. }
  2328. }
  2329. return pos - buf;
  2330. }
  2331. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  2332. struct wpa_driver_capa *capa,
  2333. char *buf, size_t buflen)
  2334. {
  2335. int ret;
  2336. char *pos, *end;
  2337. size_t len;
  2338. pos = buf;
  2339. end = pos + buflen;
  2340. if (res < 0) {
  2341. if (strict)
  2342. return 0;
  2343. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  2344. "NONE", buflen);
  2345. if (len >= buflen)
  2346. return -1;
  2347. return len;
  2348. }
  2349. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  2350. if (ret < 0 || ret >= end - pos)
  2351. return pos - buf;
  2352. pos += ret;
  2353. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2354. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2355. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  2356. if (ret < 0 || ret >= end - pos)
  2357. return pos - buf;
  2358. pos += ret;
  2359. }
  2360. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2361. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2362. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  2363. if (ret < 0 || ret >= end - pos)
  2364. return pos - buf;
  2365. pos += ret;
  2366. }
  2367. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2368. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  2369. if (ret < 0 || ret >= end - pos)
  2370. return pos - buf;
  2371. pos += ret;
  2372. }
  2373. return pos - buf;
  2374. }
  2375. static int ctrl_iface_get_capability_proto(int res, char *strict,
  2376. struct wpa_driver_capa *capa,
  2377. char *buf, size_t buflen)
  2378. {
  2379. int ret, first = 1;
  2380. char *pos, *end;
  2381. size_t len;
  2382. pos = buf;
  2383. end = pos + buflen;
  2384. if (res < 0) {
  2385. if (strict)
  2386. return 0;
  2387. len = os_strlcpy(buf, "RSN WPA", buflen);
  2388. if (len >= buflen)
  2389. return -1;
  2390. return len;
  2391. }
  2392. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2393. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2394. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  2395. if (ret < 0 || ret >= end - pos)
  2396. return pos - buf;
  2397. pos += ret;
  2398. first = 0;
  2399. }
  2400. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2401. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  2402. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  2403. if (ret < 0 || ret >= end - pos)
  2404. return pos - buf;
  2405. pos += ret;
  2406. first = 0;
  2407. }
  2408. return pos - buf;
  2409. }
  2410. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  2411. struct wpa_driver_capa *capa,
  2412. char *buf, size_t buflen)
  2413. {
  2414. int ret, first = 1;
  2415. char *pos, *end;
  2416. size_t len;
  2417. pos = buf;
  2418. end = pos + buflen;
  2419. if (res < 0) {
  2420. if (strict)
  2421. return 0;
  2422. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  2423. if (len >= buflen)
  2424. return -1;
  2425. return len;
  2426. }
  2427. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  2428. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  2429. if (ret < 0 || ret >= end - pos)
  2430. return pos - buf;
  2431. pos += ret;
  2432. first = 0;
  2433. }
  2434. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  2435. ret = os_snprintf(pos, end - pos, "%sSHARED",
  2436. first ? "" : " ");
  2437. if (ret < 0 || ret >= end - pos)
  2438. return pos - buf;
  2439. pos += ret;
  2440. first = 0;
  2441. }
  2442. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  2443. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  2444. if (ret < 0 || ret >= end - pos)
  2445. return pos - buf;
  2446. pos += ret;
  2447. first = 0;
  2448. }
  2449. return pos - buf;
  2450. }
  2451. static int ctrl_iface_get_capability_modes(int res, char *strict,
  2452. struct wpa_driver_capa *capa,
  2453. char *buf, size_t buflen)
  2454. {
  2455. int ret, first = 1;
  2456. char *pos, *end;
  2457. size_t len;
  2458. pos = buf;
  2459. end = pos + buflen;
  2460. if (res < 0) {
  2461. if (strict)
  2462. return 0;
  2463. len = os_strlcpy(buf, "IBSS AP", buflen);
  2464. if (len >= buflen)
  2465. return -1;
  2466. return len;
  2467. }
  2468. if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
  2469. ret = os_snprintf(pos, end - pos, "%sIBSS", first ? "" : " ");
  2470. if (ret < 0 || ret >= end - pos)
  2471. return pos - buf;
  2472. pos += ret;
  2473. first = 0;
  2474. }
  2475. if (capa->flags & WPA_DRIVER_FLAGS_AP) {
  2476. ret = os_snprintf(pos, end - pos, "%sAP", first ? "" : " ");
  2477. if (ret < 0 || ret >= end - pos)
  2478. return pos - buf;
  2479. pos += ret;
  2480. first = 0;
  2481. }
  2482. return pos - buf;
  2483. }
  2484. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  2485. char *buf, size_t buflen)
  2486. {
  2487. struct hostapd_channel_data *chnl;
  2488. int ret, i, j;
  2489. char *pos, *end, *hmode;
  2490. pos = buf;
  2491. end = pos + buflen;
  2492. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2493. switch (wpa_s->hw.modes[j].mode) {
  2494. case HOSTAPD_MODE_IEEE80211B:
  2495. hmode = "B";
  2496. break;
  2497. case HOSTAPD_MODE_IEEE80211G:
  2498. hmode = "G";
  2499. break;
  2500. case HOSTAPD_MODE_IEEE80211A:
  2501. hmode = "A";
  2502. break;
  2503. case HOSTAPD_MODE_IEEE80211AD:
  2504. hmode = "AD";
  2505. break;
  2506. default:
  2507. continue;
  2508. }
  2509. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  2510. if (ret < 0 || ret >= end - pos)
  2511. return pos - buf;
  2512. pos += ret;
  2513. chnl = wpa_s->hw.modes[j].channels;
  2514. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2515. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2516. continue;
  2517. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  2518. if (ret < 0 || ret >= end - pos)
  2519. return pos - buf;
  2520. pos += ret;
  2521. }
  2522. ret = os_snprintf(pos, end - pos, "\n");
  2523. if (ret < 0 || ret >= end - pos)
  2524. return pos - buf;
  2525. pos += ret;
  2526. }
  2527. return pos - buf;
  2528. }
  2529. static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
  2530. char *buf, size_t buflen)
  2531. {
  2532. struct hostapd_channel_data *chnl;
  2533. int ret, i, j;
  2534. char *pos, *end, *hmode;
  2535. pos = buf;
  2536. end = pos + buflen;
  2537. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2538. switch (wpa_s->hw.modes[j].mode) {
  2539. case HOSTAPD_MODE_IEEE80211B:
  2540. hmode = "B";
  2541. break;
  2542. case HOSTAPD_MODE_IEEE80211G:
  2543. hmode = "G";
  2544. break;
  2545. case HOSTAPD_MODE_IEEE80211A:
  2546. hmode = "A";
  2547. break;
  2548. case HOSTAPD_MODE_IEEE80211AD:
  2549. hmode = "AD";
  2550. break;
  2551. default:
  2552. continue;
  2553. }
  2554. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
  2555. hmode);
  2556. if (ret < 0 || ret >= end - pos)
  2557. return pos - buf;
  2558. pos += ret;
  2559. chnl = wpa_s->hw.modes[j].channels;
  2560. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2561. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2562. continue;
  2563. ret = os_snprintf(pos, end - pos, " %d = %d MHz%s\n",
  2564. chnl[i].chan, chnl[i].freq,
  2565. chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
  2566. " (NO_IBSS)" : "");
  2567. if (ret < 0 || ret >= end - pos)
  2568. return pos - buf;
  2569. pos += ret;
  2570. }
  2571. ret = os_snprintf(pos, end - pos, "\n");
  2572. if (ret < 0 || ret >= end - pos)
  2573. return pos - buf;
  2574. pos += ret;
  2575. }
  2576. return pos - buf;
  2577. }
  2578. static int wpa_supplicant_ctrl_iface_get_capability(
  2579. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  2580. size_t buflen)
  2581. {
  2582. struct wpa_driver_capa capa;
  2583. int res;
  2584. char *strict;
  2585. char field[30];
  2586. size_t len;
  2587. /* Determine whether or not strict checking was requested */
  2588. len = os_strlcpy(field, _field, sizeof(field));
  2589. if (len >= sizeof(field))
  2590. return -1;
  2591. strict = os_strchr(field, ' ');
  2592. if (strict != NULL) {
  2593. *strict++ = '\0';
  2594. if (os_strcmp(strict, "strict") != 0)
  2595. return -1;
  2596. }
  2597. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  2598. field, strict ? strict : "");
  2599. if (os_strcmp(field, "eap") == 0) {
  2600. return eap_get_names(buf, buflen);
  2601. }
  2602. res = wpa_drv_get_capa(wpa_s, &capa);
  2603. if (os_strcmp(field, "pairwise") == 0)
  2604. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  2605. buf, buflen);
  2606. if (os_strcmp(field, "group") == 0)
  2607. return ctrl_iface_get_capability_group(res, strict, &capa,
  2608. buf, buflen);
  2609. if (os_strcmp(field, "key_mgmt") == 0)
  2610. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  2611. buf, buflen);
  2612. if (os_strcmp(field, "proto") == 0)
  2613. return ctrl_iface_get_capability_proto(res, strict, &capa,
  2614. buf, buflen);
  2615. if (os_strcmp(field, "auth_alg") == 0)
  2616. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  2617. buf, buflen);
  2618. if (os_strcmp(field, "modes") == 0)
  2619. return ctrl_iface_get_capability_modes(res, strict, &capa,
  2620. buf, buflen);
  2621. if (os_strcmp(field, "channels") == 0)
  2622. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  2623. if (os_strcmp(field, "freq") == 0)
  2624. return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
  2625. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  2626. field);
  2627. return -1;
  2628. }
  2629. #ifdef CONFIG_INTERWORKING
  2630. static char * anqp_add_hex(char *pos, char *end, const char *title,
  2631. struct wpabuf *data)
  2632. {
  2633. char *start = pos;
  2634. size_t i;
  2635. int ret;
  2636. const u8 *d;
  2637. if (data == NULL)
  2638. return start;
  2639. ret = os_snprintf(pos, end - pos, "%s=", title);
  2640. if (ret < 0 || ret >= end - pos)
  2641. return start;
  2642. pos += ret;
  2643. d = wpabuf_head_u8(data);
  2644. for (i = 0; i < wpabuf_len(data); i++) {
  2645. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  2646. if (ret < 0 || ret >= end - pos)
  2647. return start;
  2648. pos += ret;
  2649. }
  2650. ret = os_snprintf(pos, end - pos, "\n");
  2651. if (ret < 0 || ret >= end - pos)
  2652. return start;
  2653. pos += ret;
  2654. return pos;
  2655. }
  2656. #endif /* CONFIG_INTERWORKING */
  2657. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  2658. unsigned long mask, char *buf, size_t buflen)
  2659. {
  2660. size_t i;
  2661. int ret;
  2662. char *pos, *end;
  2663. const u8 *ie, *ie2;
  2664. pos = buf;
  2665. end = buf + buflen;
  2666. if (mask & WPA_BSS_MASK_ID) {
  2667. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  2668. if (ret < 0 || ret >= end - pos)
  2669. return 0;
  2670. pos += ret;
  2671. }
  2672. if (mask & WPA_BSS_MASK_BSSID) {
  2673. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  2674. MAC2STR(bss->bssid));
  2675. if (ret < 0 || ret >= end - pos)
  2676. return 0;
  2677. pos += ret;
  2678. }
  2679. if (mask & WPA_BSS_MASK_FREQ) {
  2680. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  2681. if (ret < 0 || ret >= end - pos)
  2682. return 0;
  2683. pos += ret;
  2684. }
  2685. if (mask & WPA_BSS_MASK_BEACON_INT) {
  2686. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  2687. bss->beacon_int);
  2688. if (ret < 0 || ret >= end - pos)
  2689. return 0;
  2690. pos += ret;
  2691. }
  2692. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  2693. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  2694. bss->caps);
  2695. if (ret < 0 || ret >= end - pos)
  2696. return 0;
  2697. pos += ret;
  2698. }
  2699. if (mask & WPA_BSS_MASK_QUAL) {
  2700. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  2701. if (ret < 0 || ret >= end - pos)
  2702. return 0;
  2703. pos += ret;
  2704. }
  2705. if (mask & WPA_BSS_MASK_NOISE) {
  2706. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  2707. if (ret < 0 || ret >= end - pos)
  2708. return 0;
  2709. pos += ret;
  2710. }
  2711. if (mask & WPA_BSS_MASK_LEVEL) {
  2712. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  2713. if (ret < 0 || ret >= end - pos)
  2714. return 0;
  2715. pos += ret;
  2716. }
  2717. if (mask & WPA_BSS_MASK_TSF) {
  2718. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  2719. (unsigned long long) bss->tsf);
  2720. if (ret < 0 || ret >= end - pos)
  2721. return 0;
  2722. pos += ret;
  2723. }
  2724. if (mask & WPA_BSS_MASK_AGE) {
  2725. struct os_reltime now;
  2726. os_get_reltime(&now);
  2727. ret = os_snprintf(pos, end - pos, "age=%d\n",
  2728. (int) (now.sec - bss->last_update.sec));
  2729. if (ret < 0 || ret >= end - pos)
  2730. return 0;
  2731. pos += ret;
  2732. }
  2733. if (mask & WPA_BSS_MASK_IE) {
  2734. ret = os_snprintf(pos, end - pos, "ie=");
  2735. if (ret < 0 || ret >= end - pos)
  2736. return 0;
  2737. pos += ret;
  2738. ie = (const u8 *) (bss + 1);
  2739. for (i = 0; i < bss->ie_len; i++) {
  2740. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  2741. if (ret < 0 || ret >= end - pos)
  2742. return 0;
  2743. pos += ret;
  2744. }
  2745. ret = os_snprintf(pos, end - pos, "\n");
  2746. if (ret < 0 || ret >= end - pos)
  2747. return 0;
  2748. pos += ret;
  2749. }
  2750. if (mask & WPA_BSS_MASK_FLAGS) {
  2751. ret = os_snprintf(pos, end - pos, "flags=");
  2752. if (ret < 0 || ret >= end - pos)
  2753. return 0;
  2754. pos += ret;
  2755. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  2756. if (ie)
  2757. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  2758. 2 + ie[1]);
  2759. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  2760. if (ie2)
  2761. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
  2762. 2 + ie2[1]);
  2763. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  2764. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  2765. ret = os_snprintf(pos, end - pos, "[WEP]");
  2766. if (ret < 0 || ret >= end - pos)
  2767. return 0;
  2768. pos += ret;
  2769. }
  2770. if (bss->caps & IEEE80211_CAP_IBSS) {
  2771. ret = os_snprintf(pos, end - pos, "[IBSS]");
  2772. if (ret < 0 || ret >= end - pos)
  2773. return 0;
  2774. pos += ret;
  2775. }
  2776. if (bss->caps & IEEE80211_CAP_ESS) {
  2777. ret = os_snprintf(pos, end - pos, "[ESS]");
  2778. if (ret < 0 || ret >= end - pos)
  2779. return 0;
  2780. pos += ret;
  2781. }
  2782. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  2783. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  2784. ret = os_snprintf(pos, end - pos, "[P2P]");
  2785. if (ret < 0 || ret >= end - pos)
  2786. return 0;
  2787. pos += ret;
  2788. }
  2789. #ifdef CONFIG_HS20
  2790. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  2791. ret = os_snprintf(pos, end - pos, "[HS20]");
  2792. if (ret < 0 || ret >= end - pos)
  2793. return 0;
  2794. pos += ret;
  2795. }
  2796. #endif /* CONFIG_HS20 */
  2797. ret = os_snprintf(pos, end - pos, "\n");
  2798. if (ret < 0 || ret >= end - pos)
  2799. return 0;
  2800. pos += ret;
  2801. }
  2802. if (mask & WPA_BSS_MASK_SSID) {
  2803. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  2804. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  2805. if (ret < 0 || ret >= end - pos)
  2806. return 0;
  2807. pos += ret;
  2808. }
  2809. #ifdef CONFIG_WPS
  2810. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  2811. ie = (const u8 *) (bss + 1);
  2812. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  2813. if (ret < 0 || ret >= end - pos)
  2814. return 0;
  2815. pos += ret;
  2816. }
  2817. #endif /* CONFIG_WPS */
  2818. #ifdef CONFIG_P2P
  2819. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  2820. ie = (const u8 *) (bss + 1);
  2821. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  2822. if (ret < 0 || ret >= end - pos)
  2823. return 0;
  2824. pos += ret;
  2825. }
  2826. #endif /* CONFIG_P2P */
  2827. #ifdef CONFIG_WIFI_DISPLAY
  2828. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  2829. struct wpabuf *wfd;
  2830. ie = (const u8 *) (bss + 1);
  2831. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  2832. WFD_IE_VENDOR_TYPE);
  2833. if (wfd) {
  2834. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  2835. if (ret < 0 || ret >= end - pos)
  2836. return 0;
  2837. pos += ret;
  2838. pos += wpa_snprintf_hex(pos, end - pos,
  2839. wpabuf_head(wfd),
  2840. wpabuf_len(wfd));
  2841. wpabuf_free(wfd);
  2842. ret = os_snprintf(pos, end - pos, "\n");
  2843. if (ret < 0 || ret >= end - pos)
  2844. return 0;
  2845. pos += ret;
  2846. }
  2847. }
  2848. #endif /* CONFIG_WIFI_DISPLAY */
  2849. #ifdef CONFIG_INTERWORKING
  2850. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  2851. struct wpa_bss_anqp *anqp = bss->anqp;
  2852. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  2853. anqp->venue_name);
  2854. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  2855. anqp->network_auth_type);
  2856. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  2857. anqp->roaming_consortium);
  2858. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  2859. anqp->ip_addr_type_availability);
  2860. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  2861. anqp->nai_realm);
  2862. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  2863. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  2864. anqp->domain_name);
  2865. #ifdef CONFIG_HS20
  2866. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  2867. anqp->hs20_operator_friendly_name);
  2868. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  2869. anqp->hs20_wan_metrics);
  2870. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  2871. anqp->hs20_connection_capability);
  2872. #endif /* CONFIG_HS20 */
  2873. }
  2874. #endif /* CONFIG_INTERWORKING */
  2875. if (mask & WPA_BSS_MASK_DELIM) {
  2876. ret = os_snprintf(pos, end - pos, "====\n");
  2877. if (ret < 0 || ret >= end - pos)
  2878. return 0;
  2879. pos += ret;
  2880. }
  2881. return pos - buf;
  2882. }
  2883. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  2884. const char *cmd, char *buf,
  2885. size_t buflen)
  2886. {
  2887. u8 bssid[ETH_ALEN];
  2888. size_t i;
  2889. struct wpa_bss *bss;
  2890. struct wpa_bss *bsslast = NULL;
  2891. struct dl_list *next;
  2892. int ret = 0;
  2893. int len;
  2894. char *ctmp;
  2895. unsigned long mask = WPA_BSS_MASK_ALL;
  2896. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  2897. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  2898. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  2899. list_id);
  2900. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  2901. list_id);
  2902. } else { /* N1-N2 */
  2903. unsigned int id1, id2;
  2904. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  2905. wpa_printf(MSG_INFO, "Wrong BSS range "
  2906. "format");
  2907. return 0;
  2908. }
  2909. if (*(cmd + 6) == '-')
  2910. id1 = 0;
  2911. else
  2912. id1 = atoi(cmd + 6);
  2913. ctmp++;
  2914. if (*ctmp >= '0' && *ctmp <= '9')
  2915. id2 = atoi(ctmp);
  2916. else
  2917. id2 = (unsigned int) -1;
  2918. bss = wpa_bss_get_id_range(wpa_s, id1, id2);
  2919. if (id2 == (unsigned int) -1)
  2920. bsslast = dl_list_last(&wpa_s->bss_id,
  2921. struct wpa_bss,
  2922. list_id);
  2923. else {
  2924. bsslast = wpa_bss_get_id(wpa_s, id2);
  2925. if (bsslast == NULL && bss && id2 > id1) {
  2926. struct wpa_bss *tmp = bss;
  2927. for (;;) {
  2928. next = tmp->list_id.next;
  2929. if (next == &wpa_s->bss_id)
  2930. break;
  2931. tmp = dl_list_entry(
  2932. next, struct wpa_bss,
  2933. list_id);
  2934. if (tmp->id > id2)
  2935. break;
  2936. bsslast = tmp;
  2937. }
  2938. }
  2939. }
  2940. }
  2941. } else if (os_strncmp(cmd, "FIRST", 5) == 0)
  2942. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  2943. else if (os_strncmp(cmd, "LAST", 4) == 0)
  2944. bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
  2945. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  2946. i = atoi(cmd + 3);
  2947. bss = wpa_bss_get_id(wpa_s, i);
  2948. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  2949. i = atoi(cmd + 5);
  2950. bss = wpa_bss_get_id(wpa_s, i);
  2951. if (bss) {
  2952. next = bss->list_id.next;
  2953. if (next == &wpa_s->bss_id)
  2954. bss = NULL;
  2955. else
  2956. bss = dl_list_entry(next, struct wpa_bss,
  2957. list_id);
  2958. }
  2959. #ifdef CONFIG_P2P
  2960. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  2961. if (hwaddr_aton(cmd + 13, bssid) == 0)
  2962. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  2963. else
  2964. bss = NULL;
  2965. #endif /* CONFIG_P2P */
  2966. } else if (hwaddr_aton(cmd, bssid) == 0)
  2967. bss = wpa_bss_get_bssid(wpa_s, bssid);
  2968. else {
  2969. struct wpa_bss *tmp;
  2970. i = atoi(cmd);
  2971. bss = NULL;
  2972. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  2973. {
  2974. if (i-- == 0) {
  2975. bss = tmp;
  2976. break;
  2977. }
  2978. }
  2979. }
  2980. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  2981. mask = strtoul(ctmp + 5, NULL, 0x10);
  2982. if (mask == 0)
  2983. mask = WPA_BSS_MASK_ALL;
  2984. }
  2985. if (bss == NULL)
  2986. return 0;
  2987. if (bsslast == NULL)
  2988. bsslast = bss;
  2989. do {
  2990. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  2991. ret += len;
  2992. buf += len;
  2993. buflen -= len;
  2994. if (bss == bsslast) {
  2995. if ((mask & WPA_BSS_MASK_DELIM) && len &&
  2996. (bss == dl_list_last(&wpa_s->bss_id,
  2997. struct wpa_bss, list_id)))
  2998. os_snprintf(buf - 5, 5, "####\n");
  2999. break;
  3000. }
  3001. next = bss->list_id.next;
  3002. if (next == &wpa_s->bss_id)
  3003. break;
  3004. bss = dl_list_entry(next, struct wpa_bss, list_id);
  3005. } while (bss && len);
  3006. return ret;
  3007. }
  3008. static int wpa_supplicant_ctrl_iface_ap_scan(
  3009. struct wpa_supplicant *wpa_s, char *cmd)
  3010. {
  3011. int ap_scan = atoi(cmd);
  3012. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  3013. }
  3014. static int wpa_supplicant_ctrl_iface_scan_interval(
  3015. struct wpa_supplicant *wpa_s, char *cmd)
  3016. {
  3017. int scan_int = atoi(cmd);
  3018. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  3019. }
  3020. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  3021. struct wpa_supplicant *wpa_s, char *cmd)
  3022. {
  3023. int expire_age = atoi(cmd);
  3024. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  3025. }
  3026. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  3027. struct wpa_supplicant *wpa_s, char *cmd)
  3028. {
  3029. int expire_count = atoi(cmd);
  3030. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  3031. }
  3032. static int wpa_supplicant_ctrl_iface_bss_flush(
  3033. struct wpa_supplicant *wpa_s, char *cmd)
  3034. {
  3035. int flush_age = atoi(cmd);
  3036. if (flush_age == 0)
  3037. wpa_bss_flush(wpa_s);
  3038. else
  3039. wpa_bss_flush_by_age(wpa_s, flush_age);
  3040. return 0;
  3041. }
  3042. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  3043. {
  3044. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  3045. /* MLME-DELETEKEYS.request */
  3046. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  3047. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  3048. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  3049. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  3050. #ifdef CONFIG_IEEE80211W
  3051. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  3052. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  3053. #endif /* CONFIG_IEEE80211W */
  3054. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  3055. 0);
  3056. /* MLME-SETPROTECTION.request(None) */
  3057. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  3058. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  3059. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  3060. wpa_sm_drop_sa(wpa_s->wpa);
  3061. }
  3062. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  3063. char *addr)
  3064. {
  3065. #ifdef CONFIG_NO_SCAN_PROCESSING
  3066. return -1;
  3067. #else /* CONFIG_NO_SCAN_PROCESSING */
  3068. u8 bssid[ETH_ALEN];
  3069. struct wpa_bss *bss;
  3070. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3071. if (hwaddr_aton(addr, bssid)) {
  3072. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  3073. "address '%s'", addr);
  3074. return -1;
  3075. }
  3076. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  3077. if (!ssid) {
  3078. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  3079. "configuration known for the target AP");
  3080. return -1;
  3081. }
  3082. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  3083. if (!bss) {
  3084. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  3085. "from BSS table");
  3086. return -1;
  3087. }
  3088. /*
  3089. * TODO: Find best network configuration block from configuration to
  3090. * allow roaming to other networks
  3091. */
  3092. wpa_s->reassociate = 1;
  3093. wpa_supplicant_connect(wpa_s, bss, ssid);
  3094. return 0;
  3095. #endif /* CONFIG_NO_SCAN_PROCESSING */
  3096. }
  3097. #ifdef CONFIG_P2P
  3098. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  3099. {
  3100. unsigned int timeout = atoi(cmd);
  3101. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  3102. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  3103. char *pos;
  3104. unsigned int search_delay;
  3105. if (os_strstr(cmd, "type=social"))
  3106. type = P2P_FIND_ONLY_SOCIAL;
  3107. else if (os_strstr(cmd, "type=progressive"))
  3108. type = P2P_FIND_PROGRESSIVE;
  3109. pos = os_strstr(cmd, "dev_id=");
  3110. if (pos) {
  3111. pos += 7;
  3112. if (hwaddr_aton(pos, dev_id))
  3113. return -1;
  3114. _dev_id = dev_id;
  3115. }
  3116. pos = os_strstr(cmd, "delay=");
  3117. if (pos) {
  3118. pos += 6;
  3119. search_delay = atoi(pos);
  3120. } else
  3121. search_delay = wpas_p2p_search_delay(wpa_s);
  3122. return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id,
  3123. search_delay);
  3124. }
  3125. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  3126. char *buf, size_t buflen)
  3127. {
  3128. u8 addr[ETH_ALEN];
  3129. char *pos, *pos2;
  3130. char *pin = NULL;
  3131. enum p2p_wps_method wps_method;
  3132. int new_pin;
  3133. int ret;
  3134. int persistent_group, persistent_id = -1;
  3135. int join;
  3136. int auth;
  3137. int automatic;
  3138. int go_intent = -1;
  3139. int freq = 0;
  3140. int pd;
  3141. int ht40, vht;
  3142. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
  3143. * [persistent|persistent=<network id>]
  3144. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  3145. * [ht40] [vht] */
  3146. if (hwaddr_aton(cmd, addr))
  3147. return -1;
  3148. pos = cmd + 17;
  3149. if (*pos != ' ')
  3150. return -1;
  3151. pos++;
  3152. persistent_group = os_strstr(pos, " persistent") != NULL;
  3153. pos2 = os_strstr(pos, " persistent=");
  3154. if (pos2) {
  3155. struct wpa_ssid *ssid;
  3156. persistent_id = atoi(pos2 + 12);
  3157. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3158. if (ssid == NULL || ssid->disabled != 2 ||
  3159. ssid->mode != WPAS_MODE_P2P_GO) {
  3160. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  3161. "SSID id=%d for persistent P2P group (GO)",
  3162. persistent_id);
  3163. return -1;
  3164. }
  3165. }
  3166. join = os_strstr(pos, " join") != NULL;
  3167. auth = os_strstr(pos, " auth") != NULL;
  3168. automatic = os_strstr(pos, " auto") != NULL;
  3169. pd = os_strstr(pos, " provdisc") != NULL;
  3170. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3171. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3172. vht;
  3173. pos2 = os_strstr(pos, " go_intent=");
  3174. if (pos2) {
  3175. pos2 += 11;
  3176. go_intent = atoi(pos2);
  3177. if (go_intent < 0 || go_intent > 15)
  3178. return -1;
  3179. }
  3180. pos2 = os_strstr(pos, " freq=");
  3181. if (pos2) {
  3182. pos2 += 6;
  3183. freq = atoi(pos2);
  3184. if (freq <= 0)
  3185. return -1;
  3186. }
  3187. if (os_strncmp(pos, "pin", 3) == 0) {
  3188. /* Request random PIN (to be displayed) and enable the PIN */
  3189. wps_method = WPS_PIN_DISPLAY;
  3190. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  3191. wps_method = WPS_PBC;
  3192. } else {
  3193. pin = pos;
  3194. pos = os_strchr(pin, ' ');
  3195. wps_method = WPS_PIN_KEYPAD;
  3196. if (pos) {
  3197. *pos++ = '\0';
  3198. if (os_strncmp(pos, "display", 7) == 0)
  3199. wps_method = WPS_PIN_DISPLAY;
  3200. }
  3201. if (!wps_pin_str_valid(pin)) {
  3202. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  3203. return 17;
  3204. }
  3205. }
  3206. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  3207. persistent_group, automatic, join,
  3208. auth, go_intent, freq, persistent_id, pd,
  3209. ht40, vht);
  3210. if (new_pin == -2) {
  3211. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  3212. return 25;
  3213. }
  3214. if (new_pin == -3) {
  3215. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  3216. return 25;
  3217. }
  3218. if (new_pin < 0)
  3219. return -1;
  3220. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  3221. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  3222. if (ret < 0 || (size_t) ret >= buflen)
  3223. return -1;
  3224. return ret;
  3225. }
  3226. os_memcpy(buf, "OK\n", 3);
  3227. return 3;
  3228. }
  3229. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3230. {
  3231. unsigned int timeout = atoi(cmd);
  3232. return wpas_p2p_listen(wpa_s, timeout);
  3233. }
  3234. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  3235. {
  3236. u8 addr[ETH_ALEN];
  3237. char *pos;
  3238. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  3239. /* <addr> <config method> [join|auto] */
  3240. if (hwaddr_aton(cmd, addr))
  3241. return -1;
  3242. pos = cmd + 17;
  3243. if (*pos != ' ')
  3244. return -1;
  3245. pos++;
  3246. if (os_strstr(pos, " join") != NULL)
  3247. use = WPAS_P2P_PD_FOR_JOIN;
  3248. else if (os_strstr(pos, " auto") != NULL)
  3249. use = WPAS_P2P_PD_AUTO;
  3250. return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
  3251. }
  3252. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  3253. size_t buflen)
  3254. {
  3255. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3256. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  3257. ssid->passphrase == NULL)
  3258. return -1;
  3259. os_strlcpy(buf, ssid->passphrase, buflen);
  3260. return os_strlen(buf);
  3261. }
  3262. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  3263. char *buf, size_t buflen)
  3264. {
  3265. u64 ref;
  3266. int res;
  3267. u8 dst_buf[ETH_ALEN], *dst;
  3268. struct wpabuf *tlvs;
  3269. char *pos;
  3270. size_t len;
  3271. if (hwaddr_aton(cmd, dst_buf))
  3272. return -1;
  3273. dst = dst_buf;
  3274. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  3275. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  3276. dst = NULL;
  3277. pos = cmd + 17;
  3278. if (*pos != ' ')
  3279. return -1;
  3280. pos++;
  3281. if (os_strncmp(pos, "upnp ", 5) == 0) {
  3282. u8 version;
  3283. pos += 5;
  3284. if (hexstr2bin(pos, &version, 1) < 0)
  3285. return -1;
  3286. pos += 2;
  3287. if (*pos != ' ')
  3288. return -1;
  3289. pos++;
  3290. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  3291. #ifdef CONFIG_WIFI_DISPLAY
  3292. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  3293. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  3294. #endif /* CONFIG_WIFI_DISPLAY */
  3295. } else {
  3296. len = os_strlen(pos);
  3297. if (len & 1)
  3298. return -1;
  3299. len /= 2;
  3300. tlvs = wpabuf_alloc(len);
  3301. if (tlvs == NULL)
  3302. return -1;
  3303. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  3304. wpabuf_free(tlvs);
  3305. return -1;
  3306. }
  3307. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  3308. wpabuf_free(tlvs);
  3309. }
  3310. if (ref == 0)
  3311. return -1;
  3312. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  3313. if (res < 0 || (unsigned) res >= buflen)
  3314. return -1;
  3315. return res;
  3316. }
  3317. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  3318. char *cmd)
  3319. {
  3320. long long unsigned val;
  3321. u64 req;
  3322. if (sscanf(cmd, "%llx", &val) != 1)
  3323. return -1;
  3324. req = val;
  3325. return wpas_p2p_sd_cancel_request(wpa_s, req);
  3326. }
  3327. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  3328. {
  3329. int freq;
  3330. u8 dst[ETH_ALEN];
  3331. u8 dialog_token;
  3332. struct wpabuf *resp_tlvs;
  3333. char *pos, *pos2;
  3334. size_t len;
  3335. pos = os_strchr(cmd, ' ');
  3336. if (pos == NULL)
  3337. return -1;
  3338. *pos++ = '\0';
  3339. freq = atoi(cmd);
  3340. if (freq == 0)
  3341. return -1;
  3342. if (hwaddr_aton(pos, dst))
  3343. return -1;
  3344. pos += 17;
  3345. if (*pos != ' ')
  3346. return -1;
  3347. pos++;
  3348. pos2 = os_strchr(pos, ' ');
  3349. if (pos2 == NULL)
  3350. return -1;
  3351. *pos2++ = '\0';
  3352. dialog_token = atoi(pos);
  3353. len = os_strlen(pos2);
  3354. if (len & 1)
  3355. return -1;
  3356. len /= 2;
  3357. resp_tlvs = wpabuf_alloc(len);
  3358. if (resp_tlvs == NULL)
  3359. return -1;
  3360. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  3361. wpabuf_free(resp_tlvs);
  3362. return -1;
  3363. }
  3364. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  3365. wpabuf_free(resp_tlvs);
  3366. return 0;
  3367. }
  3368. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  3369. char *cmd)
  3370. {
  3371. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  3372. return -1;
  3373. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  3374. return 0;
  3375. }
  3376. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  3377. char *cmd)
  3378. {
  3379. char *pos;
  3380. size_t len;
  3381. struct wpabuf *query, *resp;
  3382. pos = os_strchr(cmd, ' ');
  3383. if (pos == NULL)
  3384. return -1;
  3385. *pos++ = '\0';
  3386. len = os_strlen(cmd);
  3387. if (len & 1)
  3388. return -1;
  3389. len /= 2;
  3390. query = wpabuf_alloc(len);
  3391. if (query == NULL)
  3392. return -1;
  3393. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3394. wpabuf_free(query);
  3395. return -1;
  3396. }
  3397. len = os_strlen(pos);
  3398. if (len & 1) {
  3399. wpabuf_free(query);
  3400. return -1;
  3401. }
  3402. len /= 2;
  3403. resp = wpabuf_alloc(len);
  3404. if (resp == NULL) {
  3405. wpabuf_free(query);
  3406. return -1;
  3407. }
  3408. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  3409. wpabuf_free(query);
  3410. wpabuf_free(resp);
  3411. return -1;
  3412. }
  3413. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  3414. wpabuf_free(query);
  3415. wpabuf_free(resp);
  3416. return -1;
  3417. }
  3418. return 0;
  3419. }
  3420. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3421. {
  3422. char *pos;
  3423. u8 version;
  3424. pos = os_strchr(cmd, ' ');
  3425. if (pos == NULL)
  3426. return -1;
  3427. *pos++ = '\0';
  3428. if (hexstr2bin(cmd, &version, 1) < 0)
  3429. return -1;
  3430. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  3431. }
  3432. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  3433. {
  3434. char *pos;
  3435. pos = os_strchr(cmd, ' ');
  3436. if (pos == NULL)
  3437. return -1;
  3438. *pos++ = '\0';
  3439. if (os_strcmp(cmd, "bonjour") == 0)
  3440. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  3441. if (os_strcmp(cmd, "upnp") == 0)
  3442. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  3443. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3444. return -1;
  3445. }
  3446. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  3447. char *cmd)
  3448. {
  3449. size_t len;
  3450. struct wpabuf *query;
  3451. int ret;
  3452. len = os_strlen(cmd);
  3453. if (len & 1)
  3454. return -1;
  3455. len /= 2;
  3456. query = wpabuf_alloc(len);
  3457. if (query == NULL)
  3458. return -1;
  3459. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3460. wpabuf_free(query);
  3461. return -1;
  3462. }
  3463. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  3464. wpabuf_free(query);
  3465. return ret;
  3466. }
  3467. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3468. {
  3469. char *pos;
  3470. u8 version;
  3471. pos = os_strchr(cmd, ' ');
  3472. if (pos == NULL)
  3473. return -1;
  3474. *pos++ = '\0';
  3475. if (hexstr2bin(cmd, &version, 1) < 0)
  3476. return -1;
  3477. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  3478. }
  3479. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  3480. {
  3481. char *pos;
  3482. pos = os_strchr(cmd, ' ');
  3483. if (pos == NULL)
  3484. return -1;
  3485. *pos++ = '\0';
  3486. if (os_strcmp(cmd, "bonjour") == 0)
  3487. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  3488. if (os_strcmp(cmd, "upnp") == 0)
  3489. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  3490. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3491. return -1;
  3492. }
  3493. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  3494. {
  3495. u8 addr[ETH_ALEN];
  3496. /* <addr> */
  3497. if (hwaddr_aton(cmd, addr))
  3498. return -1;
  3499. return wpas_p2p_reject(wpa_s, addr);
  3500. }
  3501. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  3502. {
  3503. char *pos;
  3504. int id;
  3505. struct wpa_ssid *ssid;
  3506. u8 *_peer = NULL, peer[ETH_ALEN];
  3507. int freq = 0, pref_freq = 0;
  3508. int ht40, vht;
  3509. id = atoi(cmd);
  3510. pos = os_strstr(cmd, " peer=");
  3511. if (pos) {
  3512. pos += 6;
  3513. if (hwaddr_aton(pos, peer))
  3514. return -1;
  3515. _peer = peer;
  3516. }
  3517. ssid = wpa_config_get_network(wpa_s->conf, id);
  3518. if (ssid == NULL || ssid->disabled != 2) {
  3519. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3520. "for persistent P2P group",
  3521. id);
  3522. return -1;
  3523. }
  3524. pos = os_strstr(cmd, " freq=");
  3525. if (pos) {
  3526. pos += 6;
  3527. freq = atoi(pos);
  3528. if (freq <= 0)
  3529. return -1;
  3530. }
  3531. pos = os_strstr(cmd, " pref=");
  3532. if (pos) {
  3533. pos += 6;
  3534. pref_freq = atoi(pos);
  3535. if (pref_freq <= 0)
  3536. return -1;
  3537. }
  3538. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3539. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3540. vht;
  3541. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
  3542. pref_freq);
  3543. }
  3544. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  3545. {
  3546. char *pos;
  3547. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  3548. pos = os_strstr(cmd, " peer=");
  3549. if (!pos)
  3550. return -1;
  3551. *pos = '\0';
  3552. pos += 6;
  3553. if (hwaddr_aton(pos, peer)) {
  3554. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  3555. return -1;
  3556. }
  3557. pos = os_strstr(pos, " go_dev_addr=");
  3558. if (pos) {
  3559. pos += 13;
  3560. if (hwaddr_aton(pos, go_dev_addr)) {
  3561. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  3562. pos);
  3563. return -1;
  3564. }
  3565. go_dev = go_dev_addr;
  3566. }
  3567. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  3568. }
  3569. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  3570. {
  3571. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3572. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  3573. if (os_strncmp(cmd, "group=", 6) == 0)
  3574. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  3575. return -1;
  3576. }
  3577. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  3578. char *cmd, int freq, int ht40,
  3579. int vht)
  3580. {
  3581. int id;
  3582. struct wpa_ssid *ssid;
  3583. id = atoi(cmd);
  3584. ssid = wpa_config_get_network(wpa_s->conf, id);
  3585. if (ssid == NULL || ssid->disabled != 2) {
  3586. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3587. "for persistent P2P group",
  3588. id);
  3589. return -1;
  3590. }
  3591. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40, vht,
  3592. NULL, 0);
  3593. }
  3594. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  3595. {
  3596. int freq = 0, ht40, vht;
  3597. char *pos;
  3598. pos = os_strstr(cmd, "freq=");
  3599. if (pos)
  3600. freq = atoi(pos + 5);
  3601. vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3602. ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3603. vht;
  3604. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3605. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
  3606. ht40, vht);
  3607. if (os_strcmp(cmd, "persistent") == 0 ||
  3608. os_strncmp(cmd, "persistent ", 11) == 0)
  3609. return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
  3610. if (os_strncmp(cmd, "freq=", 5) == 0)
  3611. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  3612. if (ht40)
  3613. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  3614. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  3615. cmd);
  3616. return -1;
  3617. }
  3618. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  3619. char *buf, size_t buflen)
  3620. {
  3621. u8 addr[ETH_ALEN], *addr_ptr;
  3622. int next, res;
  3623. const struct p2p_peer_info *info;
  3624. char *pos, *end;
  3625. char devtype[WPS_DEV_TYPE_BUFSIZE];
  3626. struct wpa_ssid *ssid;
  3627. size_t i;
  3628. if (!wpa_s->global->p2p)
  3629. return -1;
  3630. if (os_strcmp(cmd, "FIRST") == 0) {
  3631. addr_ptr = NULL;
  3632. next = 0;
  3633. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3634. if (hwaddr_aton(cmd + 5, addr) < 0)
  3635. return -1;
  3636. addr_ptr = addr;
  3637. next = 1;
  3638. } else {
  3639. if (hwaddr_aton(cmd, addr) < 0)
  3640. return -1;
  3641. addr_ptr = addr;
  3642. next = 0;
  3643. }
  3644. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  3645. if (info == NULL)
  3646. return -1;
  3647. pos = buf;
  3648. end = buf + buflen;
  3649. res = os_snprintf(pos, end - pos, MACSTR "\n"
  3650. "pri_dev_type=%s\n"
  3651. "device_name=%s\n"
  3652. "manufacturer=%s\n"
  3653. "model_name=%s\n"
  3654. "model_number=%s\n"
  3655. "serial_number=%s\n"
  3656. "config_methods=0x%x\n"
  3657. "dev_capab=0x%x\n"
  3658. "group_capab=0x%x\n"
  3659. "level=%d\n",
  3660. MAC2STR(info->p2p_device_addr),
  3661. wps_dev_type_bin2str(info->pri_dev_type,
  3662. devtype, sizeof(devtype)),
  3663. info->device_name,
  3664. info->manufacturer,
  3665. info->model_name,
  3666. info->model_number,
  3667. info->serial_number,
  3668. info->config_methods,
  3669. info->dev_capab,
  3670. info->group_capab,
  3671. info->level);
  3672. if (res < 0 || res >= end - pos)
  3673. return pos - buf;
  3674. pos += res;
  3675. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  3676. {
  3677. const u8 *t;
  3678. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  3679. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  3680. wps_dev_type_bin2str(t, devtype,
  3681. sizeof(devtype)));
  3682. if (res < 0 || res >= end - pos)
  3683. return pos - buf;
  3684. pos += res;
  3685. }
  3686. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  3687. if (ssid) {
  3688. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  3689. if (res < 0 || res >= end - pos)
  3690. return pos - buf;
  3691. pos += res;
  3692. }
  3693. res = p2p_get_peer_info_txt(info, pos, end - pos);
  3694. if (res < 0)
  3695. return pos - buf;
  3696. pos += res;
  3697. return pos - buf;
  3698. }
  3699. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  3700. const char *param)
  3701. {
  3702. unsigned int i;
  3703. if (wpa_s->global->p2p == NULL)
  3704. return -1;
  3705. if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
  3706. return -1;
  3707. for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
  3708. struct wpa_freq_range *freq;
  3709. freq = &wpa_s->global->p2p_disallow_freq.range[i];
  3710. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  3711. freq->min, freq->max);
  3712. }
  3713. wpas_p2p_update_channel_list(wpa_s);
  3714. return 0;
  3715. }
  3716. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  3717. {
  3718. char *param;
  3719. if (wpa_s->global->p2p == NULL)
  3720. return -1;
  3721. param = os_strchr(cmd, ' ');
  3722. if (param == NULL)
  3723. return -1;
  3724. *param++ = '\0';
  3725. if (os_strcmp(cmd, "discoverability") == 0) {
  3726. p2p_set_client_discoverability(wpa_s->global->p2p,
  3727. atoi(param));
  3728. return 0;
  3729. }
  3730. if (os_strcmp(cmd, "managed") == 0) {
  3731. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  3732. return 0;
  3733. }
  3734. if (os_strcmp(cmd, "listen_channel") == 0) {
  3735. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  3736. atoi(param));
  3737. }
  3738. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  3739. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  3740. os_strlen(param));
  3741. }
  3742. if (os_strcmp(cmd, "noa") == 0) {
  3743. char *pos;
  3744. int count, start, duration;
  3745. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  3746. count = atoi(param);
  3747. pos = os_strchr(param, ',');
  3748. if (pos == NULL)
  3749. return -1;
  3750. pos++;
  3751. start = atoi(pos);
  3752. pos = os_strchr(pos, ',');
  3753. if (pos == NULL)
  3754. return -1;
  3755. pos++;
  3756. duration = atoi(pos);
  3757. if (count < 0 || count > 255 || start < 0 || duration < 0)
  3758. return -1;
  3759. if (count == 0 && duration > 0)
  3760. return -1;
  3761. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  3762. "start=%d duration=%d", count, start, duration);
  3763. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  3764. }
  3765. if (os_strcmp(cmd, "ps") == 0)
  3766. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  3767. if (os_strcmp(cmd, "oppps") == 0)
  3768. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  3769. if (os_strcmp(cmd, "ctwindow") == 0)
  3770. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  3771. if (os_strcmp(cmd, "disabled") == 0) {
  3772. wpa_s->global->p2p_disabled = atoi(param);
  3773. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  3774. wpa_s->global->p2p_disabled ?
  3775. "disabled" : "enabled");
  3776. if (wpa_s->global->p2p_disabled) {
  3777. wpas_p2p_stop_find(wpa_s);
  3778. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  3779. p2p_flush(wpa_s->global->p2p);
  3780. }
  3781. return 0;
  3782. }
  3783. if (os_strcmp(cmd, "conc_pref") == 0) {
  3784. if (os_strcmp(param, "sta") == 0)
  3785. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  3786. else if (os_strcmp(param, "p2p") == 0)
  3787. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  3788. else {
  3789. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  3790. return -1;
  3791. }
  3792. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  3793. "%s", param);
  3794. return 0;
  3795. }
  3796. if (os_strcmp(cmd, "force_long_sd") == 0) {
  3797. wpa_s->force_long_sd = atoi(param);
  3798. return 0;
  3799. }
  3800. if (os_strcmp(cmd, "peer_filter") == 0) {
  3801. u8 addr[ETH_ALEN];
  3802. if (hwaddr_aton(param, addr))
  3803. return -1;
  3804. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  3805. return 0;
  3806. }
  3807. if (os_strcmp(cmd, "cross_connect") == 0)
  3808. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  3809. if (os_strcmp(cmd, "go_apsd") == 0) {
  3810. if (os_strcmp(param, "disable") == 0)
  3811. wpa_s->set_ap_uapsd = 0;
  3812. else {
  3813. wpa_s->set_ap_uapsd = 1;
  3814. wpa_s->ap_uapsd = atoi(param);
  3815. }
  3816. return 0;
  3817. }
  3818. if (os_strcmp(cmd, "client_apsd") == 0) {
  3819. if (os_strcmp(param, "disable") == 0)
  3820. wpa_s->set_sta_uapsd = 0;
  3821. else {
  3822. int be, bk, vi, vo;
  3823. char *pos;
  3824. /* format: BE,BK,VI,VO;max SP Length */
  3825. be = atoi(param);
  3826. pos = os_strchr(param, ',');
  3827. if (pos == NULL)
  3828. return -1;
  3829. pos++;
  3830. bk = atoi(pos);
  3831. pos = os_strchr(pos, ',');
  3832. if (pos == NULL)
  3833. return -1;
  3834. pos++;
  3835. vi = atoi(pos);
  3836. pos = os_strchr(pos, ',');
  3837. if (pos == NULL)
  3838. return -1;
  3839. pos++;
  3840. vo = atoi(pos);
  3841. /* ignore max SP Length for now */
  3842. wpa_s->set_sta_uapsd = 1;
  3843. wpa_s->sta_uapsd = 0;
  3844. if (be)
  3845. wpa_s->sta_uapsd |= BIT(0);
  3846. if (bk)
  3847. wpa_s->sta_uapsd |= BIT(1);
  3848. if (vi)
  3849. wpa_s->sta_uapsd |= BIT(2);
  3850. if (vo)
  3851. wpa_s->sta_uapsd |= BIT(3);
  3852. }
  3853. return 0;
  3854. }
  3855. if (os_strcmp(cmd, "disallow_freq") == 0)
  3856. return p2p_ctrl_disallow_freq(wpa_s, param);
  3857. if (os_strcmp(cmd, "disc_int") == 0) {
  3858. int min_disc_int, max_disc_int, max_disc_tu;
  3859. char *pos;
  3860. pos = param;
  3861. min_disc_int = atoi(pos);
  3862. pos = os_strchr(pos, ' ');
  3863. if (pos == NULL)
  3864. return -1;
  3865. *pos++ = '\0';
  3866. max_disc_int = atoi(pos);
  3867. pos = os_strchr(pos, ' ');
  3868. if (pos == NULL)
  3869. return -1;
  3870. *pos++ = '\0';
  3871. max_disc_tu = atoi(pos);
  3872. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  3873. max_disc_int, max_disc_tu);
  3874. }
  3875. if (os_strcmp(cmd, "per_sta_psk") == 0) {
  3876. wpa_s->global->p2p_per_sta_psk = !!atoi(param);
  3877. return 0;
  3878. }
  3879. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  3880. cmd);
  3881. return -1;
  3882. }
  3883. static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
  3884. {
  3885. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  3886. wpa_s->force_long_sd = 0;
  3887. if (wpa_s->global->p2p)
  3888. p2p_flush(wpa_s->global->p2p);
  3889. }
  3890. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  3891. {
  3892. char *pos, *pos2;
  3893. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  3894. if (cmd[0]) {
  3895. pos = os_strchr(cmd, ' ');
  3896. if (pos == NULL)
  3897. return -1;
  3898. *pos++ = '\0';
  3899. dur1 = atoi(cmd);
  3900. pos2 = os_strchr(pos, ' ');
  3901. if (pos2)
  3902. *pos2++ = '\0';
  3903. int1 = atoi(pos);
  3904. } else
  3905. pos2 = NULL;
  3906. if (pos2) {
  3907. pos = os_strchr(pos2, ' ');
  3908. if (pos == NULL)
  3909. return -1;
  3910. *pos++ = '\0';
  3911. dur2 = atoi(pos2);
  3912. int2 = atoi(pos);
  3913. }
  3914. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  3915. }
  3916. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3917. {
  3918. char *pos;
  3919. unsigned int period = 0, interval = 0;
  3920. if (cmd[0]) {
  3921. pos = os_strchr(cmd, ' ');
  3922. if (pos == NULL)
  3923. return -1;
  3924. *pos++ = '\0';
  3925. period = atoi(cmd);
  3926. interval = atoi(pos);
  3927. }
  3928. return wpas_p2p_ext_listen(wpa_s, period, interval);
  3929. }
  3930. static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
  3931. {
  3932. const char *pos;
  3933. u8 peer[ETH_ALEN];
  3934. int iface_addr = 0;
  3935. pos = cmd;
  3936. if (os_strncmp(pos, "iface=", 6) == 0) {
  3937. iface_addr = 1;
  3938. pos += 6;
  3939. }
  3940. if (hwaddr_aton(pos, peer))
  3941. return -1;
  3942. wpas_p2p_remove_client(wpa_s, peer, iface_addr);
  3943. return 0;
  3944. }
  3945. #endif /* CONFIG_P2P */
  3946. #ifdef CONFIG_INTERWORKING
  3947. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
  3948. {
  3949. u8 bssid[ETH_ALEN];
  3950. struct wpa_bss *bss;
  3951. if (hwaddr_aton(dst, bssid)) {
  3952. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  3953. return -1;
  3954. }
  3955. bss = wpa_bss_get_bssid(wpa_s, bssid);
  3956. if (bss == NULL) {
  3957. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  3958. MAC2STR(bssid));
  3959. return -1;
  3960. }
  3961. return interworking_connect(wpa_s, bss);
  3962. }
  3963. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  3964. {
  3965. u8 dst_addr[ETH_ALEN];
  3966. int used;
  3967. char *pos;
  3968. #define MAX_ANQP_INFO_ID 100
  3969. u16 id[MAX_ANQP_INFO_ID];
  3970. size_t num_id = 0;
  3971. used = hwaddr_aton2(dst, dst_addr);
  3972. if (used < 0)
  3973. return -1;
  3974. pos = dst + used;
  3975. while (num_id < MAX_ANQP_INFO_ID) {
  3976. id[num_id] = atoi(pos);
  3977. if (id[num_id])
  3978. num_id++;
  3979. pos = os_strchr(pos + 1, ',');
  3980. if (pos == NULL)
  3981. break;
  3982. pos++;
  3983. }
  3984. if (num_id == 0)
  3985. return -1;
  3986. return anqp_send_req(wpa_s, dst_addr, id, num_id);
  3987. }
  3988. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  3989. {
  3990. u8 dst_addr[ETH_ALEN];
  3991. struct wpabuf *advproto, *query = NULL;
  3992. int used, ret = -1;
  3993. char *pos, *end;
  3994. size_t len;
  3995. used = hwaddr_aton2(cmd, dst_addr);
  3996. if (used < 0)
  3997. return -1;
  3998. pos = cmd + used;
  3999. while (*pos == ' ')
  4000. pos++;
  4001. /* Advertisement Protocol ID */
  4002. end = os_strchr(pos, ' ');
  4003. if (end)
  4004. len = end - pos;
  4005. else
  4006. len = os_strlen(pos);
  4007. if (len & 0x01)
  4008. return -1;
  4009. len /= 2;
  4010. if (len == 0)
  4011. return -1;
  4012. advproto = wpabuf_alloc(len);
  4013. if (advproto == NULL)
  4014. return -1;
  4015. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  4016. goto fail;
  4017. if (end) {
  4018. /* Optional Query Request */
  4019. pos = end + 1;
  4020. while (*pos == ' ')
  4021. pos++;
  4022. len = os_strlen(pos);
  4023. if (len) {
  4024. if (len & 0x01)
  4025. goto fail;
  4026. len /= 2;
  4027. if (len == 0)
  4028. goto fail;
  4029. query = wpabuf_alloc(len);
  4030. if (query == NULL)
  4031. goto fail;
  4032. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  4033. goto fail;
  4034. }
  4035. }
  4036. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  4037. fail:
  4038. wpabuf_free(advproto);
  4039. wpabuf_free(query);
  4040. return ret;
  4041. }
  4042. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  4043. size_t buflen)
  4044. {
  4045. u8 addr[ETH_ALEN];
  4046. int dialog_token;
  4047. int used;
  4048. char *pos;
  4049. size_t resp_len, start, requested_len;
  4050. if (!wpa_s->last_gas_resp)
  4051. return -1;
  4052. used = hwaddr_aton2(cmd, addr);
  4053. if (used < 0)
  4054. return -1;
  4055. pos = cmd + used;
  4056. while (*pos == ' ')
  4057. pos++;
  4058. dialog_token = atoi(pos);
  4059. if (os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) != 0 ||
  4060. dialog_token != wpa_s->last_gas_dialog_token)
  4061. return -1;
  4062. resp_len = wpabuf_len(wpa_s->last_gas_resp);
  4063. start = 0;
  4064. requested_len = resp_len;
  4065. pos = os_strchr(pos, ' ');
  4066. if (pos) {
  4067. start = atoi(pos);
  4068. if (start > resp_len)
  4069. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4070. pos = os_strchr(pos, ',');
  4071. if (pos == NULL)
  4072. return -1;
  4073. pos++;
  4074. requested_len = atoi(pos);
  4075. if (start + requested_len > resp_len)
  4076. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4077. }
  4078. if (requested_len * 2 + 1 > buflen)
  4079. return os_snprintf(buf, buflen, "FAIL-Too long response");
  4080. return wpa_snprintf_hex(buf, buflen,
  4081. wpabuf_head_u8(wpa_s->last_gas_resp) + start,
  4082. requested_len);
  4083. }
  4084. #endif /* CONFIG_INTERWORKING */
  4085. #ifdef CONFIG_HS20
  4086. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4087. {
  4088. u8 dst_addr[ETH_ALEN];
  4089. int used;
  4090. char *pos;
  4091. u32 subtypes = 0;
  4092. used = hwaddr_aton2(dst, dst_addr);
  4093. if (used < 0)
  4094. return -1;
  4095. pos = dst + used;
  4096. for (;;) {
  4097. int num = atoi(pos);
  4098. if (num <= 0 || num > 31)
  4099. return -1;
  4100. subtypes |= BIT(num);
  4101. pos = os_strchr(pos + 1, ',');
  4102. if (pos == NULL)
  4103. break;
  4104. pos++;
  4105. }
  4106. if (subtypes == 0)
  4107. return -1;
  4108. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
  4109. }
  4110. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4111. const u8 *addr, const char *realm)
  4112. {
  4113. u8 *buf;
  4114. size_t rlen, len;
  4115. int ret;
  4116. rlen = os_strlen(realm);
  4117. len = 3 + rlen;
  4118. buf = os_malloc(len);
  4119. if (buf == NULL)
  4120. return -1;
  4121. buf[0] = 1; /* NAI Home Realm Count */
  4122. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  4123. buf[2] = rlen;
  4124. os_memcpy(buf + 3, realm, rlen);
  4125. ret = hs20_anqp_send_req(wpa_s, addr,
  4126. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4127. buf, len);
  4128. os_free(buf);
  4129. return ret;
  4130. }
  4131. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4132. char *dst)
  4133. {
  4134. struct wpa_cred *cred = wpa_s->conf->cred;
  4135. u8 dst_addr[ETH_ALEN];
  4136. int used;
  4137. u8 *buf;
  4138. size_t len;
  4139. int ret;
  4140. used = hwaddr_aton2(dst, dst_addr);
  4141. if (used < 0)
  4142. return -1;
  4143. while (dst[used] == ' ')
  4144. used++;
  4145. if (os_strncmp(dst + used, "realm=", 6) == 0)
  4146. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  4147. dst + used + 6);
  4148. len = os_strlen(dst + used);
  4149. if (len == 0 && cred && cred->realm)
  4150. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  4151. if (len % 1)
  4152. return -1;
  4153. len /= 2;
  4154. buf = os_malloc(len);
  4155. if (buf == NULL)
  4156. return -1;
  4157. if (hexstr2bin(dst + used, buf, len) < 0) {
  4158. os_free(buf);
  4159. return -1;
  4160. }
  4161. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  4162. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4163. buf, len);
  4164. os_free(buf);
  4165. return ret;
  4166. }
  4167. #endif /* CONFIG_HS20 */
  4168. static int wpa_supplicant_ctrl_iface_sta_autoconnect(
  4169. struct wpa_supplicant *wpa_s, char *cmd)
  4170. {
  4171. wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
  4172. return 0;
  4173. }
  4174. #ifdef CONFIG_AUTOSCAN
  4175. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  4176. char *cmd)
  4177. {
  4178. enum wpa_states state = wpa_s->wpa_state;
  4179. char *new_params = NULL;
  4180. if (os_strlen(cmd) > 0) {
  4181. new_params = os_strdup(cmd);
  4182. if (new_params == NULL)
  4183. return -1;
  4184. }
  4185. os_free(wpa_s->conf->autoscan);
  4186. wpa_s->conf->autoscan = new_params;
  4187. if (wpa_s->conf->autoscan == NULL)
  4188. autoscan_deinit(wpa_s);
  4189. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  4190. autoscan_init(wpa_s, 1);
  4191. else if (state == WPA_SCANNING)
  4192. wpa_supplicant_reinit_autoscan(wpa_s);
  4193. return 0;
  4194. }
  4195. #endif /* CONFIG_AUTOSCAN */
  4196. #ifdef CONFIG_WNM
  4197. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  4198. {
  4199. int enter;
  4200. int intval = 0;
  4201. char *pos;
  4202. int ret;
  4203. struct wpabuf *tfs_req = NULL;
  4204. if (os_strncmp(cmd, "enter", 5) == 0)
  4205. enter = 1;
  4206. else if (os_strncmp(cmd, "exit", 4) == 0)
  4207. enter = 0;
  4208. else
  4209. return -1;
  4210. pos = os_strstr(cmd, " interval=");
  4211. if (pos)
  4212. intval = atoi(pos + 10);
  4213. pos = os_strstr(cmd, " tfs_req=");
  4214. if (pos) {
  4215. char *end;
  4216. size_t len;
  4217. pos += 9;
  4218. end = os_strchr(pos, ' ');
  4219. if (end)
  4220. len = end - pos;
  4221. else
  4222. len = os_strlen(pos);
  4223. if (len & 1)
  4224. return -1;
  4225. len /= 2;
  4226. tfs_req = wpabuf_alloc(len);
  4227. if (tfs_req == NULL)
  4228. return -1;
  4229. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  4230. wpabuf_free(tfs_req);
  4231. return -1;
  4232. }
  4233. }
  4234. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  4235. WNM_SLEEP_MODE_EXIT, intval,
  4236. tfs_req);
  4237. wpabuf_free(tfs_req);
  4238. return ret;
  4239. }
  4240. static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
  4241. {
  4242. int query_reason;
  4243. query_reason = atoi(cmd);
  4244. wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
  4245. query_reason);
  4246. return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
  4247. }
  4248. #endif /* CONFIG_WNM */
  4249. /* Get string representation of channel width */
  4250. static const char * channel_width_name(enum chan_width width)
  4251. {
  4252. switch (width) {
  4253. case CHAN_WIDTH_20_NOHT:
  4254. return "20 MHz (no HT)";
  4255. case CHAN_WIDTH_20:
  4256. return "20 MHz";
  4257. case CHAN_WIDTH_40:
  4258. return "40 MHz";
  4259. case CHAN_WIDTH_80:
  4260. return "80 MHz";
  4261. case CHAN_WIDTH_80P80:
  4262. return "80+80 MHz";
  4263. case CHAN_WIDTH_160:
  4264. return "160 MHz";
  4265. default:
  4266. return "unknown";
  4267. }
  4268. }
  4269. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  4270. size_t buflen)
  4271. {
  4272. struct wpa_signal_info si;
  4273. int ret;
  4274. char *pos, *end;
  4275. ret = wpa_drv_signal_poll(wpa_s, &si);
  4276. if (ret)
  4277. return -1;
  4278. pos = buf;
  4279. end = buf + buflen;
  4280. ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
  4281. "NOISE=%d\nFREQUENCY=%u\n",
  4282. si.current_signal, si.current_txrate / 1000,
  4283. si.current_noise, si.frequency);
  4284. if (ret < 0 || ret > end - pos)
  4285. return -1;
  4286. pos += ret;
  4287. if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
  4288. ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
  4289. channel_width_name(si.chanwidth));
  4290. if (ret < 0 || ret > end - pos)
  4291. return -1;
  4292. pos += ret;
  4293. }
  4294. if (si.center_frq1 > 0 && si.center_frq2 > 0) {
  4295. ret = os_snprintf(pos, end - pos,
  4296. "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
  4297. si.center_frq1, si.center_frq2);
  4298. if (ret < 0 || ret > end - pos)
  4299. return -1;
  4300. pos += ret;
  4301. }
  4302. if (si.avg_signal) {
  4303. ret = os_snprintf(pos, end - pos,
  4304. "AVG_RSSI=%d\n", si.avg_signal);
  4305. if (ret < 0 || ret >= end - pos)
  4306. return -1;
  4307. pos += ret;
  4308. }
  4309. return pos - buf;
  4310. }
  4311. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  4312. size_t buflen)
  4313. {
  4314. struct hostap_sta_driver_data sta;
  4315. int ret;
  4316. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  4317. if (ret)
  4318. return -1;
  4319. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  4320. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  4321. if (ret < 0 || (size_t) ret > buflen)
  4322. return -1;
  4323. return ret;
  4324. }
  4325. #ifdef ANDROID
  4326. static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  4327. char *buf, size_t buflen)
  4328. {
  4329. int ret;
  4330. ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
  4331. if (ret == 0) {
  4332. if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
  4333. struct p2p_data *p2p = wpa_s->global->p2p;
  4334. if (p2p) {
  4335. char country[3];
  4336. country[0] = cmd[8];
  4337. country[1] = cmd[9];
  4338. country[2] = 0x04;
  4339. p2p_set_country(p2p, country);
  4340. }
  4341. }
  4342. ret = os_snprintf(buf, buflen, "%s\n", "OK");
  4343. }
  4344. return ret;
  4345. }
  4346. #endif /* ANDROID */
  4347. static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
  4348. {
  4349. wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
  4350. #ifdef CONFIG_P2P
  4351. wpas_p2p_stop_find(wpa_s);
  4352. p2p_ctrl_flush(wpa_s);
  4353. wpas_p2p_group_remove(wpa_s, "*");
  4354. #endif /* CONFIG_P2P */
  4355. #ifdef CONFIG_WPS_TESTING
  4356. wps_version_number = 0x20;
  4357. wps_testing_dummy_cred = 0;
  4358. #endif /* CONFIG_WPS_TESTING */
  4359. #ifdef CONFIG_WPS
  4360. wpa_s->wps_fragment_size = 0;
  4361. wpas_wps_cancel(wpa_s);
  4362. #endif /* CONFIG_WPS */
  4363. wpa_s->after_wps = 0;
  4364. wpa_s->known_wps_freq = 0;
  4365. #ifdef CONFIG_TDLS
  4366. #ifdef CONFIG_TDLS_TESTING
  4367. extern unsigned int tdls_testing;
  4368. tdls_testing = 0;
  4369. #endif /* CONFIG_TDLS_TESTING */
  4370. wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
  4371. wpa_tdls_enable(wpa_s->wpa, 1);
  4372. #endif /* CONFIG_TDLS */
  4373. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  4374. wpa_supplicant_stop_countermeasures(wpa_s, NULL);
  4375. wpa_s->no_keep_alive = 0;
  4376. os_free(wpa_s->disallow_aps_bssid);
  4377. wpa_s->disallow_aps_bssid = NULL;
  4378. wpa_s->disallow_aps_bssid_count = 0;
  4379. os_free(wpa_s->disallow_aps_ssid);
  4380. wpa_s->disallow_aps_ssid = NULL;
  4381. wpa_s->disallow_aps_ssid_count = 0;
  4382. wpa_s->set_sta_uapsd = 0;
  4383. wpa_s->sta_uapsd = 0;
  4384. wpa_drv_radio_disable(wpa_s, 0);
  4385. wpa_bss_flush(wpa_s);
  4386. wpa_blacklist_clear(wpa_s);
  4387. wpa_s->extra_blacklist_count = 0;
  4388. wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
  4389. wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
  4390. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
  4391. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
  4392. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
  4393. }
  4394. static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
  4395. {
  4396. struct wpa_supplicant *wpa_s = eloop_ctx;
  4397. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  4398. }
  4399. static int set_scan_freqs(struct wpa_supplicant *wpa_s, char *val)
  4400. {
  4401. struct wpa_freq_range_list ranges;
  4402. int *freqs = NULL;
  4403. struct hostapd_hw_modes *mode;
  4404. u16 i;
  4405. if (wpa_s->hw.modes == NULL)
  4406. return -1;
  4407. os_memset(&ranges, 0, sizeof(ranges));
  4408. if (freq_range_list_parse(&ranges, val) < 0)
  4409. return -1;
  4410. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  4411. int j;
  4412. mode = &wpa_s->hw.modes[i];
  4413. for (j = 0; j < mode->num_channels; j++) {
  4414. unsigned int freq;
  4415. if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
  4416. continue;
  4417. freq = mode->channels[j].freq;
  4418. if (!freq_range_list_includes(&ranges, freq))
  4419. continue;
  4420. int_array_add_unique(&freqs, freq);
  4421. }
  4422. }
  4423. os_free(ranges.range);
  4424. os_free(wpa_s->manual_scan_freqs);
  4425. wpa_s->manual_scan_freqs = freqs;
  4426. return 0;
  4427. }
  4428. static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
  4429. char *reply, int reply_size, int *reply_len)
  4430. {
  4431. char *pos;
  4432. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  4433. *reply_len = -1;
  4434. return;
  4435. }
  4436. wpa_s->manual_scan_passive = 0;
  4437. wpa_s->manual_scan_use_id = 0;
  4438. if (params) {
  4439. if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
  4440. wpa_s->scan_res_handler = scan_only_handler;
  4441. pos = os_strstr(params, "freq=");
  4442. if (pos && set_scan_freqs(wpa_s, pos + 5) < 0) {
  4443. *reply_len = -1;
  4444. return;
  4445. }
  4446. pos = os_strstr(params, "passive=");
  4447. if (pos)
  4448. wpa_s->manual_scan_passive = !!atoi(pos + 8);
  4449. pos = os_strstr(params, "use_id=");
  4450. if (pos)
  4451. wpa_s->manual_scan_use_id = atoi(pos + 7);
  4452. } else {
  4453. os_free(wpa_s->manual_scan_freqs);
  4454. wpa_s->manual_scan_freqs = NULL;
  4455. if (wpa_s->scan_res_handler == scan_only_handler)
  4456. wpa_s->scan_res_handler = NULL;
  4457. }
  4458. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  4459. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  4460. (wpa_s->wpa_state == WPA_COMPLETED))) {
  4461. wpa_s->normal_scans = 0;
  4462. wpa_s->scan_req = MANUAL_SCAN_REQ;
  4463. wpa_s->after_wps = 0;
  4464. wpa_s->known_wps_freq = 0;
  4465. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4466. if (wpa_s->manual_scan_use_id) {
  4467. wpa_s->manual_scan_id++;
  4468. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  4469. wpa_s->manual_scan_id);
  4470. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  4471. wpa_s->manual_scan_id);
  4472. }
  4473. } else if (wpa_s->sched_scanning) {
  4474. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
  4475. wpa_supplicant_cancel_sched_scan(wpa_s);
  4476. wpa_s->scan_req = MANUAL_SCAN_REQ;
  4477. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4478. if (wpa_s->manual_scan_use_id) {
  4479. wpa_s->manual_scan_id++;
  4480. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  4481. wpa_s->manual_scan_id);
  4482. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  4483. wpa_s->manual_scan_id);
  4484. }
  4485. } else {
  4486. wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
  4487. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  4488. }
  4489. }
  4490. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  4491. char *buf, size_t *resp_len)
  4492. {
  4493. char *reply;
  4494. const int reply_size = 4096;
  4495. int reply_len;
  4496. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  4497. os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
  4498. os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  4499. os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0 ||
  4500. os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
  4501. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  4502. (const u8 *) buf, os_strlen(buf));
  4503. } else {
  4504. int level = MSG_DEBUG;
  4505. if (os_strcmp(buf, "PING") == 0)
  4506. level = MSG_EXCESSIVE;
  4507. wpa_hexdump_ascii(level, "RX ctrl_iface",
  4508. (const u8 *) buf, os_strlen(buf));
  4509. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  4510. }
  4511. reply = os_malloc(reply_size);
  4512. if (reply == NULL) {
  4513. *resp_len = 1;
  4514. return NULL;
  4515. }
  4516. os_memcpy(reply, "OK\n", 3);
  4517. reply_len = 3;
  4518. if (os_strcmp(buf, "PING") == 0) {
  4519. os_memcpy(reply, "PONG\n", 5);
  4520. reply_len = 5;
  4521. } else if (os_strcmp(buf, "IFNAME") == 0) {
  4522. reply_len = os_strlen(wpa_s->ifname);
  4523. os_memcpy(reply, wpa_s->ifname, reply_len);
  4524. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  4525. if (wpa_debug_reopen_file() < 0)
  4526. reply_len = -1;
  4527. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  4528. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  4529. } else if (os_strcmp(buf, "MIB") == 0) {
  4530. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  4531. if (reply_len >= 0) {
  4532. int res;
  4533. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  4534. reply_size - reply_len);
  4535. if (res < 0)
  4536. reply_len = -1;
  4537. else
  4538. reply_len += res;
  4539. }
  4540. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  4541. reply_len = wpa_supplicant_ctrl_iface_status(
  4542. wpa_s, buf + 6, reply, reply_size);
  4543. } else if (os_strcmp(buf, "PMKSA") == 0) {
  4544. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  4545. reply_size);
  4546. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  4547. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  4548. reply_len = -1;
  4549. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  4550. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  4551. reply, reply_size);
  4552. } else if (os_strcmp(buf, "LOGON") == 0) {
  4553. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  4554. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  4555. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  4556. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  4557. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4558. reply_len = -1;
  4559. else
  4560. wpas_request_connection(wpa_s);
  4561. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  4562. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4563. reply_len = -1;
  4564. else if (wpa_s->disconnected)
  4565. wpas_request_connection(wpa_s);
  4566. #ifdef IEEE8021X_EAPOL
  4567. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  4568. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  4569. reply_len = -1;
  4570. #endif /* IEEE8021X_EAPOL */
  4571. #ifdef CONFIG_PEERKEY
  4572. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  4573. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  4574. reply_len = -1;
  4575. #endif /* CONFIG_PEERKEY */
  4576. #ifdef CONFIG_IEEE80211R
  4577. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  4578. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  4579. reply_len = -1;
  4580. #endif /* CONFIG_IEEE80211R */
  4581. #ifdef CONFIG_WPS
  4582. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  4583. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  4584. if (res == -2) {
  4585. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4586. reply_len = 17;
  4587. } else if (res)
  4588. reply_len = -1;
  4589. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  4590. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  4591. if (res == -2) {
  4592. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4593. reply_len = 17;
  4594. } else if (res)
  4595. reply_len = -1;
  4596. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  4597. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  4598. reply,
  4599. reply_size);
  4600. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  4601. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  4602. wpa_s, buf + 14, reply, reply_size);
  4603. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  4604. if (wpas_wps_cancel(wpa_s))
  4605. reply_len = -1;
  4606. #ifdef CONFIG_WPS_NFC
  4607. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  4608. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  4609. reply_len = -1;
  4610. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  4611. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  4612. reply_len = -1;
  4613. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  4614. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  4615. wpa_s, buf + 21, reply, reply_size);
  4616. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  4617. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  4618. wpa_s, buf + 14, reply, reply_size);
  4619. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  4620. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  4621. buf + 17))
  4622. reply_len = -1;
  4623. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  4624. reply_len = wpas_ctrl_nfc_get_handover_req(
  4625. wpa_s, buf + 21, reply, reply_size);
  4626. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  4627. reply_len = wpas_ctrl_nfc_get_handover_sel(
  4628. wpa_s, buf + 21, reply, reply_size);
  4629. } else if (os_strncmp(buf, "NFC_RX_HANDOVER_REQ ", 20) == 0) {
  4630. reply_len = wpas_ctrl_nfc_rx_handover_req(
  4631. wpa_s, buf + 20, reply, reply_size);
  4632. } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
  4633. if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
  4634. reply_len = -1;
  4635. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  4636. if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
  4637. reply_len = -1;
  4638. #endif /* CONFIG_WPS_NFC */
  4639. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  4640. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  4641. reply_len = -1;
  4642. #ifdef CONFIG_AP
  4643. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  4644. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  4645. wpa_s, buf + 11, reply, reply_size);
  4646. #endif /* CONFIG_AP */
  4647. #ifdef CONFIG_WPS_ER
  4648. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  4649. if (wpas_wps_er_start(wpa_s, NULL))
  4650. reply_len = -1;
  4651. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  4652. if (wpas_wps_er_start(wpa_s, buf + 13))
  4653. reply_len = -1;
  4654. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  4655. if (wpas_wps_er_stop(wpa_s))
  4656. reply_len = -1;
  4657. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  4658. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  4659. reply_len = -1;
  4660. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  4661. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  4662. if (ret == -2) {
  4663. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4664. reply_len = 17;
  4665. } else if (ret == -3) {
  4666. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  4667. reply_len = 18;
  4668. } else if (ret == -4) {
  4669. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  4670. reply_len = 20;
  4671. } else if (ret)
  4672. reply_len = -1;
  4673. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  4674. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  4675. reply_len = -1;
  4676. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  4677. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  4678. buf + 18))
  4679. reply_len = -1;
  4680. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  4681. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  4682. reply_len = -1;
  4683. #ifdef CONFIG_WPS_NFC
  4684. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  4685. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  4686. wpa_s, buf + 24, reply, reply_size);
  4687. #endif /* CONFIG_WPS_NFC */
  4688. #endif /* CONFIG_WPS_ER */
  4689. #endif /* CONFIG_WPS */
  4690. #ifdef CONFIG_IBSS_RSN
  4691. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  4692. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  4693. reply_len = -1;
  4694. #endif /* CONFIG_IBSS_RSN */
  4695. #ifdef CONFIG_P2P
  4696. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  4697. if (p2p_ctrl_find(wpa_s, buf + 9))
  4698. reply_len = -1;
  4699. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  4700. if (p2p_ctrl_find(wpa_s, ""))
  4701. reply_len = -1;
  4702. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  4703. wpas_p2p_stop_find(wpa_s);
  4704. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  4705. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  4706. reply_size);
  4707. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  4708. if (p2p_ctrl_listen(wpa_s, buf + 11))
  4709. reply_len = -1;
  4710. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  4711. if (p2p_ctrl_listen(wpa_s, ""))
  4712. reply_len = -1;
  4713. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  4714. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  4715. reply_len = -1;
  4716. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  4717. if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
  4718. reply_len = -1;
  4719. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  4720. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  4721. reply_len = -1;
  4722. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  4723. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  4724. reply_len = -1;
  4725. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  4726. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  4727. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  4728. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  4729. reply_size);
  4730. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  4731. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  4732. reply_len = -1;
  4733. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  4734. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  4735. reply_len = -1;
  4736. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  4737. wpas_p2p_sd_service_update(wpa_s);
  4738. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  4739. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  4740. reply_len = -1;
  4741. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  4742. wpas_p2p_service_flush(wpa_s);
  4743. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  4744. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  4745. reply_len = -1;
  4746. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  4747. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  4748. reply_len = -1;
  4749. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  4750. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  4751. reply_len = -1;
  4752. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  4753. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  4754. reply_len = -1;
  4755. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  4756. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  4757. reply_size);
  4758. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  4759. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  4760. reply_len = -1;
  4761. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  4762. p2p_ctrl_flush(wpa_s);
  4763. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  4764. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  4765. reply_len = -1;
  4766. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  4767. if (wpas_p2p_cancel(wpa_s))
  4768. reply_len = -1;
  4769. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  4770. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  4771. reply_len = -1;
  4772. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  4773. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  4774. reply_len = -1;
  4775. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  4776. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  4777. reply_len = -1;
  4778. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  4779. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  4780. reply_len = -1;
  4781. } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
  4782. if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
  4783. reply_len = -1;
  4784. #endif /* CONFIG_P2P */
  4785. #ifdef CONFIG_WIFI_DISPLAY
  4786. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  4787. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  4788. reply_len = -1;
  4789. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  4790. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  4791. reply, reply_size);
  4792. #endif /* CONFIG_WIFI_DISPLAY */
  4793. #ifdef CONFIG_INTERWORKING
  4794. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  4795. if (interworking_fetch_anqp(wpa_s) < 0)
  4796. reply_len = -1;
  4797. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  4798. interworking_stop_fetch_anqp(wpa_s);
  4799. } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
  4800. if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
  4801. NULL) < 0)
  4802. reply_len = -1;
  4803. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  4804. if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
  4805. reply_len = -1;
  4806. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  4807. if (get_anqp(wpa_s, buf + 9) < 0)
  4808. reply_len = -1;
  4809. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  4810. if (gas_request(wpa_s, buf + 12) < 0)
  4811. reply_len = -1;
  4812. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  4813. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  4814. reply_size);
  4815. #endif /* CONFIG_INTERWORKING */
  4816. #ifdef CONFIG_HS20
  4817. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  4818. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  4819. reply_len = -1;
  4820. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  4821. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  4822. reply_len = -1;
  4823. #endif /* CONFIG_HS20 */
  4824. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  4825. {
  4826. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  4827. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  4828. reply_len = -1;
  4829. else {
  4830. /*
  4831. * Notify response from timeout to allow the control
  4832. * interface response to be sent first.
  4833. */
  4834. eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
  4835. wpa_s, NULL);
  4836. }
  4837. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  4838. if (wpa_supplicant_reload_configuration(wpa_s))
  4839. reply_len = -1;
  4840. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  4841. wpa_supplicant_terminate_proc(wpa_s->global);
  4842. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  4843. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  4844. reply_len = -1;
  4845. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  4846. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  4847. wpa_s, buf + 9, reply, reply_size);
  4848. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  4849. reply_len = wpa_supplicant_ctrl_iface_log_level(
  4850. wpa_s, buf + 9, reply, reply_size);
  4851. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  4852. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  4853. wpa_s, reply, reply_size);
  4854. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  4855. #ifdef CONFIG_SME
  4856. wpa_s->sme.prev_bssid_set = 0;
  4857. #endif /* CONFIG_SME */
  4858. wpa_s->reassociate = 0;
  4859. wpa_s->disconnected = 1;
  4860. wpa_supplicant_cancel_sched_scan(wpa_s);
  4861. wpa_supplicant_cancel_scan(wpa_s);
  4862. wpa_supplicant_deauthenticate(wpa_s,
  4863. WLAN_REASON_DEAUTH_LEAVING);
  4864. } else if (os_strcmp(buf, "SCAN") == 0) {
  4865. wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
  4866. } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
  4867. wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
  4868. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  4869. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  4870. wpa_s, reply, reply_size);
  4871. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  4872. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  4873. reply_len = -1;
  4874. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  4875. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  4876. reply_len = -1;
  4877. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  4878. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  4879. reply_len = -1;
  4880. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  4881. reply_len = wpa_supplicant_ctrl_iface_add_network(
  4882. wpa_s, reply, reply_size);
  4883. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  4884. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  4885. reply_len = -1;
  4886. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  4887. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  4888. reply_len = -1;
  4889. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  4890. reply_len = wpa_supplicant_ctrl_iface_get_network(
  4891. wpa_s, buf + 12, reply, reply_size);
  4892. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  4893. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  4894. wpa_s, reply, reply_size);
  4895. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  4896. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  4897. wpa_s, reply, reply_size);
  4898. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  4899. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  4900. reply_len = -1;
  4901. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  4902. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  4903. reply_len = -1;
  4904. #ifndef CONFIG_NO_CONFIG_WRITE
  4905. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  4906. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  4907. reply_len = -1;
  4908. #endif /* CONFIG_NO_CONFIG_WRITE */
  4909. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  4910. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  4911. wpa_s, buf + 15, reply, reply_size);
  4912. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  4913. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  4914. reply_len = -1;
  4915. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  4916. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  4917. reply_len = -1;
  4918. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  4919. reply_len = wpa_supplicant_global_iface_list(
  4920. wpa_s->global, reply, reply_size);
  4921. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  4922. reply_len = wpa_supplicant_global_iface_interfaces(
  4923. wpa_s->global, reply, reply_size);
  4924. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  4925. reply_len = wpa_supplicant_ctrl_iface_bss(
  4926. wpa_s, buf + 4, reply, reply_size);
  4927. #ifdef CONFIG_AP
  4928. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  4929. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  4930. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  4931. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  4932. reply_size);
  4933. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  4934. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  4935. reply_size);
  4936. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  4937. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  4938. reply_len = -1;
  4939. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  4940. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  4941. reply_len = -1;
  4942. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  4943. if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
  4944. reply_len = -1;
  4945. #endif /* CONFIG_AP */
  4946. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  4947. wpas_notify_suspend(wpa_s->global);
  4948. } else if (os_strcmp(buf, "RESUME") == 0) {
  4949. wpas_notify_resume(wpa_s->global);
  4950. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  4951. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  4952. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  4953. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  4954. reply_len = -1;
  4955. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  4956. if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
  4957. reply_len = -1;
  4958. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  4959. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  4960. reply_len = -1;
  4961. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  4962. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  4963. buf + 17))
  4964. reply_len = -1;
  4965. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  4966. if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
  4967. reply_len = -1;
  4968. #ifdef CONFIG_TDLS
  4969. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  4970. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  4971. reply_len = -1;
  4972. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  4973. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  4974. reply_len = -1;
  4975. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  4976. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  4977. reply_len = -1;
  4978. #endif /* CONFIG_TDLS */
  4979. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  4980. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  4981. reply_size);
  4982. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  4983. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  4984. reply_size);
  4985. #ifdef CONFIG_AUTOSCAN
  4986. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  4987. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  4988. reply_len = -1;
  4989. #endif /* CONFIG_AUTOSCAN */
  4990. #ifdef ANDROID
  4991. } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
  4992. reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
  4993. reply_size);
  4994. #endif /* ANDROID */
  4995. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  4996. pmksa_cache_clear_current(wpa_s->wpa);
  4997. eapol_sm_request_reauth(wpa_s->eapol);
  4998. #ifdef CONFIG_WNM
  4999. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  5000. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  5001. reply_len = -1;
  5002. } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
  5003. if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
  5004. reply_len = -1;
  5005. #endif /* CONFIG_WNM */
  5006. } else if (os_strcmp(buf, "FLUSH") == 0) {
  5007. wpa_supplicant_ctrl_iface_flush(wpa_s);
  5008. } else {
  5009. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  5010. reply_len = 16;
  5011. }
  5012. if (reply_len < 0) {
  5013. os_memcpy(reply, "FAIL\n", 5);
  5014. reply_len = 5;
  5015. }
  5016. *resp_len = reply_len;
  5017. return reply;
  5018. }
  5019. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  5020. char *cmd)
  5021. {
  5022. struct wpa_interface iface;
  5023. char *pos;
  5024. /*
  5025. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  5026. * TAB<bridge_ifname>
  5027. */
  5028. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  5029. os_memset(&iface, 0, sizeof(iface));
  5030. do {
  5031. iface.ifname = pos = cmd;
  5032. pos = os_strchr(pos, '\t');
  5033. if (pos)
  5034. *pos++ = '\0';
  5035. if (iface.ifname[0] == '\0')
  5036. return -1;
  5037. if (pos == NULL)
  5038. break;
  5039. iface.confname = pos;
  5040. pos = os_strchr(pos, '\t');
  5041. if (pos)
  5042. *pos++ = '\0';
  5043. if (iface.confname[0] == '\0')
  5044. iface.confname = NULL;
  5045. if (pos == NULL)
  5046. break;
  5047. iface.driver = pos;
  5048. pos = os_strchr(pos, '\t');
  5049. if (pos)
  5050. *pos++ = '\0';
  5051. if (iface.driver[0] == '\0')
  5052. iface.driver = NULL;
  5053. if (pos == NULL)
  5054. break;
  5055. iface.ctrl_interface = pos;
  5056. pos = os_strchr(pos, '\t');
  5057. if (pos)
  5058. *pos++ = '\0';
  5059. if (iface.ctrl_interface[0] == '\0')
  5060. iface.ctrl_interface = NULL;
  5061. if (pos == NULL)
  5062. break;
  5063. iface.driver_param = pos;
  5064. pos = os_strchr(pos, '\t');
  5065. if (pos)
  5066. *pos++ = '\0';
  5067. if (iface.driver_param[0] == '\0')
  5068. iface.driver_param = NULL;
  5069. if (pos == NULL)
  5070. break;
  5071. iface.bridge_ifname = pos;
  5072. pos = os_strchr(pos, '\t');
  5073. if (pos)
  5074. *pos++ = '\0';
  5075. if (iface.bridge_ifname[0] == '\0')
  5076. iface.bridge_ifname = NULL;
  5077. if (pos == NULL)
  5078. break;
  5079. } while (0);
  5080. if (wpa_supplicant_get_iface(global, iface.ifname))
  5081. return -1;
  5082. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  5083. }
  5084. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  5085. char *cmd)
  5086. {
  5087. struct wpa_supplicant *wpa_s;
  5088. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  5089. wpa_s = wpa_supplicant_get_iface(global, cmd);
  5090. if (wpa_s == NULL)
  5091. return -1;
  5092. return wpa_supplicant_remove_iface(global, wpa_s, 0);
  5093. }
  5094. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  5095. {
  5096. struct wpa_interface_info *prev;
  5097. while (iface) {
  5098. prev = iface;
  5099. iface = iface->next;
  5100. os_free(prev->ifname);
  5101. os_free(prev->desc);
  5102. os_free(prev);
  5103. }
  5104. }
  5105. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  5106. char *buf, int len)
  5107. {
  5108. int i, res;
  5109. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  5110. char *pos, *end;
  5111. for (i = 0; wpa_drivers[i]; i++) {
  5112. struct wpa_driver_ops *drv = wpa_drivers[i];
  5113. if (drv->get_interfaces == NULL)
  5114. continue;
  5115. tmp = drv->get_interfaces(global->drv_priv[i]);
  5116. if (tmp == NULL)
  5117. continue;
  5118. if (last == NULL)
  5119. iface = last = tmp;
  5120. else
  5121. last->next = tmp;
  5122. while (last->next)
  5123. last = last->next;
  5124. }
  5125. pos = buf;
  5126. end = buf + len;
  5127. for (tmp = iface; tmp; tmp = tmp->next) {
  5128. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  5129. tmp->drv_name, tmp->ifname,
  5130. tmp->desc ? tmp->desc : "");
  5131. if (res < 0 || res >= end - pos) {
  5132. *pos = '\0';
  5133. break;
  5134. }
  5135. pos += res;
  5136. }
  5137. wpa_free_iface_info(iface);
  5138. return pos - buf;
  5139. }
  5140. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  5141. char *buf, int len)
  5142. {
  5143. int res;
  5144. char *pos, *end;
  5145. struct wpa_supplicant *wpa_s;
  5146. wpa_s = global->ifaces;
  5147. pos = buf;
  5148. end = buf + len;
  5149. while (wpa_s) {
  5150. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  5151. if (res < 0 || res >= end - pos) {
  5152. *pos = '\0';
  5153. break;
  5154. }
  5155. pos += res;
  5156. wpa_s = wpa_s->next;
  5157. }
  5158. return pos - buf;
  5159. }
  5160. static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
  5161. const char *ifname,
  5162. char *cmd, size_t *resp_len)
  5163. {
  5164. struct wpa_supplicant *wpa_s;
  5165. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5166. if (os_strcmp(ifname, wpa_s->ifname) == 0)
  5167. break;
  5168. }
  5169. if (wpa_s == NULL) {
  5170. char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
  5171. if (resp)
  5172. *resp_len = os_strlen(resp);
  5173. else
  5174. *resp_len = 1;
  5175. return resp;
  5176. }
  5177. return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
  5178. }
  5179. static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
  5180. char *buf, size_t *resp_len)
  5181. {
  5182. #ifdef CONFIG_P2P
  5183. static const char * cmd[] = {
  5184. "P2P_FIND",
  5185. "P2P_STOP_FIND",
  5186. "P2P_LISTEN",
  5187. "P2P_GROUP_ADD",
  5188. "P2P_GET_PASSPHRASE",
  5189. "P2P_SERVICE_UPDATE",
  5190. "P2P_SERVICE_FLUSH",
  5191. "P2P_FLUSH",
  5192. "P2P_CANCEL",
  5193. "P2P_PRESENCE_REQ",
  5194. "P2P_EXT_LISTEN",
  5195. NULL
  5196. };
  5197. static const char * prefix[] = {
  5198. "P2P_FIND ",
  5199. "P2P_CONNECT ",
  5200. "P2P_LISTEN ",
  5201. "P2P_GROUP_REMOVE ",
  5202. "P2P_GROUP_ADD ",
  5203. "P2P_PROV_DISC ",
  5204. "P2P_SERV_DISC_REQ ",
  5205. "P2P_SERV_DISC_CANCEL_REQ ",
  5206. "P2P_SERV_DISC_RESP ",
  5207. "P2P_SERV_DISC_EXTERNAL ",
  5208. "P2P_SERVICE_ADD ",
  5209. "P2P_SERVICE_DEL ",
  5210. "P2P_REJECT ",
  5211. "P2P_INVITE ",
  5212. "P2P_PEER ",
  5213. "P2P_SET ",
  5214. "P2P_UNAUTHORIZE ",
  5215. "P2P_PRESENCE_REQ ",
  5216. "P2P_EXT_LISTEN ",
  5217. "P2P_REMOVE_CLIENT ",
  5218. NULL
  5219. };
  5220. int found = 0;
  5221. int i;
  5222. if (global->p2p_init_wpa_s == NULL)
  5223. return NULL;
  5224. for (i = 0; !found && cmd[i]; i++) {
  5225. if (os_strcmp(buf, cmd[i]) == 0)
  5226. found = 1;
  5227. }
  5228. for (i = 0; !found && prefix[i]; i++) {
  5229. if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
  5230. found = 1;
  5231. }
  5232. if (found)
  5233. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  5234. buf, resp_len);
  5235. #endif /* CONFIG_P2P */
  5236. return NULL;
  5237. }
  5238. static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
  5239. char *buf, size_t *resp_len)
  5240. {
  5241. #ifdef CONFIG_WIFI_DISPLAY
  5242. if (global->p2p_init_wpa_s == NULL)
  5243. return NULL;
  5244. if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
  5245. os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
  5246. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  5247. buf, resp_len);
  5248. #endif /* CONFIG_WIFI_DISPLAY */
  5249. return NULL;
  5250. }
  5251. static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
  5252. char *buf, size_t *resp_len)
  5253. {
  5254. char *ret;
  5255. ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
  5256. if (ret)
  5257. return ret;
  5258. ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
  5259. if (ret)
  5260. return ret;
  5261. return NULL;
  5262. }
  5263. static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
  5264. {
  5265. char *value;
  5266. value = os_strchr(cmd, ' ');
  5267. if (value == NULL)
  5268. return -1;
  5269. *value++ = '\0';
  5270. wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
  5271. #ifdef CONFIG_WIFI_DISPLAY
  5272. if (os_strcasecmp(cmd, "wifi_display") == 0) {
  5273. wifi_display_enable(global, !!atoi(value));
  5274. return 0;
  5275. }
  5276. #endif /* CONFIG_WIFI_DISPLAY */
  5277. return -1;
  5278. }
  5279. #ifndef CONFIG_NO_CONFIG_WRITE
  5280. static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
  5281. {
  5282. int ret = 0;
  5283. struct wpa_supplicant *wpa_s;
  5284. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5285. if (!wpa_s->conf->update_config) {
  5286. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
  5287. continue;
  5288. }
  5289. if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  5290. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
  5291. ret = 1;
  5292. } else {
  5293. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
  5294. }
  5295. }
  5296. return ret;
  5297. }
  5298. #endif /* CONFIG_NO_CONFIG_WRITE */
  5299. static int wpas_global_ctrl_iface_status(struct wpa_global *global,
  5300. char *buf, size_t buflen)
  5301. {
  5302. char *pos, *end;
  5303. int ret;
  5304. struct wpa_supplicant *wpa_s;
  5305. pos = buf;
  5306. end = buf + buflen;
  5307. #ifdef CONFIG_P2P
  5308. if (global->p2p && !global->p2p_disabled) {
  5309. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  5310. "\n"
  5311. "p2p_state=%s\n",
  5312. MAC2STR(global->p2p_dev_addr),
  5313. p2p_get_state_txt(global->p2p));
  5314. if (ret < 0 || ret >= end - pos)
  5315. return pos - buf;
  5316. pos += ret;
  5317. } else if (global->p2p) {
  5318. ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
  5319. if (ret < 0 || ret >= end - pos)
  5320. return pos - buf;
  5321. pos += ret;
  5322. }
  5323. #endif /* CONFIG_P2P */
  5324. #ifdef CONFIG_WIFI_DISPLAY
  5325. ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
  5326. !!global->wifi_display);
  5327. if (ret < 0 || ret >= end - pos)
  5328. return pos - buf;
  5329. pos += ret;
  5330. #endif /* CONFIG_WIFI_DISPLAY */
  5331. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5332. ret = os_snprintf(pos, end - pos, "ifname=%s\n"
  5333. "address=" MACSTR "\n",
  5334. wpa_s->ifname, MAC2STR(wpa_s->own_addr));
  5335. if (ret < 0 || ret >= end - pos)
  5336. return pos - buf;
  5337. pos += ret;
  5338. }
  5339. return pos - buf;
  5340. }
  5341. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  5342. char *buf, size_t *resp_len)
  5343. {
  5344. char *reply;
  5345. const int reply_size = 2048;
  5346. int reply_len;
  5347. int level = MSG_DEBUG;
  5348. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  5349. char *pos = os_strchr(buf + 7, ' ');
  5350. if (pos) {
  5351. *pos++ = '\0';
  5352. return wpas_global_ctrl_iface_ifname(global,
  5353. buf + 7, pos,
  5354. resp_len);
  5355. }
  5356. }
  5357. reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
  5358. if (reply)
  5359. return reply;
  5360. if (os_strcmp(buf, "PING") == 0)
  5361. level = MSG_EXCESSIVE;
  5362. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  5363. (const u8 *) buf, os_strlen(buf));
  5364. reply = os_malloc(reply_size);
  5365. if (reply == NULL) {
  5366. *resp_len = 1;
  5367. return NULL;
  5368. }
  5369. os_memcpy(reply, "OK\n", 3);
  5370. reply_len = 3;
  5371. if (os_strcmp(buf, "PING") == 0) {
  5372. os_memcpy(reply, "PONG\n", 5);
  5373. reply_len = 5;
  5374. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  5375. if (wpa_supplicant_global_iface_add(global, buf + 14))
  5376. reply_len = -1;
  5377. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  5378. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  5379. reply_len = -1;
  5380. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  5381. reply_len = wpa_supplicant_global_iface_list(
  5382. global, reply, reply_size);
  5383. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  5384. reply_len = wpa_supplicant_global_iface_interfaces(
  5385. global, reply, reply_size);
  5386. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  5387. wpa_supplicant_terminate_proc(global);
  5388. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  5389. wpas_notify_suspend(global);
  5390. } else if (os_strcmp(buf, "RESUME") == 0) {
  5391. wpas_notify_resume(global);
  5392. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  5393. if (wpas_global_ctrl_iface_set(global, buf + 4))
  5394. reply_len = -1;
  5395. #ifndef CONFIG_NO_CONFIG_WRITE
  5396. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  5397. if (wpas_global_ctrl_iface_save_config(global))
  5398. reply_len = -1;
  5399. #endif /* CONFIG_NO_CONFIG_WRITE */
  5400. } else if (os_strcmp(buf, "STATUS") == 0) {
  5401. reply_len = wpas_global_ctrl_iface_status(global, reply,
  5402. reply_size);
  5403. } else {
  5404. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  5405. reply_len = 16;
  5406. }
  5407. if (reply_len < 0) {
  5408. os_memcpy(reply, "FAIL\n", 5);
  5409. reply_len = 5;
  5410. }
  5411. *resp_len = reply_len;
  5412. return reply;
  5413. }