ctrl_iface.c 145 KB

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