ctrl_iface.c 145 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212
  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. if (os_strcmp(params, "-DRIVER") == 0)
  1180. return wpa_drv_status(wpa_s, buf, buflen);
  1181. verbose = os_strcmp(params, "-VERBOSE") == 0;
  1182. wps = os_strcmp(params, "-WPS") == 0;
  1183. pos = buf;
  1184. end = buf + buflen;
  1185. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  1186. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1187. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  1188. MAC2STR(wpa_s->bssid));
  1189. if (ret < 0 || ret >= end - pos)
  1190. return pos - buf;
  1191. pos += ret;
  1192. if (ssid) {
  1193. u8 *_ssid = ssid->ssid;
  1194. size_t ssid_len = ssid->ssid_len;
  1195. u8 ssid_buf[MAX_SSID_LEN];
  1196. if (ssid_len == 0) {
  1197. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  1198. if (_res < 0)
  1199. ssid_len = 0;
  1200. else
  1201. ssid_len = _res;
  1202. _ssid = ssid_buf;
  1203. }
  1204. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  1205. wpa_ssid_txt(_ssid, ssid_len),
  1206. ssid->id);
  1207. if (ret < 0 || ret >= end - pos)
  1208. return pos - buf;
  1209. pos += ret;
  1210. if (wps && ssid->passphrase &&
  1211. wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
  1212. (ssid->mode == WPAS_MODE_AP ||
  1213. ssid->mode == WPAS_MODE_P2P_GO)) {
  1214. ret = os_snprintf(pos, end - pos,
  1215. "passphrase=%s\n",
  1216. ssid->passphrase);
  1217. if (ret < 0 || ret >= end - pos)
  1218. return pos - buf;
  1219. pos += ret;
  1220. }
  1221. if (ssid->id_str) {
  1222. ret = os_snprintf(pos, end - pos,
  1223. "id_str=%s\n",
  1224. ssid->id_str);
  1225. if (ret < 0 || ret >= end - pos)
  1226. return pos - buf;
  1227. pos += ret;
  1228. }
  1229. switch (ssid->mode) {
  1230. case WPAS_MODE_INFRA:
  1231. ret = os_snprintf(pos, end - pos,
  1232. "mode=station\n");
  1233. break;
  1234. case WPAS_MODE_IBSS:
  1235. ret = os_snprintf(pos, end - pos,
  1236. "mode=IBSS\n");
  1237. break;
  1238. case WPAS_MODE_AP:
  1239. ret = os_snprintf(pos, end - pos,
  1240. "mode=AP\n");
  1241. break;
  1242. case WPAS_MODE_P2P_GO:
  1243. ret = os_snprintf(pos, end - pos,
  1244. "mode=P2P GO\n");
  1245. break;
  1246. case WPAS_MODE_P2P_GROUP_FORMATION:
  1247. ret = os_snprintf(pos, end - pos,
  1248. "mode=P2P GO - group "
  1249. "formation\n");
  1250. break;
  1251. default:
  1252. ret = 0;
  1253. break;
  1254. }
  1255. if (ret < 0 || ret >= end - pos)
  1256. return pos - buf;
  1257. pos += ret;
  1258. }
  1259. #ifdef CONFIG_AP
  1260. if (wpa_s->ap_iface) {
  1261. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  1262. end - pos,
  1263. verbose);
  1264. } else
  1265. #endif /* CONFIG_AP */
  1266. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1267. }
  1268. #ifdef CONFIG_SAE
  1269. if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
  1270. #ifdef CONFIG_AP
  1271. !wpa_s->ap_iface &&
  1272. #endif /* CONFIG_AP */
  1273. wpa_s->sme.sae.state == SAE_ACCEPTED) {
  1274. ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
  1275. wpa_s->sme.sae.group);
  1276. if (ret < 0 || ret >= end - pos)
  1277. return pos - buf;
  1278. pos += ret;
  1279. }
  1280. #endif /* CONFIG_SAE */
  1281. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  1282. wpa_supplicant_state_txt(wpa_s->wpa_state));
  1283. if (ret < 0 || ret >= end - pos)
  1284. return pos - buf;
  1285. pos += ret;
  1286. if (wpa_s->l2 &&
  1287. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  1288. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  1289. if (ret < 0 || ret >= end - pos)
  1290. return pos - buf;
  1291. pos += ret;
  1292. }
  1293. #ifdef CONFIG_P2P
  1294. if (wpa_s->global->p2p) {
  1295. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  1296. "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
  1297. if (ret < 0 || ret >= end - pos)
  1298. return pos - buf;
  1299. pos += ret;
  1300. }
  1301. #endif /* CONFIG_P2P */
  1302. ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
  1303. MAC2STR(wpa_s->own_addr));
  1304. if (ret < 0 || ret >= end - pos)
  1305. return pos - buf;
  1306. pos += ret;
  1307. #ifdef CONFIG_HS20
  1308. if (wpa_s->current_bss &&
  1309. wpa_bss_get_vendor_ie(wpa_s->current_bss, HS20_IE_VENDOR_TYPE) &&
  1310. wpa_s->wpa_proto == WPA_PROTO_RSN &&
  1311. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1312. ret = os_snprintf(pos, end - pos, "hs20=1\n");
  1313. if (ret < 0 || ret >= end - pos)
  1314. return pos - buf;
  1315. pos += ret;
  1316. }
  1317. if (wpa_s->current_ssid) {
  1318. struct wpa_cred *cred;
  1319. char *type;
  1320. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1321. size_t i;
  1322. if (wpa_s->current_ssid->parent_cred != cred)
  1323. continue;
  1324. for (i = 0; cred->domain && i < cred->num_domain; i++) {
  1325. ret = os_snprintf(pos, end - pos,
  1326. "home_sp=%s\n",
  1327. cred->domain[i]);
  1328. if (ret < 0 || ret >= end - pos)
  1329. return pos - buf;
  1330. pos += ret;
  1331. }
  1332. if (wpa_s->current_bss == NULL ||
  1333. wpa_s->current_bss->anqp == NULL)
  1334. res = -1;
  1335. else
  1336. res = interworking_home_sp_cred(
  1337. wpa_s, cred,
  1338. wpa_s->current_bss->anqp->domain_name);
  1339. if (res > 0)
  1340. type = "home";
  1341. else if (res == 0)
  1342. type = "roaming";
  1343. else
  1344. type = "unknown";
  1345. ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
  1346. if (ret < 0 || ret >= end - pos)
  1347. return pos - buf;
  1348. pos += ret;
  1349. break;
  1350. }
  1351. }
  1352. #endif /* CONFIG_HS20 */
  1353. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1354. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1355. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  1356. verbose);
  1357. if (res >= 0)
  1358. pos += res;
  1359. }
  1360. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1361. if (res >= 0)
  1362. pos += res;
  1363. return pos - buf;
  1364. }
  1365. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  1366. char *cmd)
  1367. {
  1368. char *pos;
  1369. int id;
  1370. struct wpa_ssid *ssid;
  1371. u8 bssid[ETH_ALEN];
  1372. /* cmd: "<network id> <BSSID>" */
  1373. pos = os_strchr(cmd, ' ');
  1374. if (pos == NULL)
  1375. return -1;
  1376. *pos++ = '\0';
  1377. id = atoi(cmd);
  1378. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  1379. if (hwaddr_aton(pos, bssid)) {
  1380. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  1381. return -1;
  1382. }
  1383. ssid = wpa_config_get_network(wpa_s->conf, id);
  1384. if (ssid == NULL) {
  1385. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1386. "to update", id);
  1387. return -1;
  1388. }
  1389. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  1390. ssid->bssid_set = !is_zero_ether_addr(bssid);
  1391. return 0;
  1392. }
  1393. static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
  1394. char *cmd, char *buf,
  1395. size_t buflen)
  1396. {
  1397. u8 bssid[ETH_ALEN];
  1398. struct wpa_blacklist *e;
  1399. char *pos, *end;
  1400. int ret;
  1401. /* cmd: "BLACKLIST [<BSSID>]" */
  1402. if (*cmd == '\0') {
  1403. pos = buf;
  1404. end = buf + buflen;
  1405. e = wpa_s->blacklist;
  1406. while (e) {
  1407. ret = os_snprintf(pos, end - pos, MACSTR "\n",
  1408. MAC2STR(e->bssid));
  1409. if (ret < 0 || ret >= end - pos)
  1410. return pos - buf;
  1411. pos += ret;
  1412. e = e->next;
  1413. }
  1414. return pos - buf;
  1415. }
  1416. cmd++;
  1417. if (os_strncmp(cmd, "clear", 5) == 0) {
  1418. wpa_blacklist_clear(wpa_s);
  1419. os_memcpy(buf, "OK\n", 3);
  1420. return 3;
  1421. }
  1422. wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
  1423. if (hwaddr_aton(cmd, bssid)) {
  1424. wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
  1425. return -1;
  1426. }
  1427. /*
  1428. * Add the BSSID twice, so its count will be 2, causing it to be
  1429. * skipped when processing scan results.
  1430. */
  1431. ret = wpa_blacklist_add(wpa_s, bssid);
  1432. if (ret != 0)
  1433. return -1;
  1434. ret = wpa_blacklist_add(wpa_s, bssid);
  1435. if (ret != 0)
  1436. return -1;
  1437. os_memcpy(buf, "OK\n", 3);
  1438. return 3;
  1439. }
  1440. extern int wpa_debug_level;
  1441. extern int wpa_debug_timestamp;
  1442. static const char * debug_level_str(int level)
  1443. {
  1444. switch (level) {
  1445. case MSG_EXCESSIVE:
  1446. return "EXCESSIVE";
  1447. case MSG_MSGDUMP:
  1448. return "MSGDUMP";
  1449. case MSG_DEBUG:
  1450. return "DEBUG";
  1451. case MSG_INFO:
  1452. return "INFO";
  1453. case MSG_WARNING:
  1454. return "WARNING";
  1455. case MSG_ERROR:
  1456. return "ERROR";
  1457. default:
  1458. return "?";
  1459. }
  1460. }
  1461. static int str_to_debug_level(const char *s)
  1462. {
  1463. if (os_strcasecmp(s, "EXCESSIVE") == 0)
  1464. return MSG_EXCESSIVE;
  1465. if (os_strcasecmp(s, "MSGDUMP") == 0)
  1466. return MSG_MSGDUMP;
  1467. if (os_strcasecmp(s, "DEBUG") == 0)
  1468. return MSG_DEBUG;
  1469. if (os_strcasecmp(s, "INFO") == 0)
  1470. return MSG_INFO;
  1471. if (os_strcasecmp(s, "WARNING") == 0)
  1472. return MSG_WARNING;
  1473. if (os_strcasecmp(s, "ERROR") == 0)
  1474. return MSG_ERROR;
  1475. return -1;
  1476. }
  1477. static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
  1478. char *cmd, char *buf,
  1479. size_t buflen)
  1480. {
  1481. char *pos, *end, *stamp;
  1482. int ret;
  1483. if (cmd == NULL) {
  1484. return -1;
  1485. }
  1486. /* cmd: "LOG_LEVEL [<level>]" */
  1487. if (*cmd == '\0') {
  1488. pos = buf;
  1489. end = buf + buflen;
  1490. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  1491. "Timestamp: %d\n",
  1492. debug_level_str(wpa_debug_level),
  1493. wpa_debug_timestamp);
  1494. if (ret < 0 || ret >= end - pos)
  1495. ret = 0;
  1496. return ret;
  1497. }
  1498. while (*cmd == ' ')
  1499. cmd++;
  1500. stamp = os_strchr(cmd, ' ');
  1501. if (stamp) {
  1502. *stamp++ = '\0';
  1503. while (*stamp == ' ') {
  1504. stamp++;
  1505. }
  1506. }
  1507. if (cmd && os_strlen(cmd)) {
  1508. int level = str_to_debug_level(cmd);
  1509. if (level < 0)
  1510. return -1;
  1511. wpa_debug_level = level;
  1512. }
  1513. if (stamp && os_strlen(stamp))
  1514. wpa_debug_timestamp = atoi(stamp);
  1515. os_memcpy(buf, "OK\n", 3);
  1516. return 3;
  1517. }
  1518. static int wpa_supplicant_ctrl_iface_list_networks(
  1519. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1520. {
  1521. char *pos, *end;
  1522. struct wpa_ssid *ssid;
  1523. int ret;
  1524. pos = buf;
  1525. end = buf + buflen;
  1526. ret = os_snprintf(pos, end - pos,
  1527. "network id / ssid / bssid / flags\n");
  1528. if (ret < 0 || ret >= end - pos)
  1529. return pos - buf;
  1530. pos += ret;
  1531. ssid = wpa_s->conf->ssid;
  1532. while (ssid) {
  1533. ret = os_snprintf(pos, end - pos, "%d\t%s",
  1534. ssid->id,
  1535. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1536. if (ret < 0 || ret >= end - pos)
  1537. return pos - buf;
  1538. pos += ret;
  1539. if (ssid->bssid_set) {
  1540. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  1541. MAC2STR(ssid->bssid));
  1542. } else {
  1543. ret = os_snprintf(pos, end - pos, "\tany");
  1544. }
  1545. if (ret < 0 || ret >= end - pos)
  1546. return pos - buf;
  1547. pos += ret;
  1548. ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
  1549. ssid == wpa_s->current_ssid ?
  1550. "[CURRENT]" : "",
  1551. ssid->disabled ? "[DISABLED]" : "",
  1552. ssid->disabled_until.sec ?
  1553. "[TEMP-DISABLED]" : "",
  1554. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  1555. "");
  1556. if (ret < 0 || ret >= end - pos)
  1557. return pos - buf;
  1558. pos += ret;
  1559. ret = os_snprintf(pos, end - pos, "\n");
  1560. if (ret < 0 || ret >= end - pos)
  1561. return pos - buf;
  1562. pos += ret;
  1563. ssid = ssid->next;
  1564. }
  1565. return pos - buf;
  1566. }
  1567. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  1568. {
  1569. int ret;
  1570. ret = os_snprintf(pos, end - pos, "-");
  1571. if (ret < 0 || ret >= end - pos)
  1572. return pos;
  1573. pos += ret;
  1574. ret = wpa_write_ciphers(pos, end, cipher, "+");
  1575. if (ret < 0)
  1576. return pos;
  1577. pos += ret;
  1578. return pos;
  1579. }
  1580. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  1581. const u8 *ie, size_t ie_len)
  1582. {
  1583. struct wpa_ie_data data;
  1584. int first, ret;
  1585. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  1586. if (ret < 0 || ret >= end - pos)
  1587. return pos;
  1588. pos += ret;
  1589. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  1590. ret = os_snprintf(pos, end - pos, "?]");
  1591. if (ret < 0 || ret >= end - pos)
  1592. return pos;
  1593. pos += ret;
  1594. return pos;
  1595. }
  1596. first = 1;
  1597. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  1598. ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
  1599. if (ret < 0 || ret >= end - pos)
  1600. return pos;
  1601. pos += ret;
  1602. first = 0;
  1603. }
  1604. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  1605. ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
  1606. if (ret < 0 || ret >= end - pos)
  1607. return pos;
  1608. pos += ret;
  1609. first = 0;
  1610. }
  1611. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  1612. ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
  1613. if (ret < 0 || ret >= end - pos)
  1614. return pos;
  1615. pos += ret;
  1616. first = 0;
  1617. }
  1618. #ifdef CONFIG_IEEE80211R
  1619. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  1620. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  1621. first ? "" : "+");
  1622. if (ret < 0 || ret >= end - pos)
  1623. return pos;
  1624. pos += ret;
  1625. first = 0;
  1626. }
  1627. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  1628. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  1629. first ? "" : "+");
  1630. if (ret < 0 || ret >= end - pos)
  1631. return pos;
  1632. pos += ret;
  1633. first = 0;
  1634. }
  1635. #endif /* CONFIG_IEEE80211R */
  1636. #ifdef CONFIG_IEEE80211W
  1637. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  1638. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  1639. first ? "" : "+");
  1640. if (ret < 0 || ret >= end - pos)
  1641. return pos;
  1642. pos += ret;
  1643. first = 0;
  1644. }
  1645. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  1646. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  1647. first ? "" : "+");
  1648. if (ret < 0 || ret >= end - pos)
  1649. return pos;
  1650. pos += ret;
  1651. first = 0;
  1652. }
  1653. #endif /* CONFIG_IEEE80211W */
  1654. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  1655. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  1656. ret = os_snprintf(pos, end - pos, "-preauth");
  1657. if (ret < 0 || ret >= end - pos)
  1658. return pos;
  1659. pos += ret;
  1660. }
  1661. ret = os_snprintf(pos, end - pos, "]");
  1662. if (ret < 0 || ret >= end - pos)
  1663. return pos;
  1664. pos += ret;
  1665. return pos;
  1666. }
  1667. #ifdef CONFIG_WPS
  1668. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  1669. char *pos, char *end,
  1670. struct wpabuf *wps_ie)
  1671. {
  1672. int ret;
  1673. const char *txt;
  1674. if (wps_ie == NULL)
  1675. return pos;
  1676. if (wps_is_selected_pbc_registrar(wps_ie))
  1677. txt = "[WPS-PBC]";
  1678. #ifdef CONFIG_WPS2
  1679. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  1680. txt = "[WPS-AUTH]";
  1681. #endif /* CONFIG_WPS2 */
  1682. else if (wps_is_selected_pin_registrar(wps_ie))
  1683. txt = "[WPS-PIN]";
  1684. else
  1685. txt = "[WPS]";
  1686. ret = os_snprintf(pos, end - pos, "%s", txt);
  1687. if (ret >= 0 && ret < end - pos)
  1688. pos += ret;
  1689. wpabuf_free(wps_ie);
  1690. return pos;
  1691. }
  1692. #endif /* CONFIG_WPS */
  1693. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  1694. char *pos, char *end,
  1695. const struct wpa_bss *bss)
  1696. {
  1697. #ifdef CONFIG_WPS
  1698. struct wpabuf *wps_ie;
  1699. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1700. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  1701. #else /* CONFIG_WPS */
  1702. return pos;
  1703. #endif /* CONFIG_WPS */
  1704. }
  1705. /* Format one result on one text line into a buffer. */
  1706. static int wpa_supplicant_ctrl_iface_scan_result(
  1707. struct wpa_supplicant *wpa_s,
  1708. const struct wpa_bss *bss, char *buf, size_t buflen)
  1709. {
  1710. char *pos, *end;
  1711. int ret;
  1712. const u8 *ie, *ie2, *p2p;
  1713. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  1714. if (!p2p)
  1715. p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
  1716. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  1717. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  1718. 0)
  1719. return 0; /* Do not show P2P listen discovery results here */
  1720. pos = buf;
  1721. end = buf + buflen;
  1722. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  1723. MAC2STR(bss->bssid), bss->freq, bss->level);
  1724. if (ret < 0 || ret >= end - pos)
  1725. return -1;
  1726. pos += ret;
  1727. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1728. if (ie)
  1729. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1730. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1731. if (ie2)
  1732. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1733. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1734. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1735. ret = os_snprintf(pos, end - pos, "[WEP]");
  1736. if (ret < 0 || ret >= end - pos)
  1737. return -1;
  1738. pos += ret;
  1739. }
  1740. if (bss->caps & IEEE80211_CAP_IBSS) {
  1741. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1742. if (ret < 0 || ret >= end - pos)
  1743. return -1;
  1744. pos += ret;
  1745. }
  1746. if (bss->caps & IEEE80211_CAP_ESS) {
  1747. ret = os_snprintf(pos, end - pos, "[ESS]");
  1748. if (ret < 0 || ret >= end - pos)
  1749. return -1;
  1750. pos += ret;
  1751. }
  1752. if (p2p) {
  1753. ret = os_snprintf(pos, end - pos, "[P2P]");
  1754. if (ret < 0 || ret >= end - pos)
  1755. return -1;
  1756. pos += ret;
  1757. }
  1758. #ifdef CONFIG_HS20
  1759. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  1760. ret = os_snprintf(pos, end - pos, "[HS20]");
  1761. if (ret < 0 || ret >= end - pos)
  1762. return -1;
  1763. pos += ret;
  1764. }
  1765. #endif /* CONFIG_HS20 */
  1766. ret = os_snprintf(pos, end - pos, "\t%s",
  1767. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1768. if (ret < 0 || ret >= end - pos)
  1769. return -1;
  1770. pos += ret;
  1771. ret = os_snprintf(pos, end - pos, "\n");
  1772. if (ret < 0 || ret >= end - pos)
  1773. return -1;
  1774. pos += ret;
  1775. return pos - buf;
  1776. }
  1777. static int wpa_supplicant_ctrl_iface_scan_results(
  1778. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1779. {
  1780. char *pos, *end;
  1781. struct wpa_bss *bss;
  1782. int ret;
  1783. pos = buf;
  1784. end = buf + buflen;
  1785. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  1786. "flags / ssid\n");
  1787. if (ret < 0 || ret >= end - pos)
  1788. return pos - buf;
  1789. pos += ret;
  1790. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  1791. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  1792. end - pos);
  1793. if (ret < 0 || ret >= end - pos)
  1794. return pos - buf;
  1795. pos += ret;
  1796. }
  1797. return pos - buf;
  1798. }
  1799. static int wpa_supplicant_ctrl_iface_select_network(
  1800. struct wpa_supplicant *wpa_s, char *cmd)
  1801. {
  1802. int id;
  1803. struct wpa_ssid *ssid;
  1804. /* cmd: "<network id>" or "any" */
  1805. if (os_strcmp(cmd, "any") == 0) {
  1806. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  1807. ssid = NULL;
  1808. } else {
  1809. id = atoi(cmd);
  1810. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  1811. ssid = wpa_config_get_network(wpa_s->conf, id);
  1812. if (ssid == NULL) {
  1813. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1814. "network id=%d", id);
  1815. return -1;
  1816. }
  1817. if (ssid->disabled == 2) {
  1818. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1819. "SELECT_NETWORK with persistent P2P group");
  1820. return -1;
  1821. }
  1822. }
  1823. wpa_supplicant_select_network(wpa_s, ssid);
  1824. return 0;
  1825. }
  1826. static int wpa_supplicant_ctrl_iface_enable_network(
  1827. struct wpa_supplicant *wpa_s, char *cmd)
  1828. {
  1829. int id;
  1830. struct wpa_ssid *ssid;
  1831. /* cmd: "<network id>" or "all" */
  1832. if (os_strcmp(cmd, "all") == 0) {
  1833. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  1834. ssid = NULL;
  1835. } else {
  1836. id = atoi(cmd);
  1837. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  1838. ssid = wpa_config_get_network(wpa_s->conf, id);
  1839. if (ssid == NULL) {
  1840. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1841. "network id=%d", id);
  1842. return -1;
  1843. }
  1844. if (ssid->disabled == 2) {
  1845. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1846. "ENABLE_NETWORK with persistent P2P group");
  1847. return -1;
  1848. }
  1849. if (os_strstr(cmd, " no-connect")) {
  1850. ssid->disabled = 0;
  1851. return 0;
  1852. }
  1853. }
  1854. wpa_supplicant_enable_network(wpa_s, ssid);
  1855. return 0;
  1856. }
  1857. static int wpa_supplicant_ctrl_iface_disable_network(
  1858. struct wpa_supplicant *wpa_s, char *cmd)
  1859. {
  1860. int id;
  1861. struct wpa_ssid *ssid;
  1862. /* cmd: "<network id>" or "all" */
  1863. if (os_strcmp(cmd, "all") == 0) {
  1864. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  1865. ssid = NULL;
  1866. } else {
  1867. id = atoi(cmd);
  1868. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  1869. ssid = wpa_config_get_network(wpa_s->conf, id);
  1870. if (ssid == NULL) {
  1871. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1872. "network id=%d", id);
  1873. return -1;
  1874. }
  1875. if (ssid->disabled == 2) {
  1876. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1877. "DISABLE_NETWORK with persistent P2P "
  1878. "group");
  1879. return -1;
  1880. }
  1881. }
  1882. wpa_supplicant_disable_network(wpa_s, ssid);
  1883. return 0;
  1884. }
  1885. static int wpa_supplicant_ctrl_iface_add_network(
  1886. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1887. {
  1888. struct wpa_ssid *ssid;
  1889. int ret;
  1890. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  1891. ssid = wpa_config_add_network(wpa_s->conf);
  1892. if (ssid == NULL)
  1893. return -1;
  1894. wpas_notify_network_added(wpa_s, ssid);
  1895. ssid->disabled = 1;
  1896. wpa_config_set_network_defaults(ssid);
  1897. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  1898. if (ret < 0 || (size_t) ret >= buflen)
  1899. return -1;
  1900. return ret;
  1901. }
  1902. static int wpa_supplicant_ctrl_iface_remove_network(
  1903. struct wpa_supplicant *wpa_s, char *cmd)
  1904. {
  1905. int id;
  1906. struct wpa_ssid *ssid;
  1907. int was_disabled;
  1908. /* cmd: "<network id>" or "all" */
  1909. if (os_strcmp(cmd, "all") == 0) {
  1910. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  1911. if (wpa_s->sched_scanning)
  1912. wpa_supplicant_cancel_sched_scan(wpa_s);
  1913. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1914. if (wpa_s->current_ssid) {
  1915. #ifdef CONFIG_SME
  1916. wpa_s->sme.prev_bssid_set = 0;
  1917. #endif /* CONFIG_SME */
  1918. wpa_sm_set_config(wpa_s->wpa, NULL);
  1919. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1920. wpa_supplicant_deauthenticate(
  1921. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1922. }
  1923. ssid = wpa_s->conf->ssid;
  1924. while (ssid) {
  1925. struct wpa_ssid *remove_ssid = ssid;
  1926. id = ssid->id;
  1927. ssid = ssid->next;
  1928. wpas_notify_network_removed(wpa_s, remove_ssid);
  1929. wpa_config_remove_network(wpa_s->conf, id);
  1930. }
  1931. return 0;
  1932. }
  1933. id = atoi(cmd);
  1934. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  1935. ssid = wpa_config_get_network(wpa_s->conf, id);
  1936. if (ssid)
  1937. wpas_notify_network_removed(wpa_s, ssid);
  1938. if (ssid == NULL) {
  1939. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1940. "id=%d", id);
  1941. return -1;
  1942. }
  1943. if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
  1944. #ifdef CONFIG_SME
  1945. wpa_s->sme.prev_bssid_set = 0;
  1946. #endif /* CONFIG_SME */
  1947. /*
  1948. * Invalidate the EAP session cache if the current or
  1949. * previously used network is removed.
  1950. */
  1951. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1952. }
  1953. if (ssid == wpa_s->current_ssid) {
  1954. wpa_sm_set_config(wpa_s->wpa, NULL);
  1955. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1956. wpa_supplicant_deauthenticate(wpa_s,
  1957. WLAN_REASON_DEAUTH_LEAVING);
  1958. }
  1959. was_disabled = ssid->disabled;
  1960. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  1961. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
  1962. "network id=%d", id);
  1963. return -1;
  1964. }
  1965. if (!was_disabled && wpa_s->sched_scanning) {
  1966. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
  1967. "network from filters");
  1968. wpa_supplicant_cancel_sched_scan(wpa_s);
  1969. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1970. }
  1971. return 0;
  1972. }
  1973. static int wpa_supplicant_ctrl_iface_set_network(
  1974. struct wpa_supplicant *wpa_s, char *cmd)
  1975. {
  1976. int id;
  1977. struct wpa_ssid *ssid;
  1978. char *name, *value;
  1979. /* cmd: "<network id> <variable name> <value>" */
  1980. name = os_strchr(cmd, ' ');
  1981. if (name == NULL)
  1982. return -1;
  1983. *name++ = '\0';
  1984. value = os_strchr(name, ' ');
  1985. if (value == NULL)
  1986. return -1;
  1987. *value++ = '\0';
  1988. id = atoi(cmd);
  1989. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  1990. id, name);
  1991. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1992. (u8 *) value, os_strlen(value));
  1993. ssid = wpa_config_get_network(wpa_s->conf, id);
  1994. if (ssid == NULL) {
  1995. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1996. "id=%d", id);
  1997. return -1;
  1998. }
  1999. if (wpa_config_set(ssid, name, value, 0) < 0) {
  2000. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  2001. "variable '%s'", name);
  2002. return -1;
  2003. }
  2004. if (os_strcmp(name, "bssid") != 0 &&
  2005. os_strcmp(name, "priority") != 0)
  2006. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  2007. if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
  2008. /*
  2009. * Invalidate the EAP session cache if anything in the current
  2010. * or previously used configuration changes.
  2011. */
  2012. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2013. }
  2014. if ((os_strcmp(name, "psk") == 0 &&
  2015. value[0] == '"' && ssid->ssid_len) ||
  2016. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  2017. wpa_config_update_psk(ssid);
  2018. else if (os_strcmp(name, "priority") == 0)
  2019. wpa_config_update_prio_list(wpa_s->conf);
  2020. return 0;
  2021. }
  2022. static int wpa_supplicant_ctrl_iface_get_network(
  2023. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  2024. {
  2025. int id;
  2026. size_t res;
  2027. struct wpa_ssid *ssid;
  2028. char *name, *value;
  2029. /* cmd: "<network id> <variable name>" */
  2030. name = os_strchr(cmd, ' ');
  2031. if (name == NULL || buflen == 0)
  2032. return -1;
  2033. *name++ = '\0';
  2034. id = atoi(cmd);
  2035. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  2036. id, name);
  2037. ssid = wpa_config_get_network(wpa_s->conf, id);
  2038. if (ssid == NULL) {
  2039. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2040. "id=%d", id);
  2041. return -1;
  2042. }
  2043. value = wpa_config_get_no_key(ssid, name);
  2044. if (value == NULL) {
  2045. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2046. "variable '%s'", name);
  2047. return -1;
  2048. }
  2049. res = os_strlcpy(buf, value, buflen);
  2050. if (res >= buflen) {
  2051. os_free(value);
  2052. return -1;
  2053. }
  2054. os_free(value);
  2055. return res;
  2056. }
  2057. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  2058. char *buf, size_t buflen)
  2059. {
  2060. char *pos, *end;
  2061. struct wpa_cred *cred;
  2062. int ret;
  2063. pos = buf;
  2064. end = buf + buflen;
  2065. ret = os_snprintf(pos, end - pos,
  2066. "cred id / realm / username / domain / imsi\n");
  2067. if (ret < 0 || ret >= end - pos)
  2068. return pos - buf;
  2069. pos += ret;
  2070. cred = wpa_s->conf->cred;
  2071. while (cred) {
  2072. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  2073. cred->id, cred->realm ? cred->realm : "",
  2074. cred->username ? cred->username : "",
  2075. cred->domain ? cred->domain[0] : "",
  2076. cred->imsi ? cred->imsi : "");
  2077. if (ret < 0 || ret >= end - pos)
  2078. return pos - buf;
  2079. pos += ret;
  2080. cred = cred->next;
  2081. }
  2082. return pos - buf;
  2083. }
  2084. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  2085. char *buf, size_t buflen)
  2086. {
  2087. struct wpa_cred *cred;
  2088. int ret;
  2089. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  2090. cred = wpa_config_add_cred(wpa_s->conf);
  2091. if (cred == NULL)
  2092. return -1;
  2093. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  2094. if (ret < 0 || (size_t) ret >= buflen)
  2095. return -1;
  2096. return ret;
  2097. }
  2098. static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
  2099. struct wpa_cred *cred)
  2100. {
  2101. struct wpa_ssid *ssid;
  2102. char str[20];
  2103. if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
  2104. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2105. return -1;
  2106. }
  2107. /* Remove any network entry created based on the removed credential */
  2108. ssid = wpa_s->conf->ssid;
  2109. while (ssid) {
  2110. if (ssid->parent_cred == cred) {
  2111. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  2112. "used the removed credential", ssid->id);
  2113. os_snprintf(str, sizeof(str), "%d", ssid->id);
  2114. ssid = ssid->next;
  2115. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  2116. } else
  2117. ssid = ssid->next;
  2118. }
  2119. return 0;
  2120. }
  2121. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  2122. char *cmd)
  2123. {
  2124. int id;
  2125. struct wpa_cred *cred, *prev;
  2126. /* cmd: "<cred id>", "all", or "sp_fqdn=<FQDN>" */
  2127. if (os_strcmp(cmd, "all") == 0) {
  2128. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  2129. cred = wpa_s->conf->cred;
  2130. while (cred) {
  2131. prev = cred;
  2132. cred = cred->next;
  2133. wpas_ctrl_remove_cred(wpa_s, prev);
  2134. }
  2135. return 0;
  2136. }
  2137. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  2138. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  2139. cmd + 8);
  2140. cred = wpa_s->conf->cred;
  2141. while (cred) {
  2142. prev = cred;
  2143. cred = cred->next;
  2144. if (prev->domain) {
  2145. size_t i;
  2146. for (i = 0; i < prev->num_domain; i++) {
  2147. if (os_strcmp(prev->domain[i], cmd + 8)
  2148. != 0)
  2149. continue;
  2150. wpas_ctrl_remove_cred(wpa_s, prev);
  2151. break;
  2152. }
  2153. }
  2154. }
  2155. return 0;
  2156. }
  2157. id = atoi(cmd);
  2158. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  2159. cred = wpa_config_get_cred(wpa_s->conf, id);
  2160. return wpas_ctrl_remove_cred(wpa_s, cred);
  2161. }
  2162. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  2163. char *cmd)
  2164. {
  2165. int id;
  2166. struct wpa_cred *cred;
  2167. char *name, *value;
  2168. /* cmd: "<cred id> <variable name> <value>" */
  2169. name = os_strchr(cmd, ' ');
  2170. if (name == NULL)
  2171. return -1;
  2172. *name++ = '\0';
  2173. value = os_strchr(name, ' ');
  2174. if (value == NULL)
  2175. return -1;
  2176. *value++ = '\0';
  2177. id = atoi(cmd);
  2178. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  2179. id, name);
  2180. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2181. (u8 *) value, os_strlen(value));
  2182. cred = wpa_config_get_cred(wpa_s->conf, id);
  2183. if (cred == NULL) {
  2184. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2185. id);
  2186. return -1;
  2187. }
  2188. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  2189. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  2190. "variable '%s'", name);
  2191. return -1;
  2192. }
  2193. return 0;
  2194. }
  2195. #ifndef CONFIG_NO_CONFIG_WRITE
  2196. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  2197. {
  2198. int ret;
  2199. if (!wpa_s->conf->update_config) {
  2200. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  2201. "to update configuration (update_config=0)");
  2202. return -1;
  2203. }
  2204. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  2205. if (ret) {
  2206. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  2207. "update configuration");
  2208. } else {
  2209. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  2210. " updated");
  2211. }
  2212. return ret;
  2213. }
  2214. #endif /* CONFIG_NO_CONFIG_WRITE */
  2215. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  2216. struct wpa_driver_capa *capa,
  2217. char *buf, size_t buflen)
  2218. {
  2219. int ret, first = 1;
  2220. char *pos, *end;
  2221. size_t len;
  2222. pos = buf;
  2223. end = pos + buflen;
  2224. if (res < 0) {
  2225. if (strict)
  2226. return 0;
  2227. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  2228. if (len >= buflen)
  2229. return -1;
  2230. return len;
  2231. }
  2232. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  2233. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  2234. if (ret < 0 || ret >= end - pos)
  2235. return pos - buf;
  2236. pos += ret;
  2237. first = 0;
  2238. }
  2239. if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
  2240. ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
  2241. if (ret < 0 || ret >= end - pos)
  2242. return pos - buf;
  2243. pos += ret;
  2244. first = 0;
  2245. }
  2246. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  2247. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  2248. if (ret < 0 || ret >= end - pos)
  2249. return pos - buf;
  2250. pos += ret;
  2251. first = 0;
  2252. }
  2253. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2254. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
  2255. if (ret < 0 || ret >= end - pos)
  2256. return pos - buf;
  2257. pos += ret;
  2258. first = 0;
  2259. }
  2260. return pos - buf;
  2261. }
  2262. static int ctrl_iface_get_capability_group(int res, char *strict,
  2263. struct wpa_driver_capa *capa,
  2264. char *buf, size_t buflen)
  2265. {
  2266. int ret, first = 1;
  2267. char *pos, *end;
  2268. size_t len;
  2269. pos = buf;
  2270. end = pos + buflen;
  2271. if (res < 0) {
  2272. if (strict)
  2273. return 0;
  2274. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  2275. if (len >= buflen)
  2276. return -1;
  2277. return len;
  2278. }
  2279. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  2280. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  2281. if (ret < 0 || ret >= end - pos)
  2282. return pos - buf;
  2283. pos += ret;
  2284. first = 0;
  2285. }
  2286. if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
  2287. ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
  2288. if (ret < 0 || ret >= end - pos)
  2289. return pos - buf;
  2290. pos += ret;
  2291. first = 0;
  2292. }
  2293. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  2294. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  2295. if (ret < 0 || ret >= end - pos)
  2296. return pos - buf;
  2297. pos += ret;
  2298. first = 0;
  2299. }
  2300. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  2301. ret = os_snprintf(pos, end - pos, "%sWEP104",
  2302. first ? "" : " ");
  2303. if (ret < 0 || ret >= end - pos)
  2304. return pos - buf;
  2305. pos += ret;
  2306. first = 0;
  2307. }
  2308. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  2309. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
  2310. if (ret < 0 || ret >= end - pos)
  2311. return pos - buf;
  2312. pos += ret;
  2313. first = 0;
  2314. }
  2315. return pos - buf;
  2316. }
  2317. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  2318. struct wpa_driver_capa *capa,
  2319. char *buf, size_t buflen)
  2320. {
  2321. int ret;
  2322. char *pos, *end;
  2323. size_t len;
  2324. pos = buf;
  2325. end = pos + buflen;
  2326. if (res < 0) {
  2327. if (strict)
  2328. return 0;
  2329. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  2330. "NONE", buflen);
  2331. if (len >= buflen)
  2332. return -1;
  2333. return len;
  2334. }
  2335. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  2336. if (ret < 0 || ret >= end - pos)
  2337. return pos - buf;
  2338. pos += ret;
  2339. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2340. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2341. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  2342. if (ret < 0 || ret >= end - pos)
  2343. return pos - buf;
  2344. pos += ret;
  2345. }
  2346. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2347. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2348. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  2349. if (ret < 0 || ret >= end - pos)
  2350. return pos - buf;
  2351. pos += ret;
  2352. }
  2353. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2354. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  2355. if (ret < 0 || ret >= end - pos)
  2356. return pos - buf;
  2357. pos += ret;
  2358. }
  2359. return pos - buf;
  2360. }
  2361. static int ctrl_iface_get_capability_proto(int res, char *strict,
  2362. struct wpa_driver_capa *capa,
  2363. char *buf, size_t buflen)
  2364. {
  2365. int ret, first = 1;
  2366. char *pos, *end;
  2367. size_t len;
  2368. pos = buf;
  2369. end = pos + buflen;
  2370. if (res < 0) {
  2371. if (strict)
  2372. return 0;
  2373. len = os_strlcpy(buf, "RSN WPA", buflen);
  2374. if (len >= buflen)
  2375. return -1;
  2376. return len;
  2377. }
  2378. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2379. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2380. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  2381. if (ret < 0 || ret >= end - pos)
  2382. return pos - buf;
  2383. pos += ret;
  2384. first = 0;
  2385. }
  2386. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2387. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  2388. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  2389. if (ret < 0 || ret >= end - pos)
  2390. return pos - buf;
  2391. pos += ret;
  2392. first = 0;
  2393. }
  2394. return pos - buf;
  2395. }
  2396. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  2397. struct wpa_driver_capa *capa,
  2398. char *buf, size_t buflen)
  2399. {
  2400. int ret, first = 1;
  2401. char *pos, *end;
  2402. size_t len;
  2403. pos = buf;
  2404. end = pos + buflen;
  2405. if (res < 0) {
  2406. if (strict)
  2407. return 0;
  2408. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  2409. if (len >= buflen)
  2410. return -1;
  2411. return len;
  2412. }
  2413. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  2414. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  2415. if (ret < 0 || ret >= end - pos)
  2416. return pos - buf;
  2417. pos += ret;
  2418. first = 0;
  2419. }
  2420. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  2421. ret = os_snprintf(pos, end - pos, "%sSHARED",
  2422. first ? "" : " ");
  2423. if (ret < 0 || ret >= end - pos)
  2424. return pos - buf;
  2425. pos += ret;
  2426. first = 0;
  2427. }
  2428. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  2429. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  2430. if (ret < 0 || ret >= end - pos)
  2431. return pos - buf;
  2432. pos += ret;
  2433. first = 0;
  2434. }
  2435. return pos - buf;
  2436. }
  2437. static int ctrl_iface_get_capability_modes(int res, char *strict,
  2438. struct wpa_driver_capa *capa,
  2439. char *buf, size_t buflen)
  2440. {
  2441. int ret, first = 1;
  2442. char *pos, *end;
  2443. size_t len;
  2444. pos = buf;
  2445. end = pos + buflen;
  2446. if (res < 0) {
  2447. if (strict)
  2448. return 0;
  2449. len = os_strlcpy(buf, "IBSS AP", buflen);
  2450. if (len >= buflen)
  2451. return -1;
  2452. return len;
  2453. }
  2454. if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
  2455. ret = os_snprintf(pos, end - pos, "%sIBSS", first ? "" : " ");
  2456. if (ret < 0 || ret >= end - pos)
  2457. return pos - buf;
  2458. pos += ret;
  2459. first = 0;
  2460. }
  2461. if (capa->flags & WPA_DRIVER_FLAGS_AP) {
  2462. ret = os_snprintf(pos, end - pos, "%sAP", first ? "" : " ");
  2463. if (ret < 0 || ret >= end - pos)
  2464. return pos - buf;
  2465. pos += ret;
  2466. first = 0;
  2467. }
  2468. return pos - buf;
  2469. }
  2470. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  2471. char *buf, size_t buflen)
  2472. {
  2473. struct hostapd_channel_data *chnl;
  2474. int ret, i, j;
  2475. char *pos, *end, *hmode;
  2476. pos = buf;
  2477. end = pos + buflen;
  2478. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2479. switch (wpa_s->hw.modes[j].mode) {
  2480. case HOSTAPD_MODE_IEEE80211B:
  2481. hmode = "B";
  2482. break;
  2483. case HOSTAPD_MODE_IEEE80211G:
  2484. hmode = "G";
  2485. break;
  2486. case HOSTAPD_MODE_IEEE80211A:
  2487. hmode = "A";
  2488. break;
  2489. case HOSTAPD_MODE_IEEE80211AD:
  2490. hmode = "AD";
  2491. break;
  2492. default:
  2493. continue;
  2494. }
  2495. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  2496. if (ret < 0 || ret >= end - pos)
  2497. return pos - buf;
  2498. pos += ret;
  2499. chnl = wpa_s->hw.modes[j].channels;
  2500. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2501. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2502. continue;
  2503. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  2504. if (ret < 0 || ret >= end - pos)
  2505. return pos - buf;
  2506. pos += ret;
  2507. }
  2508. ret = os_snprintf(pos, end - pos, "\n");
  2509. if (ret < 0 || ret >= end - pos)
  2510. return pos - buf;
  2511. pos += ret;
  2512. }
  2513. return pos - buf;
  2514. }
  2515. static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
  2516. char *buf, size_t buflen)
  2517. {
  2518. struct hostapd_channel_data *chnl;
  2519. int ret, i, j;
  2520. char *pos, *end, *hmode;
  2521. pos = buf;
  2522. end = pos + buflen;
  2523. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2524. switch (wpa_s->hw.modes[j].mode) {
  2525. case HOSTAPD_MODE_IEEE80211B:
  2526. hmode = "B";
  2527. break;
  2528. case HOSTAPD_MODE_IEEE80211G:
  2529. hmode = "G";
  2530. break;
  2531. case HOSTAPD_MODE_IEEE80211A:
  2532. hmode = "A";
  2533. break;
  2534. case HOSTAPD_MODE_IEEE80211AD:
  2535. hmode = "AD";
  2536. break;
  2537. default:
  2538. continue;
  2539. }
  2540. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
  2541. hmode);
  2542. if (ret < 0 || ret >= end - pos)
  2543. return pos - buf;
  2544. pos += ret;
  2545. chnl = wpa_s->hw.modes[j].channels;
  2546. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2547. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2548. continue;
  2549. ret = os_snprintf(pos, end - pos, " %d = %d MHz%s\n",
  2550. chnl[i].chan, chnl[i].freq,
  2551. chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
  2552. " (NO_IBSS)" : "");
  2553. if (ret < 0 || ret >= end - pos)
  2554. return pos - buf;
  2555. pos += ret;
  2556. }
  2557. ret = os_snprintf(pos, end - pos, "\n");
  2558. if (ret < 0 || ret >= end - pos)
  2559. return pos - buf;
  2560. pos += ret;
  2561. }
  2562. return pos - buf;
  2563. }
  2564. static int wpa_supplicant_ctrl_iface_get_capability(
  2565. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  2566. size_t buflen)
  2567. {
  2568. struct wpa_driver_capa capa;
  2569. int res;
  2570. char *strict;
  2571. char field[30];
  2572. size_t len;
  2573. /* Determine whether or not strict checking was requested */
  2574. len = os_strlcpy(field, _field, sizeof(field));
  2575. if (len >= sizeof(field))
  2576. return -1;
  2577. strict = os_strchr(field, ' ');
  2578. if (strict != NULL) {
  2579. *strict++ = '\0';
  2580. if (os_strcmp(strict, "strict") != 0)
  2581. return -1;
  2582. }
  2583. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  2584. field, strict ? strict : "");
  2585. if (os_strcmp(field, "eap") == 0) {
  2586. return eap_get_names(buf, buflen);
  2587. }
  2588. res = wpa_drv_get_capa(wpa_s, &capa);
  2589. if (os_strcmp(field, "pairwise") == 0)
  2590. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  2591. buf, buflen);
  2592. if (os_strcmp(field, "group") == 0)
  2593. return ctrl_iface_get_capability_group(res, strict, &capa,
  2594. buf, buflen);
  2595. if (os_strcmp(field, "key_mgmt") == 0)
  2596. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  2597. buf, buflen);
  2598. if (os_strcmp(field, "proto") == 0)
  2599. return ctrl_iface_get_capability_proto(res, strict, &capa,
  2600. buf, buflen);
  2601. if (os_strcmp(field, "auth_alg") == 0)
  2602. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  2603. buf, buflen);
  2604. if (os_strcmp(field, "modes") == 0)
  2605. return ctrl_iface_get_capability_modes(res, strict, &capa,
  2606. buf, buflen);
  2607. if (os_strcmp(field, "channels") == 0)
  2608. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  2609. if (os_strcmp(field, "freq") == 0)
  2610. return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
  2611. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  2612. field);
  2613. return -1;
  2614. }
  2615. #ifdef CONFIG_INTERWORKING
  2616. static char * anqp_add_hex(char *pos, char *end, const char *title,
  2617. struct wpabuf *data)
  2618. {
  2619. char *start = pos;
  2620. size_t i;
  2621. int ret;
  2622. const u8 *d;
  2623. if (data == NULL)
  2624. return start;
  2625. ret = os_snprintf(pos, end - pos, "%s=", title);
  2626. if (ret < 0 || ret >= end - pos)
  2627. return start;
  2628. pos += ret;
  2629. d = wpabuf_head_u8(data);
  2630. for (i = 0; i < wpabuf_len(data); i++) {
  2631. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  2632. if (ret < 0 || ret >= end - pos)
  2633. return start;
  2634. pos += ret;
  2635. }
  2636. ret = os_snprintf(pos, end - pos, "\n");
  2637. if (ret < 0 || ret >= end - pos)
  2638. return start;
  2639. pos += ret;
  2640. return pos;
  2641. }
  2642. #endif /* CONFIG_INTERWORKING */
  2643. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  2644. unsigned long mask, char *buf, size_t buflen)
  2645. {
  2646. size_t i;
  2647. int ret;
  2648. char *pos, *end;
  2649. const u8 *ie, *ie2;
  2650. pos = buf;
  2651. end = buf + buflen;
  2652. if (mask & WPA_BSS_MASK_ID) {
  2653. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  2654. if (ret < 0 || ret >= end - pos)
  2655. return 0;
  2656. pos += ret;
  2657. }
  2658. if (mask & WPA_BSS_MASK_BSSID) {
  2659. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  2660. MAC2STR(bss->bssid));
  2661. if (ret < 0 || ret >= end - pos)
  2662. return 0;
  2663. pos += ret;
  2664. }
  2665. if (mask & WPA_BSS_MASK_FREQ) {
  2666. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  2667. if (ret < 0 || ret >= end - pos)
  2668. return 0;
  2669. pos += ret;
  2670. }
  2671. if (mask & WPA_BSS_MASK_BEACON_INT) {
  2672. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  2673. bss->beacon_int);
  2674. if (ret < 0 || ret >= end - pos)
  2675. return 0;
  2676. pos += ret;
  2677. }
  2678. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  2679. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  2680. bss->caps);
  2681. if (ret < 0 || ret >= end - pos)
  2682. return 0;
  2683. pos += ret;
  2684. }
  2685. if (mask & WPA_BSS_MASK_QUAL) {
  2686. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  2687. if (ret < 0 || ret >= end - pos)
  2688. return 0;
  2689. pos += ret;
  2690. }
  2691. if (mask & WPA_BSS_MASK_NOISE) {
  2692. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  2693. if (ret < 0 || ret >= end - pos)
  2694. return 0;
  2695. pos += ret;
  2696. }
  2697. if (mask & WPA_BSS_MASK_LEVEL) {
  2698. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  2699. if (ret < 0 || ret >= end - pos)
  2700. return 0;
  2701. pos += ret;
  2702. }
  2703. if (mask & WPA_BSS_MASK_TSF) {
  2704. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  2705. (unsigned long long) bss->tsf);
  2706. if (ret < 0 || ret >= end - pos)
  2707. return 0;
  2708. pos += ret;
  2709. }
  2710. if (mask & WPA_BSS_MASK_AGE) {
  2711. struct os_time now;
  2712. os_get_time(&now);
  2713. ret = os_snprintf(pos, end - pos, "age=%d\n",
  2714. (int) (now.sec - bss->last_update.sec));
  2715. if (ret < 0 || ret >= end - pos)
  2716. return 0;
  2717. pos += ret;
  2718. }
  2719. if (mask & WPA_BSS_MASK_IE) {
  2720. ret = os_snprintf(pos, end - pos, "ie=");
  2721. if (ret < 0 || ret >= end - pos)
  2722. return 0;
  2723. pos += ret;
  2724. ie = (const u8 *) (bss + 1);
  2725. for (i = 0; i < bss->ie_len; i++) {
  2726. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  2727. if (ret < 0 || ret >= end - pos)
  2728. return 0;
  2729. pos += ret;
  2730. }
  2731. ret = os_snprintf(pos, end - pos, "\n");
  2732. if (ret < 0 || ret >= end - pos)
  2733. return 0;
  2734. pos += ret;
  2735. }
  2736. if (mask & WPA_BSS_MASK_FLAGS) {
  2737. ret = os_snprintf(pos, end - pos, "flags=");
  2738. if (ret < 0 || ret >= end - pos)
  2739. return 0;
  2740. pos += ret;
  2741. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  2742. if (ie)
  2743. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  2744. 2 + ie[1]);
  2745. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  2746. if (ie2)
  2747. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
  2748. 2 + ie2[1]);
  2749. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  2750. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  2751. ret = os_snprintf(pos, end - pos, "[WEP]");
  2752. if (ret < 0 || ret >= end - pos)
  2753. return 0;
  2754. pos += ret;
  2755. }
  2756. if (bss->caps & IEEE80211_CAP_IBSS) {
  2757. ret = os_snprintf(pos, end - pos, "[IBSS]");
  2758. if (ret < 0 || ret >= end - pos)
  2759. return 0;
  2760. pos += ret;
  2761. }
  2762. if (bss->caps & IEEE80211_CAP_ESS) {
  2763. ret = os_snprintf(pos, end - pos, "[ESS]");
  2764. if (ret < 0 || ret >= end - pos)
  2765. return 0;
  2766. pos += ret;
  2767. }
  2768. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  2769. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  2770. ret = os_snprintf(pos, end - pos, "[P2P]");
  2771. if (ret < 0 || ret >= end - pos)
  2772. return 0;
  2773. pos += ret;
  2774. }
  2775. #ifdef CONFIG_HS20
  2776. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  2777. ret = os_snprintf(pos, end - pos, "[HS20]");
  2778. if (ret < 0 || ret >= end - pos)
  2779. return 0;
  2780. pos += ret;
  2781. }
  2782. #endif /* CONFIG_HS20 */
  2783. ret = os_snprintf(pos, end - pos, "\n");
  2784. if (ret < 0 || ret >= end - pos)
  2785. return 0;
  2786. pos += ret;
  2787. }
  2788. if (mask & WPA_BSS_MASK_SSID) {
  2789. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  2790. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  2791. if (ret < 0 || ret >= end - pos)
  2792. return 0;
  2793. pos += ret;
  2794. }
  2795. #ifdef CONFIG_WPS
  2796. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  2797. ie = (const u8 *) (bss + 1);
  2798. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  2799. if (ret < 0 || ret >= end - pos)
  2800. return 0;
  2801. pos += ret;
  2802. }
  2803. #endif /* CONFIG_WPS */
  2804. #ifdef CONFIG_P2P
  2805. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  2806. ie = (const u8 *) (bss + 1);
  2807. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  2808. if (ret < 0 || ret >= end - pos)
  2809. return 0;
  2810. pos += ret;
  2811. }
  2812. #endif /* CONFIG_P2P */
  2813. #ifdef CONFIG_WIFI_DISPLAY
  2814. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  2815. struct wpabuf *wfd;
  2816. ie = (const u8 *) (bss + 1);
  2817. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  2818. WFD_IE_VENDOR_TYPE);
  2819. if (wfd) {
  2820. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  2821. if (ret < 0 || ret >= end - pos)
  2822. return 0;
  2823. pos += ret;
  2824. pos += wpa_snprintf_hex(pos, end - pos,
  2825. wpabuf_head(wfd),
  2826. wpabuf_len(wfd));
  2827. wpabuf_free(wfd);
  2828. ret = os_snprintf(pos, end - pos, "\n");
  2829. if (ret < 0 || ret >= end - pos)
  2830. return 0;
  2831. pos += ret;
  2832. }
  2833. }
  2834. #endif /* CONFIG_WIFI_DISPLAY */
  2835. #ifdef CONFIG_INTERWORKING
  2836. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  2837. struct wpa_bss_anqp *anqp = bss->anqp;
  2838. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  2839. anqp->venue_name);
  2840. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  2841. anqp->network_auth_type);
  2842. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  2843. anqp->roaming_consortium);
  2844. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  2845. anqp->ip_addr_type_availability);
  2846. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  2847. anqp->nai_realm);
  2848. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  2849. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  2850. anqp->domain_name);
  2851. #ifdef CONFIG_HS20
  2852. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  2853. anqp->hs20_operator_friendly_name);
  2854. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  2855. anqp->hs20_wan_metrics);
  2856. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  2857. anqp->hs20_connection_capability);
  2858. #endif /* CONFIG_HS20 */
  2859. }
  2860. #endif /* CONFIG_INTERWORKING */
  2861. if (mask & WPA_BSS_MASK_DELIM) {
  2862. ret = os_snprintf(pos, end - pos, "====\n");
  2863. if (ret < 0 || ret >= end - pos)
  2864. return 0;
  2865. pos += ret;
  2866. }
  2867. return pos - buf;
  2868. }
  2869. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  2870. const char *cmd, char *buf,
  2871. size_t buflen)
  2872. {
  2873. u8 bssid[ETH_ALEN];
  2874. size_t i;
  2875. struct wpa_bss *bss;
  2876. struct wpa_bss *bsslast = NULL;
  2877. struct dl_list *next;
  2878. int ret = 0;
  2879. int len;
  2880. char *ctmp;
  2881. unsigned long mask = WPA_BSS_MASK_ALL;
  2882. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  2883. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  2884. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  2885. list_id);
  2886. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  2887. list_id);
  2888. } else { /* N1-N2 */
  2889. unsigned int id1, id2;
  2890. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  2891. wpa_printf(MSG_INFO, "Wrong BSS range "
  2892. "format");
  2893. return 0;
  2894. }
  2895. if (*(cmd + 6) == '-')
  2896. id1 = 0;
  2897. else
  2898. id1 = atoi(cmd + 6);
  2899. ctmp++;
  2900. if (*ctmp >= '0' && *ctmp <= '9')
  2901. id2 = atoi(ctmp);
  2902. else
  2903. id2 = (unsigned int) -1;
  2904. bss = wpa_bss_get_id_range(wpa_s, id1, id2);
  2905. if (id2 == (unsigned int) -1)
  2906. bsslast = dl_list_last(&wpa_s->bss_id,
  2907. struct wpa_bss,
  2908. list_id);
  2909. else {
  2910. bsslast = wpa_bss_get_id(wpa_s, id2);
  2911. if (bsslast == NULL && bss && id2 > id1) {
  2912. struct wpa_bss *tmp = bss;
  2913. for (;;) {
  2914. next = tmp->list_id.next;
  2915. if (next == &wpa_s->bss_id)
  2916. break;
  2917. tmp = dl_list_entry(
  2918. next, struct wpa_bss,
  2919. list_id);
  2920. if (tmp->id > id2)
  2921. break;
  2922. bsslast = tmp;
  2923. }
  2924. }
  2925. }
  2926. }
  2927. } else if (os_strncmp(cmd, "FIRST", 5) == 0)
  2928. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  2929. else if (os_strncmp(cmd, "LAST", 4) == 0)
  2930. bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
  2931. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  2932. i = atoi(cmd + 3);
  2933. bss = wpa_bss_get_id(wpa_s, i);
  2934. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  2935. i = atoi(cmd + 5);
  2936. bss = wpa_bss_get_id(wpa_s, i);
  2937. if (bss) {
  2938. next = bss->list_id.next;
  2939. if (next == &wpa_s->bss_id)
  2940. bss = NULL;
  2941. else
  2942. bss = dl_list_entry(next, struct wpa_bss,
  2943. list_id);
  2944. }
  2945. #ifdef CONFIG_P2P
  2946. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  2947. if (hwaddr_aton(cmd + 13, bssid) == 0)
  2948. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  2949. else
  2950. bss = NULL;
  2951. #endif /* CONFIG_P2P */
  2952. } else if (hwaddr_aton(cmd, bssid) == 0)
  2953. bss = wpa_bss_get_bssid(wpa_s, bssid);
  2954. else {
  2955. struct wpa_bss *tmp;
  2956. i = atoi(cmd);
  2957. bss = NULL;
  2958. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  2959. {
  2960. if (i-- == 0) {
  2961. bss = tmp;
  2962. break;
  2963. }
  2964. }
  2965. }
  2966. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  2967. mask = strtoul(ctmp + 5, NULL, 0x10);
  2968. if (mask == 0)
  2969. mask = WPA_BSS_MASK_ALL;
  2970. }
  2971. if (bss == NULL)
  2972. return 0;
  2973. if (bsslast == NULL)
  2974. bsslast = bss;
  2975. do {
  2976. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  2977. ret += len;
  2978. buf += len;
  2979. buflen -= len;
  2980. if (bss == bsslast) {
  2981. if ((mask & WPA_BSS_MASK_DELIM) && len &&
  2982. (bss == dl_list_last(&wpa_s->bss_id,
  2983. struct wpa_bss, list_id)))
  2984. os_snprintf(buf - 5, 5, "####\n");
  2985. break;
  2986. }
  2987. next = bss->list_id.next;
  2988. if (next == &wpa_s->bss_id)
  2989. break;
  2990. bss = dl_list_entry(next, struct wpa_bss, list_id);
  2991. } while (bss && len);
  2992. return ret;
  2993. }
  2994. static int wpa_supplicant_ctrl_iface_ap_scan(
  2995. struct wpa_supplicant *wpa_s, char *cmd)
  2996. {
  2997. int ap_scan = atoi(cmd);
  2998. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  2999. }
  3000. static int wpa_supplicant_ctrl_iface_scan_interval(
  3001. struct wpa_supplicant *wpa_s, char *cmd)
  3002. {
  3003. int scan_int = atoi(cmd);
  3004. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  3005. }
  3006. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  3007. struct wpa_supplicant *wpa_s, char *cmd)
  3008. {
  3009. int expire_age = atoi(cmd);
  3010. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  3011. }
  3012. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  3013. struct wpa_supplicant *wpa_s, char *cmd)
  3014. {
  3015. int expire_count = atoi(cmd);
  3016. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  3017. }
  3018. static int wpa_supplicant_ctrl_iface_bss_flush(
  3019. struct wpa_supplicant *wpa_s, char *cmd)
  3020. {
  3021. int flush_age = atoi(cmd);
  3022. if (flush_age == 0)
  3023. wpa_bss_flush(wpa_s);
  3024. else
  3025. wpa_bss_flush_by_age(wpa_s, flush_age);
  3026. return 0;
  3027. }
  3028. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  3029. {
  3030. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  3031. /* MLME-DELETEKEYS.request */
  3032. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  3033. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  3034. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  3035. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  3036. #ifdef CONFIG_IEEE80211W
  3037. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  3038. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  3039. #endif /* CONFIG_IEEE80211W */
  3040. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  3041. 0);
  3042. /* MLME-SETPROTECTION.request(None) */
  3043. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  3044. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  3045. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  3046. wpa_sm_drop_sa(wpa_s->wpa);
  3047. }
  3048. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  3049. char *addr)
  3050. {
  3051. #ifdef CONFIG_NO_SCAN_PROCESSING
  3052. return -1;
  3053. #else /* CONFIG_NO_SCAN_PROCESSING */
  3054. u8 bssid[ETH_ALEN];
  3055. struct wpa_bss *bss;
  3056. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3057. if (hwaddr_aton(addr, bssid)) {
  3058. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  3059. "address '%s'", addr);
  3060. return -1;
  3061. }
  3062. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  3063. if (!ssid) {
  3064. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  3065. "configuration known for the target AP");
  3066. return -1;
  3067. }
  3068. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  3069. if (!bss) {
  3070. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  3071. "from BSS table");
  3072. return -1;
  3073. }
  3074. /*
  3075. * TODO: Find best network configuration block from configuration to
  3076. * allow roaming to other networks
  3077. */
  3078. wpa_s->reassociate = 1;
  3079. wpa_supplicant_connect(wpa_s, bss, ssid);
  3080. return 0;
  3081. #endif /* CONFIG_NO_SCAN_PROCESSING */
  3082. }
  3083. #ifdef CONFIG_P2P
  3084. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  3085. {
  3086. unsigned int timeout = atoi(cmd);
  3087. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  3088. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  3089. char *pos;
  3090. unsigned int search_delay;
  3091. if (os_strstr(cmd, "type=social"))
  3092. type = P2P_FIND_ONLY_SOCIAL;
  3093. else if (os_strstr(cmd, "type=progressive"))
  3094. type = P2P_FIND_PROGRESSIVE;
  3095. pos = os_strstr(cmd, "dev_id=");
  3096. if (pos) {
  3097. pos += 7;
  3098. if (hwaddr_aton(pos, dev_id))
  3099. return -1;
  3100. _dev_id = dev_id;
  3101. }
  3102. pos = os_strstr(cmd, "delay=");
  3103. if (pos) {
  3104. pos += 6;
  3105. search_delay = atoi(pos);
  3106. } else
  3107. search_delay = wpas_p2p_search_delay(wpa_s);
  3108. return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id,
  3109. search_delay);
  3110. }
  3111. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  3112. char *buf, size_t buflen)
  3113. {
  3114. u8 addr[ETH_ALEN];
  3115. char *pos, *pos2;
  3116. char *pin = NULL;
  3117. enum p2p_wps_method wps_method;
  3118. int new_pin;
  3119. int ret;
  3120. int persistent_group, persistent_id = -1;
  3121. int join;
  3122. int auth;
  3123. int automatic;
  3124. int go_intent = -1;
  3125. int freq = 0;
  3126. int pd;
  3127. int ht40, vht;
  3128. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
  3129. * [persistent|persistent=<network id>]
  3130. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  3131. * [ht40] [vht] */
  3132. if (hwaddr_aton(cmd, addr))
  3133. return -1;
  3134. pos = cmd + 17;
  3135. if (*pos != ' ')
  3136. return -1;
  3137. pos++;
  3138. persistent_group = os_strstr(pos, " persistent") != NULL;
  3139. pos2 = os_strstr(pos, " persistent=");
  3140. if (pos2) {
  3141. struct wpa_ssid *ssid;
  3142. persistent_id = atoi(pos2 + 12);
  3143. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3144. if (ssid == NULL || ssid->disabled != 2 ||
  3145. ssid->mode != WPAS_MODE_P2P_GO) {
  3146. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  3147. "SSID id=%d for persistent P2P group (GO)",
  3148. persistent_id);
  3149. return -1;
  3150. }
  3151. }
  3152. join = os_strstr(pos, " join") != NULL;
  3153. auth = os_strstr(pos, " auth") != NULL;
  3154. automatic = os_strstr(pos, " auto") != NULL;
  3155. pd = os_strstr(pos, " provdisc") != NULL;
  3156. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3157. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3158. vht;
  3159. pos2 = os_strstr(pos, " go_intent=");
  3160. if (pos2) {
  3161. pos2 += 11;
  3162. go_intent = atoi(pos2);
  3163. if (go_intent < 0 || go_intent > 15)
  3164. return -1;
  3165. }
  3166. pos2 = os_strstr(pos, " freq=");
  3167. if (pos2) {
  3168. pos2 += 6;
  3169. freq = atoi(pos2);
  3170. if (freq <= 0)
  3171. return -1;
  3172. }
  3173. if (os_strncmp(pos, "pin", 3) == 0) {
  3174. /* Request random PIN (to be displayed) and enable the PIN */
  3175. wps_method = WPS_PIN_DISPLAY;
  3176. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  3177. wps_method = WPS_PBC;
  3178. } else {
  3179. pin = pos;
  3180. pos = os_strchr(pin, ' ');
  3181. wps_method = WPS_PIN_KEYPAD;
  3182. if (pos) {
  3183. *pos++ = '\0';
  3184. if (os_strncmp(pos, "display", 7) == 0)
  3185. wps_method = WPS_PIN_DISPLAY;
  3186. }
  3187. if (!wps_pin_str_valid(pin)) {
  3188. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  3189. return 17;
  3190. }
  3191. }
  3192. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  3193. persistent_group, automatic, join,
  3194. auth, go_intent, freq, persistent_id, pd,
  3195. ht40, vht);
  3196. if (new_pin == -2) {
  3197. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  3198. return 25;
  3199. }
  3200. if (new_pin == -3) {
  3201. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  3202. return 25;
  3203. }
  3204. if (new_pin < 0)
  3205. return -1;
  3206. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  3207. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  3208. if (ret < 0 || (size_t) ret >= buflen)
  3209. return -1;
  3210. return ret;
  3211. }
  3212. os_memcpy(buf, "OK\n", 3);
  3213. return 3;
  3214. }
  3215. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3216. {
  3217. unsigned int timeout = atoi(cmd);
  3218. return wpas_p2p_listen(wpa_s, timeout);
  3219. }
  3220. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  3221. {
  3222. u8 addr[ETH_ALEN];
  3223. char *pos;
  3224. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  3225. /* <addr> <config method> [join|auto] */
  3226. if (hwaddr_aton(cmd, addr))
  3227. return -1;
  3228. pos = cmd + 17;
  3229. if (*pos != ' ')
  3230. return -1;
  3231. pos++;
  3232. if (os_strstr(pos, " join") != NULL)
  3233. use = WPAS_P2P_PD_FOR_JOIN;
  3234. else if (os_strstr(pos, " auto") != NULL)
  3235. use = WPAS_P2P_PD_AUTO;
  3236. return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
  3237. }
  3238. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  3239. size_t buflen)
  3240. {
  3241. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3242. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  3243. ssid->passphrase == NULL)
  3244. return -1;
  3245. os_strlcpy(buf, ssid->passphrase, buflen);
  3246. return os_strlen(buf);
  3247. }
  3248. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  3249. char *buf, size_t buflen)
  3250. {
  3251. u64 ref;
  3252. int res;
  3253. u8 dst_buf[ETH_ALEN], *dst;
  3254. struct wpabuf *tlvs;
  3255. char *pos;
  3256. size_t len;
  3257. if (hwaddr_aton(cmd, dst_buf))
  3258. return -1;
  3259. dst = dst_buf;
  3260. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  3261. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  3262. dst = NULL;
  3263. pos = cmd + 17;
  3264. if (*pos != ' ')
  3265. return -1;
  3266. pos++;
  3267. if (os_strncmp(pos, "upnp ", 5) == 0) {
  3268. u8 version;
  3269. pos += 5;
  3270. if (hexstr2bin(pos, &version, 1) < 0)
  3271. return -1;
  3272. pos += 2;
  3273. if (*pos != ' ')
  3274. return -1;
  3275. pos++;
  3276. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  3277. #ifdef CONFIG_WIFI_DISPLAY
  3278. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  3279. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  3280. #endif /* CONFIG_WIFI_DISPLAY */
  3281. } else {
  3282. len = os_strlen(pos);
  3283. if (len & 1)
  3284. return -1;
  3285. len /= 2;
  3286. tlvs = wpabuf_alloc(len);
  3287. if (tlvs == NULL)
  3288. return -1;
  3289. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  3290. wpabuf_free(tlvs);
  3291. return -1;
  3292. }
  3293. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  3294. wpabuf_free(tlvs);
  3295. }
  3296. if (ref == 0)
  3297. return -1;
  3298. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  3299. if (res < 0 || (unsigned) res >= buflen)
  3300. return -1;
  3301. return res;
  3302. }
  3303. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  3304. char *cmd)
  3305. {
  3306. long long unsigned val;
  3307. u64 req;
  3308. if (sscanf(cmd, "%llx", &val) != 1)
  3309. return -1;
  3310. req = val;
  3311. return wpas_p2p_sd_cancel_request(wpa_s, req);
  3312. }
  3313. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  3314. {
  3315. int freq;
  3316. u8 dst[ETH_ALEN];
  3317. u8 dialog_token;
  3318. struct wpabuf *resp_tlvs;
  3319. char *pos, *pos2;
  3320. size_t len;
  3321. pos = os_strchr(cmd, ' ');
  3322. if (pos == NULL)
  3323. return -1;
  3324. *pos++ = '\0';
  3325. freq = atoi(cmd);
  3326. if (freq == 0)
  3327. return -1;
  3328. if (hwaddr_aton(pos, dst))
  3329. return -1;
  3330. pos += 17;
  3331. if (*pos != ' ')
  3332. return -1;
  3333. pos++;
  3334. pos2 = os_strchr(pos, ' ');
  3335. if (pos2 == NULL)
  3336. return -1;
  3337. *pos2++ = '\0';
  3338. dialog_token = atoi(pos);
  3339. len = os_strlen(pos2);
  3340. if (len & 1)
  3341. return -1;
  3342. len /= 2;
  3343. resp_tlvs = wpabuf_alloc(len);
  3344. if (resp_tlvs == NULL)
  3345. return -1;
  3346. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  3347. wpabuf_free(resp_tlvs);
  3348. return -1;
  3349. }
  3350. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  3351. wpabuf_free(resp_tlvs);
  3352. return 0;
  3353. }
  3354. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  3355. char *cmd)
  3356. {
  3357. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  3358. return -1;
  3359. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  3360. return 0;
  3361. }
  3362. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  3363. char *cmd)
  3364. {
  3365. char *pos;
  3366. size_t len;
  3367. struct wpabuf *query, *resp;
  3368. pos = os_strchr(cmd, ' ');
  3369. if (pos == NULL)
  3370. return -1;
  3371. *pos++ = '\0';
  3372. len = os_strlen(cmd);
  3373. if (len & 1)
  3374. return -1;
  3375. len /= 2;
  3376. query = wpabuf_alloc(len);
  3377. if (query == NULL)
  3378. return -1;
  3379. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3380. wpabuf_free(query);
  3381. return -1;
  3382. }
  3383. len = os_strlen(pos);
  3384. if (len & 1) {
  3385. wpabuf_free(query);
  3386. return -1;
  3387. }
  3388. len /= 2;
  3389. resp = wpabuf_alloc(len);
  3390. if (resp == NULL) {
  3391. wpabuf_free(query);
  3392. return -1;
  3393. }
  3394. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  3395. wpabuf_free(query);
  3396. wpabuf_free(resp);
  3397. return -1;
  3398. }
  3399. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  3400. wpabuf_free(query);
  3401. wpabuf_free(resp);
  3402. return -1;
  3403. }
  3404. return 0;
  3405. }
  3406. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3407. {
  3408. char *pos;
  3409. u8 version;
  3410. pos = os_strchr(cmd, ' ');
  3411. if (pos == NULL)
  3412. return -1;
  3413. *pos++ = '\0';
  3414. if (hexstr2bin(cmd, &version, 1) < 0)
  3415. return -1;
  3416. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  3417. }
  3418. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  3419. {
  3420. char *pos;
  3421. pos = os_strchr(cmd, ' ');
  3422. if (pos == NULL)
  3423. return -1;
  3424. *pos++ = '\0';
  3425. if (os_strcmp(cmd, "bonjour") == 0)
  3426. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  3427. if (os_strcmp(cmd, "upnp") == 0)
  3428. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  3429. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3430. return -1;
  3431. }
  3432. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  3433. char *cmd)
  3434. {
  3435. size_t len;
  3436. struct wpabuf *query;
  3437. int ret;
  3438. len = os_strlen(cmd);
  3439. if (len & 1)
  3440. return -1;
  3441. len /= 2;
  3442. query = wpabuf_alloc(len);
  3443. if (query == NULL)
  3444. return -1;
  3445. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3446. wpabuf_free(query);
  3447. return -1;
  3448. }
  3449. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  3450. wpabuf_free(query);
  3451. return ret;
  3452. }
  3453. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3454. {
  3455. char *pos;
  3456. u8 version;
  3457. pos = os_strchr(cmd, ' ');
  3458. if (pos == NULL)
  3459. return -1;
  3460. *pos++ = '\0';
  3461. if (hexstr2bin(cmd, &version, 1) < 0)
  3462. return -1;
  3463. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  3464. }
  3465. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  3466. {
  3467. char *pos;
  3468. pos = os_strchr(cmd, ' ');
  3469. if (pos == NULL)
  3470. return -1;
  3471. *pos++ = '\0';
  3472. if (os_strcmp(cmd, "bonjour") == 0)
  3473. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  3474. if (os_strcmp(cmd, "upnp") == 0)
  3475. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  3476. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3477. return -1;
  3478. }
  3479. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  3480. {
  3481. u8 addr[ETH_ALEN];
  3482. /* <addr> */
  3483. if (hwaddr_aton(cmd, addr))
  3484. return -1;
  3485. return wpas_p2p_reject(wpa_s, addr);
  3486. }
  3487. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  3488. {
  3489. char *pos;
  3490. int id;
  3491. struct wpa_ssid *ssid;
  3492. u8 *_peer = NULL, peer[ETH_ALEN];
  3493. int freq = 0, pref_freq = 0;
  3494. int ht40, vht;
  3495. id = atoi(cmd);
  3496. pos = os_strstr(cmd, " peer=");
  3497. if (pos) {
  3498. pos += 6;
  3499. if (hwaddr_aton(pos, peer))
  3500. return -1;
  3501. _peer = peer;
  3502. }
  3503. ssid = wpa_config_get_network(wpa_s->conf, id);
  3504. if (ssid == NULL || ssid->disabled != 2) {
  3505. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3506. "for persistent P2P group",
  3507. id);
  3508. return -1;
  3509. }
  3510. pos = os_strstr(cmd, " freq=");
  3511. if (pos) {
  3512. pos += 6;
  3513. freq = atoi(pos);
  3514. if (freq <= 0)
  3515. return -1;
  3516. }
  3517. pos = os_strstr(cmd, " pref=");
  3518. if (pos) {
  3519. pos += 6;
  3520. pref_freq = atoi(pos);
  3521. if (pref_freq <= 0)
  3522. return -1;
  3523. }
  3524. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3525. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3526. vht;
  3527. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
  3528. pref_freq);
  3529. }
  3530. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  3531. {
  3532. char *pos;
  3533. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  3534. pos = os_strstr(cmd, " peer=");
  3535. if (!pos)
  3536. return -1;
  3537. *pos = '\0';
  3538. pos += 6;
  3539. if (hwaddr_aton(pos, peer)) {
  3540. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  3541. return -1;
  3542. }
  3543. pos = os_strstr(pos, " go_dev_addr=");
  3544. if (pos) {
  3545. pos += 13;
  3546. if (hwaddr_aton(pos, go_dev_addr)) {
  3547. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  3548. pos);
  3549. return -1;
  3550. }
  3551. go_dev = go_dev_addr;
  3552. }
  3553. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  3554. }
  3555. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  3556. {
  3557. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3558. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  3559. if (os_strncmp(cmd, "group=", 6) == 0)
  3560. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  3561. return -1;
  3562. }
  3563. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  3564. char *cmd, int freq, int ht40,
  3565. int vht)
  3566. {
  3567. int id;
  3568. struct wpa_ssid *ssid;
  3569. id = atoi(cmd);
  3570. ssid = wpa_config_get_network(wpa_s->conf, id);
  3571. if (ssid == NULL || ssid->disabled != 2) {
  3572. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3573. "for persistent P2P group",
  3574. id);
  3575. return -1;
  3576. }
  3577. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40, vht,
  3578. NULL, 0);
  3579. }
  3580. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  3581. {
  3582. int freq = 0, ht40, vht;
  3583. char *pos;
  3584. pos = os_strstr(cmd, "freq=");
  3585. if (pos)
  3586. freq = atoi(pos + 5);
  3587. vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3588. ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3589. vht;
  3590. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3591. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
  3592. ht40, vht);
  3593. if (os_strcmp(cmd, "persistent") == 0 ||
  3594. os_strncmp(cmd, "persistent ", 11) == 0)
  3595. return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
  3596. if (os_strncmp(cmd, "freq=", 5) == 0)
  3597. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  3598. if (ht40)
  3599. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  3600. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  3601. cmd);
  3602. return -1;
  3603. }
  3604. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  3605. char *buf, size_t buflen)
  3606. {
  3607. u8 addr[ETH_ALEN], *addr_ptr;
  3608. int next, res;
  3609. const struct p2p_peer_info *info;
  3610. char *pos, *end;
  3611. char devtype[WPS_DEV_TYPE_BUFSIZE];
  3612. struct wpa_ssid *ssid;
  3613. size_t i;
  3614. if (!wpa_s->global->p2p)
  3615. return -1;
  3616. if (os_strcmp(cmd, "FIRST") == 0) {
  3617. addr_ptr = NULL;
  3618. next = 0;
  3619. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3620. if (hwaddr_aton(cmd + 5, addr) < 0)
  3621. return -1;
  3622. addr_ptr = addr;
  3623. next = 1;
  3624. } else {
  3625. if (hwaddr_aton(cmd, addr) < 0)
  3626. return -1;
  3627. addr_ptr = addr;
  3628. next = 0;
  3629. }
  3630. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  3631. if (info == NULL)
  3632. return -1;
  3633. pos = buf;
  3634. end = buf + buflen;
  3635. res = os_snprintf(pos, end - pos, MACSTR "\n"
  3636. "pri_dev_type=%s\n"
  3637. "device_name=%s\n"
  3638. "manufacturer=%s\n"
  3639. "model_name=%s\n"
  3640. "model_number=%s\n"
  3641. "serial_number=%s\n"
  3642. "config_methods=0x%x\n"
  3643. "dev_capab=0x%x\n"
  3644. "group_capab=0x%x\n"
  3645. "level=%d\n",
  3646. MAC2STR(info->p2p_device_addr),
  3647. wps_dev_type_bin2str(info->pri_dev_type,
  3648. devtype, sizeof(devtype)),
  3649. info->device_name,
  3650. info->manufacturer,
  3651. info->model_name,
  3652. info->model_number,
  3653. info->serial_number,
  3654. info->config_methods,
  3655. info->dev_capab,
  3656. info->group_capab,
  3657. info->level);
  3658. if (res < 0 || res >= end - pos)
  3659. return pos - buf;
  3660. pos += res;
  3661. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  3662. {
  3663. const u8 *t;
  3664. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  3665. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  3666. wps_dev_type_bin2str(t, devtype,
  3667. sizeof(devtype)));
  3668. if (res < 0 || res >= end - pos)
  3669. return pos - buf;
  3670. pos += res;
  3671. }
  3672. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  3673. if (ssid) {
  3674. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  3675. if (res < 0 || res >= end - pos)
  3676. return pos - buf;
  3677. pos += res;
  3678. }
  3679. res = p2p_get_peer_info_txt(info, pos, end - pos);
  3680. if (res < 0)
  3681. return pos - buf;
  3682. pos += res;
  3683. return pos - buf;
  3684. }
  3685. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  3686. const char *param)
  3687. {
  3688. unsigned int i;
  3689. if (wpa_s->global->p2p == NULL)
  3690. return -1;
  3691. if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
  3692. return -1;
  3693. for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
  3694. struct wpa_freq_range *freq;
  3695. freq = &wpa_s->global->p2p_disallow_freq.range[i];
  3696. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  3697. freq->min, freq->max);
  3698. }
  3699. wpas_p2p_update_channel_list(wpa_s);
  3700. return 0;
  3701. }
  3702. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  3703. {
  3704. char *param;
  3705. if (wpa_s->global->p2p == NULL)
  3706. return -1;
  3707. param = os_strchr(cmd, ' ');
  3708. if (param == NULL)
  3709. return -1;
  3710. *param++ = '\0';
  3711. if (os_strcmp(cmd, "discoverability") == 0) {
  3712. p2p_set_client_discoverability(wpa_s->global->p2p,
  3713. atoi(param));
  3714. return 0;
  3715. }
  3716. if (os_strcmp(cmd, "managed") == 0) {
  3717. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  3718. return 0;
  3719. }
  3720. if (os_strcmp(cmd, "listen_channel") == 0) {
  3721. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  3722. atoi(param));
  3723. }
  3724. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  3725. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  3726. os_strlen(param));
  3727. }
  3728. if (os_strcmp(cmd, "noa") == 0) {
  3729. char *pos;
  3730. int count, start, duration;
  3731. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  3732. count = atoi(param);
  3733. pos = os_strchr(param, ',');
  3734. if (pos == NULL)
  3735. return -1;
  3736. pos++;
  3737. start = atoi(pos);
  3738. pos = os_strchr(pos, ',');
  3739. if (pos == NULL)
  3740. return -1;
  3741. pos++;
  3742. duration = atoi(pos);
  3743. if (count < 0 || count > 255 || start < 0 || duration < 0)
  3744. return -1;
  3745. if (count == 0 && duration > 0)
  3746. return -1;
  3747. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  3748. "start=%d duration=%d", count, start, duration);
  3749. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  3750. }
  3751. if (os_strcmp(cmd, "ps") == 0)
  3752. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  3753. if (os_strcmp(cmd, "oppps") == 0)
  3754. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  3755. if (os_strcmp(cmd, "ctwindow") == 0)
  3756. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  3757. if (os_strcmp(cmd, "disabled") == 0) {
  3758. wpa_s->global->p2p_disabled = atoi(param);
  3759. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  3760. wpa_s->global->p2p_disabled ?
  3761. "disabled" : "enabled");
  3762. if (wpa_s->global->p2p_disabled) {
  3763. wpas_p2p_stop_find(wpa_s);
  3764. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  3765. p2p_flush(wpa_s->global->p2p);
  3766. }
  3767. return 0;
  3768. }
  3769. if (os_strcmp(cmd, "conc_pref") == 0) {
  3770. if (os_strcmp(param, "sta") == 0)
  3771. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  3772. else if (os_strcmp(param, "p2p") == 0)
  3773. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  3774. else {
  3775. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  3776. return -1;
  3777. }
  3778. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  3779. "%s", param);
  3780. return 0;
  3781. }
  3782. if (os_strcmp(cmd, "force_long_sd") == 0) {
  3783. wpa_s->force_long_sd = atoi(param);
  3784. return 0;
  3785. }
  3786. if (os_strcmp(cmd, "peer_filter") == 0) {
  3787. u8 addr[ETH_ALEN];
  3788. if (hwaddr_aton(param, addr))
  3789. return -1;
  3790. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  3791. return 0;
  3792. }
  3793. if (os_strcmp(cmd, "cross_connect") == 0)
  3794. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  3795. if (os_strcmp(cmd, "go_apsd") == 0) {
  3796. if (os_strcmp(param, "disable") == 0)
  3797. wpa_s->set_ap_uapsd = 0;
  3798. else {
  3799. wpa_s->set_ap_uapsd = 1;
  3800. wpa_s->ap_uapsd = atoi(param);
  3801. }
  3802. return 0;
  3803. }
  3804. if (os_strcmp(cmd, "client_apsd") == 0) {
  3805. if (os_strcmp(param, "disable") == 0)
  3806. wpa_s->set_sta_uapsd = 0;
  3807. else {
  3808. int be, bk, vi, vo;
  3809. char *pos;
  3810. /* format: BE,BK,VI,VO;max SP Length */
  3811. be = atoi(param);
  3812. pos = os_strchr(param, ',');
  3813. if (pos == NULL)
  3814. return -1;
  3815. pos++;
  3816. bk = atoi(pos);
  3817. pos = os_strchr(pos, ',');
  3818. if (pos == NULL)
  3819. return -1;
  3820. pos++;
  3821. vi = atoi(pos);
  3822. pos = os_strchr(pos, ',');
  3823. if (pos == NULL)
  3824. return -1;
  3825. pos++;
  3826. vo = atoi(pos);
  3827. /* ignore max SP Length for now */
  3828. wpa_s->set_sta_uapsd = 1;
  3829. wpa_s->sta_uapsd = 0;
  3830. if (be)
  3831. wpa_s->sta_uapsd |= BIT(0);
  3832. if (bk)
  3833. wpa_s->sta_uapsd |= BIT(1);
  3834. if (vi)
  3835. wpa_s->sta_uapsd |= BIT(2);
  3836. if (vo)
  3837. wpa_s->sta_uapsd |= BIT(3);
  3838. }
  3839. return 0;
  3840. }
  3841. if (os_strcmp(cmd, "disallow_freq") == 0)
  3842. return p2p_ctrl_disallow_freq(wpa_s, param);
  3843. if (os_strcmp(cmd, "disc_int") == 0) {
  3844. int min_disc_int, max_disc_int, max_disc_tu;
  3845. char *pos;
  3846. pos = param;
  3847. min_disc_int = atoi(pos);
  3848. pos = os_strchr(pos, ' ');
  3849. if (pos == NULL)
  3850. return -1;
  3851. *pos++ = '\0';
  3852. max_disc_int = atoi(pos);
  3853. pos = os_strchr(pos, ' ');
  3854. if (pos == NULL)
  3855. return -1;
  3856. *pos++ = '\0';
  3857. max_disc_tu = atoi(pos);
  3858. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  3859. max_disc_int, max_disc_tu);
  3860. }
  3861. if (os_strcmp(cmd, "per_sta_psk") == 0) {
  3862. wpa_s->global->p2p_per_sta_psk = !!atoi(param);
  3863. return 0;
  3864. }
  3865. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  3866. cmd);
  3867. return -1;
  3868. }
  3869. static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
  3870. {
  3871. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  3872. wpa_s->force_long_sd = 0;
  3873. if (wpa_s->global->p2p)
  3874. p2p_flush(wpa_s->global->p2p);
  3875. }
  3876. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  3877. {
  3878. char *pos, *pos2;
  3879. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  3880. if (cmd[0]) {
  3881. pos = os_strchr(cmd, ' ');
  3882. if (pos == NULL)
  3883. return -1;
  3884. *pos++ = '\0';
  3885. dur1 = atoi(cmd);
  3886. pos2 = os_strchr(pos, ' ');
  3887. if (pos2)
  3888. *pos2++ = '\0';
  3889. int1 = atoi(pos);
  3890. } else
  3891. pos2 = NULL;
  3892. if (pos2) {
  3893. pos = os_strchr(pos2, ' ');
  3894. if (pos == NULL)
  3895. return -1;
  3896. *pos++ = '\0';
  3897. dur2 = atoi(pos2);
  3898. int2 = atoi(pos);
  3899. }
  3900. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  3901. }
  3902. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3903. {
  3904. char *pos;
  3905. unsigned int period = 0, interval = 0;
  3906. if (cmd[0]) {
  3907. pos = os_strchr(cmd, ' ');
  3908. if (pos == NULL)
  3909. return -1;
  3910. *pos++ = '\0';
  3911. period = atoi(cmd);
  3912. interval = atoi(pos);
  3913. }
  3914. return wpas_p2p_ext_listen(wpa_s, period, interval);
  3915. }
  3916. static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
  3917. {
  3918. const char *pos;
  3919. u8 peer[ETH_ALEN];
  3920. int iface_addr = 0;
  3921. pos = cmd;
  3922. if (os_strncmp(pos, "iface=", 6) == 0) {
  3923. iface_addr = 1;
  3924. pos += 6;
  3925. }
  3926. if (hwaddr_aton(pos, peer))
  3927. return -1;
  3928. wpas_p2p_remove_client(wpa_s, peer, iface_addr);
  3929. return 0;
  3930. }
  3931. #endif /* CONFIG_P2P */
  3932. #ifdef CONFIG_INTERWORKING
  3933. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
  3934. {
  3935. u8 bssid[ETH_ALEN];
  3936. struct wpa_bss *bss;
  3937. if (hwaddr_aton(dst, bssid)) {
  3938. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  3939. return -1;
  3940. }
  3941. bss = wpa_bss_get_bssid(wpa_s, bssid);
  3942. if (bss == NULL) {
  3943. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  3944. MAC2STR(bssid));
  3945. return -1;
  3946. }
  3947. return interworking_connect(wpa_s, bss);
  3948. }
  3949. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  3950. {
  3951. u8 dst_addr[ETH_ALEN];
  3952. int used;
  3953. char *pos;
  3954. #define MAX_ANQP_INFO_ID 100
  3955. u16 id[MAX_ANQP_INFO_ID];
  3956. size_t num_id = 0;
  3957. used = hwaddr_aton2(dst, dst_addr);
  3958. if (used < 0)
  3959. return -1;
  3960. pos = dst + used;
  3961. while (num_id < MAX_ANQP_INFO_ID) {
  3962. id[num_id] = atoi(pos);
  3963. if (id[num_id])
  3964. num_id++;
  3965. pos = os_strchr(pos + 1, ',');
  3966. if (pos == NULL)
  3967. break;
  3968. pos++;
  3969. }
  3970. if (num_id == 0)
  3971. return -1;
  3972. return anqp_send_req(wpa_s, dst_addr, id, num_id);
  3973. }
  3974. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  3975. {
  3976. u8 dst_addr[ETH_ALEN];
  3977. struct wpabuf *advproto, *query = NULL;
  3978. int used, ret = -1;
  3979. char *pos, *end;
  3980. size_t len;
  3981. used = hwaddr_aton2(cmd, dst_addr);
  3982. if (used < 0)
  3983. return -1;
  3984. pos = cmd + used;
  3985. while (*pos == ' ')
  3986. pos++;
  3987. /* Advertisement Protocol ID */
  3988. end = os_strchr(pos, ' ');
  3989. if (end)
  3990. len = end - pos;
  3991. else
  3992. len = os_strlen(pos);
  3993. if (len & 0x01)
  3994. return -1;
  3995. len /= 2;
  3996. if (len == 0)
  3997. return -1;
  3998. advproto = wpabuf_alloc(len);
  3999. if (advproto == NULL)
  4000. return -1;
  4001. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  4002. goto fail;
  4003. if (end) {
  4004. /* Optional Query Request */
  4005. pos = end + 1;
  4006. while (*pos == ' ')
  4007. pos++;
  4008. len = os_strlen(pos);
  4009. if (len) {
  4010. if (len & 0x01)
  4011. goto fail;
  4012. len /= 2;
  4013. if (len == 0)
  4014. goto fail;
  4015. query = wpabuf_alloc(len);
  4016. if (query == NULL)
  4017. goto fail;
  4018. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  4019. goto fail;
  4020. }
  4021. }
  4022. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  4023. fail:
  4024. wpabuf_free(advproto);
  4025. wpabuf_free(query);
  4026. return ret;
  4027. }
  4028. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  4029. size_t buflen)
  4030. {
  4031. u8 addr[ETH_ALEN];
  4032. int dialog_token;
  4033. int used;
  4034. char *pos;
  4035. size_t resp_len, start, requested_len;
  4036. if (!wpa_s->last_gas_resp)
  4037. return -1;
  4038. used = hwaddr_aton2(cmd, addr);
  4039. if (used < 0)
  4040. return -1;
  4041. pos = cmd + used;
  4042. while (*pos == ' ')
  4043. pos++;
  4044. dialog_token = atoi(pos);
  4045. if (os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) != 0 ||
  4046. dialog_token != wpa_s->last_gas_dialog_token)
  4047. return -1;
  4048. resp_len = wpabuf_len(wpa_s->last_gas_resp);
  4049. start = 0;
  4050. requested_len = resp_len;
  4051. pos = os_strchr(pos, ' ');
  4052. if (pos) {
  4053. start = atoi(pos);
  4054. if (start > resp_len)
  4055. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4056. pos = os_strchr(pos, ',');
  4057. if (pos == NULL)
  4058. return -1;
  4059. pos++;
  4060. requested_len = atoi(pos);
  4061. if (start + requested_len > resp_len)
  4062. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4063. }
  4064. if (requested_len * 2 + 1 > buflen)
  4065. return os_snprintf(buf, buflen, "FAIL-Too long response");
  4066. return wpa_snprintf_hex(buf, buflen,
  4067. wpabuf_head_u8(wpa_s->last_gas_resp) + start,
  4068. requested_len);
  4069. }
  4070. #endif /* CONFIG_INTERWORKING */
  4071. #ifdef CONFIG_HS20
  4072. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4073. {
  4074. u8 dst_addr[ETH_ALEN];
  4075. int used;
  4076. char *pos;
  4077. u32 subtypes = 0;
  4078. used = hwaddr_aton2(dst, dst_addr);
  4079. if (used < 0)
  4080. return -1;
  4081. pos = dst + used;
  4082. for (;;) {
  4083. int num = atoi(pos);
  4084. if (num <= 0 || num > 31)
  4085. return -1;
  4086. subtypes |= BIT(num);
  4087. pos = os_strchr(pos + 1, ',');
  4088. if (pos == NULL)
  4089. break;
  4090. pos++;
  4091. }
  4092. if (subtypes == 0)
  4093. return -1;
  4094. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
  4095. }
  4096. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4097. const u8 *addr, const char *realm)
  4098. {
  4099. u8 *buf;
  4100. size_t rlen, len;
  4101. int ret;
  4102. rlen = os_strlen(realm);
  4103. len = 3 + rlen;
  4104. buf = os_malloc(len);
  4105. if (buf == NULL)
  4106. return -1;
  4107. buf[0] = 1; /* NAI Home Realm Count */
  4108. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  4109. buf[2] = rlen;
  4110. os_memcpy(buf + 3, realm, rlen);
  4111. ret = hs20_anqp_send_req(wpa_s, addr,
  4112. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4113. buf, len);
  4114. os_free(buf);
  4115. return ret;
  4116. }
  4117. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4118. char *dst)
  4119. {
  4120. struct wpa_cred *cred = wpa_s->conf->cred;
  4121. u8 dst_addr[ETH_ALEN];
  4122. int used;
  4123. u8 *buf;
  4124. size_t len;
  4125. int ret;
  4126. used = hwaddr_aton2(dst, dst_addr);
  4127. if (used < 0)
  4128. return -1;
  4129. while (dst[used] == ' ')
  4130. used++;
  4131. if (os_strncmp(dst + used, "realm=", 6) == 0)
  4132. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  4133. dst + used + 6);
  4134. len = os_strlen(dst + used);
  4135. if (len == 0 && cred && cred->realm)
  4136. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  4137. if (len % 1)
  4138. return -1;
  4139. len /= 2;
  4140. buf = os_malloc(len);
  4141. if (buf == NULL)
  4142. return -1;
  4143. if (hexstr2bin(dst + used, buf, len) < 0) {
  4144. os_free(buf);
  4145. return -1;
  4146. }
  4147. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  4148. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4149. buf, len);
  4150. os_free(buf);
  4151. return ret;
  4152. }
  4153. #endif /* CONFIG_HS20 */
  4154. static int wpa_supplicant_ctrl_iface_sta_autoconnect(
  4155. struct wpa_supplicant *wpa_s, char *cmd)
  4156. {
  4157. wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
  4158. return 0;
  4159. }
  4160. #ifdef CONFIG_AUTOSCAN
  4161. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  4162. char *cmd)
  4163. {
  4164. enum wpa_states state = wpa_s->wpa_state;
  4165. char *new_params = NULL;
  4166. if (os_strlen(cmd) > 0) {
  4167. new_params = os_strdup(cmd);
  4168. if (new_params == NULL)
  4169. return -1;
  4170. }
  4171. os_free(wpa_s->conf->autoscan);
  4172. wpa_s->conf->autoscan = new_params;
  4173. if (wpa_s->conf->autoscan == NULL)
  4174. autoscan_deinit(wpa_s);
  4175. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  4176. autoscan_init(wpa_s, 1);
  4177. else if (state == WPA_SCANNING)
  4178. wpa_supplicant_reinit_autoscan(wpa_s);
  4179. return 0;
  4180. }
  4181. #endif /* CONFIG_AUTOSCAN */
  4182. #ifdef CONFIG_WNM
  4183. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  4184. {
  4185. int enter;
  4186. int intval = 0;
  4187. char *pos;
  4188. int ret;
  4189. struct wpabuf *tfs_req = NULL;
  4190. if (os_strncmp(cmd, "enter", 5) == 0)
  4191. enter = 1;
  4192. else if (os_strncmp(cmd, "exit", 4) == 0)
  4193. enter = 0;
  4194. else
  4195. return -1;
  4196. pos = os_strstr(cmd, " interval=");
  4197. if (pos)
  4198. intval = atoi(pos + 10);
  4199. pos = os_strstr(cmd, " tfs_req=");
  4200. if (pos) {
  4201. char *end;
  4202. size_t len;
  4203. pos += 9;
  4204. end = os_strchr(pos, ' ');
  4205. if (end)
  4206. len = end - pos;
  4207. else
  4208. len = os_strlen(pos);
  4209. if (len & 1)
  4210. return -1;
  4211. len /= 2;
  4212. tfs_req = wpabuf_alloc(len);
  4213. if (tfs_req == NULL)
  4214. return -1;
  4215. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  4216. wpabuf_free(tfs_req);
  4217. return -1;
  4218. }
  4219. }
  4220. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  4221. WNM_SLEEP_MODE_EXIT, intval,
  4222. tfs_req);
  4223. wpabuf_free(tfs_req);
  4224. return ret;
  4225. }
  4226. static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
  4227. {
  4228. int query_reason;
  4229. query_reason = atoi(cmd);
  4230. wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
  4231. query_reason);
  4232. return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
  4233. }
  4234. #endif /* CONFIG_WNM */
  4235. /* Get string representation of channel width */
  4236. static const char * channel_width_name(enum chan_width width)
  4237. {
  4238. switch (width) {
  4239. case CHAN_WIDTH_20_NOHT:
  4240. return "20 MHz (no HT)";
  4241. case CHAN_WIDTH_20:
  4242. return "20 MHz";
  4243. case CHAN_WIDTH_40:
  4244. return "40 MHz";
  4245. case CHAN_WIDTH_80:
  4246. return "80 MHz";
  4247. case CHAN_WIDTH_80P80:
  4248. return "80+80 MHz";
  4249. case CHAN_WIDTH_160:
  4250. return "160 MHz";
  4251. default:
  4252. return "unknown";
  4253. }
  4254. }
  4255. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  4256. size_t buflen)
  4257. {
  4258. struct wpa_signal_info si;
  4259. int ret;
  4260. char *pos, *end;
  4261. ret = wpa_drv_signal_poll(wpa_s, &si);
  4262. if (ret)
  4263. return -1;
  4264. pos = buf;
  4265. end = buf + buflen;
  4266. ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
  4267. "NOISE=%d\nFREQUENCY=%u\n",
  4268. si.current_signal, si.current_txrate / 1000,
  4269. si.current_noise, si.frequency);
  4270. if (ret < 0 || ret > end - pos)
  4271. return -1;
  4272. pos += ret;
  4273. if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
  4274. ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
  4275. channel_width_name(si.chanwidth));
  4276. if (ret < 0 || ret > end - pos)
  4277. return -1;
  4278. pos += ret;
  4279. }
  4280. if (si.center_frq1 > 0 && si.center_frq2 > 0) {
  4281. ret = os_snprintf(pos, end - pos,
  4282. "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
  4283. si.center_frq1, si.center_frq2);
  4284. if (ret < 0 || ret > end - pos)
  4285. return -1;
  4286. pos += ret;
  4287. }
  4288. if (si.avg_signal) {
  4289. ret = os_snprintf(pos, end - pos,
  4290. "AVG_RSSI=%d\n", si.avg_signal);
  4291. if (ret < 0 || ret >= end - pos)
  4292. return -1;
  4293. pos += ret;
  4294. }
  4295. return pos - buf;
  4296. }
  4297. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  4298. size_t buflen)
  4299. {
  4300. struct hostap_sta_driver_data sta;
  4301. int ret;
  4302. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  4303. if (ret)
  4304. return -1;
  4305. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  4306. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  4307. if (ret < 0 || (size_t) ret > buflen)
  4308. return -1;
  4309. return ret;
  4310. }
  4311. static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
  4312. {
  4313. wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
  4314. #ifdef CONFIG_P2P
  4315. wpas_p2p_stop_find(wpa_s);
  4316. p2p_ctrl_flush(wpa_s);
  4317. wpas_p2p_group_remove(wpa_s, "*");
  4318. #endif /* CONFIG_P2P */
  4319. #ifdef CONFIG_WPS_TESTING
  4320. wps_version_number = 0x20;
  4321. wps_testing_dummy_cred = 0;
  4322. #endif /* CONFIG_WPS_TESTING */
  4323. #ifdef CONFIG_WPS
  4324. wpas_wps_cancel(wpa_s);
  4325. #endif /* CONFIG_WPS */
  4326. wpa_s->after_wps = 0;
  4327. wpa_s->known_wps_freq = 0;
  4328. #ifdef CONFIG_TDLS_TESTING
  4329. extern unsigned int tdls_testing;
  4330. tdls_testing = 0;
  4331. #endif /* CONFIG_TDLS_TESTING */
  4332. #ifdef CONFIG_TDLS
  4333. wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
  4334. wpa_tdls_enable(wpa_s->wpa, 1);
  4335. #endif /* CONFIG_TDLS */
  4336. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  4337. wpa_supplicant_stop_countermeasures(wpa_s, NULL);
  4338. wpa_s->no_keep_alive = 0;
  4339. os_free(wpa_s->disallow_aps_bssid);
  4340. wpa_s->disallow_aps_bssid = NULL;
  4341. wpa_s->disallow_aps_bssid_count = 0;
  4342. os_free(wpa_s->disallow_aps_ssid);
  4343. wpa_s->disallow_aps_ssid = NULL;
  4344. wpa_s->disallow_aps_ssid_count = 0;
  4345. wpa_s->set_sta_uapsd = 0;
  4346. wpa_s->sta_uapsd = 0;
  4347. wpa_drv_radio_disable(wpa_s, 0);
  4348. wpa_bss_flush(wpa_s);
  4349. wpa_blacklist_clear(wpa_s);
  4350. wpa_s->extra_blacklist_count = 0;
  4351. wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
  4352. wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
  4353. }
  4354. static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
  4355. {
  4356. struct wpa_supplicant *wpa_s = eloop_ctx;
  4357. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  4358. }
  4359. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  4360. char *buf, size_t *resp_len)
  4361. {
  4362. char *reply;
  4363. const int reply_size = 4096;
  4364. int reply_len;
  4365. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  4366. os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
  4367. os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  4368. os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0 ||
  4369. os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
  4370. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  4371. (const u8 *) buf, os_strlen(buf));
  4372. } else {
  4373. int level = MSG_DEBUG;
  4374. if (os_strcmp(buf, "PING") == 0)
  4375. level = MSG_EXCESSIVE;
  4376. wpa_hexdump_ascii(level, "RX ctrl_iface",
  4377. (const u8 *) buf, os_strlen(buf));
  4378. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  4379. }
  4380. reply = os_malloc(reply_size);
  4381. if (reply == NULL) {
  4382. *resp_len = 1;
  4383. return NULL;
  4384. }
  4385. os_memcpy(reply, "OK\n", 3);
  4386. reply_len = 3;
  4387. if (os_strcmp(buf, "PING") == 0) {
  4388. os_memcpy(reply, "PONG\n", 5);
  4389. reply_len = 5;
  4390. } else if (os_strcmp(buf, "IFNAME") == 0) {
  4391. reply_len = os_strlen(wpa_s->ifname);
  4392. os_memcpy(reply, wpa_s->ifname, reply_len);
  4393. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  4394. if (wpa_debug_reopen_file() < 0)
  4395. reply_len = -1;
  4396. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  4397. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  4398. } else if (os_strcmp(buf, "MIB") == 0) {
  4399. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  4400. if (reply_len >= 0) {
  4401. int res;
  4402. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  4403. reply_size - reply_len);
  4404. if (res < 0)
  4405. reply_len = -1;
  4406. else
  4407. reply_len += res;
  4408. }
  4409. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  4410. reply_len = wpa_supplicant_ctrl_iface_status(
  4411. wpa_s, buf + 6, reply, reply_size);
  4412. } else if (os_strcmp(buf, "PMKSA") == 0) {
  4413. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  4414. reply_size);
  4415. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  4416. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  4417. reply_len = -1;
  4418. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  4419. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  4420. reply, reply_size);
  4421. } else if (os_strcmp(buf, "LOGON") == 0) {
  4422. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  4423. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  4424. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  4425. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  4426. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4427. reply_len = -1;
  4428. else
  4429. wpas_request_connection(wpa_s);
  4430. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  4431. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4432. reply_len = -1;
  4433. else if (wpa_s->disconnected)
  4434. wpas_request_connection(wpa_s);
  4435. #ifdef IEEE8021X_EAPOL
  4436. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  4437. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  4438. reply_len = -1;
  4439. #endif /* IEEE8021X_EAPOL */
  4440. #ifdef CONFIG_PEERKEY
  4441. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  4442. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  4443. reply_len = -1;
  4444. #endif /* CONFIG_PEERKEY */
  4445. #ifdef CONFIG_IEEE80211R
  4446. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  4447. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  4448. reply_len = -1;
  4449. #endif /* CONFIG_IEEE80211R */
  4450. #ifdef CONFIG_WPS
  4451. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  4452. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  4453. if (res == -2) {
  4454. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4455. reply_len = 17;
  4456. } else if (res)
  4457. reply_len = -1;
  4458. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  4459. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  4460. if (res == -2) {
  4461. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4462. reply_len = 17;
  4463. } else if (res)
  4464. reply_len = -1;
  4465. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  4466. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  4467. reply,
  4468. reply_size);
  4469. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  4470. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  4471. wpa_s, buf + 14, reply, reply_size);
  4472. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  4473. if (wpas_wps_cancel(wpa_s))
  4474. reply_len = -1;
  4475. #ifdef CONFIG_WPS_NFC
  4476. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  4477. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  4478. reply_len = -1;
  4479. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  4480. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  4481. reply_len = -1;
  4482. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  4483. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  4484. wpa_s, buf + 21, reply, reply_size);
  4485. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  4486. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  4487. wpa_s, buf + 14, reply, reply_size);
  4488. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  4489. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  4490. buf + 17))
  4491. reply_len = -1;
  4492. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  4493. reply_len = wpas_ctrl_nfc_get_handover_req(
  4494. wpa_s, buf + 21, reply, reply_size);
  4495. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  4496. reply_len = wpas_ctrl_nfc_get_handover_sel(
  4497. wpa_s, buf + 21, reply, reply_size);
  4498. } else if (os_strncmp(buf, "NFC_RX_HANDOVER_REQ ", 20) == 0) {
  4499. reply_len = wpas_ctrl_nfc_rx_handover_req(
  4500. wpa_s, buf + 20, reply, reply_size);
  4501. } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
  4502. if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
  4503. reply_len = -1;
  4504. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  4505. if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
  4506. reply_len = -1;
  4507. #endif /* CONFIG_WPS_NFC */
  4508. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  4509. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  4510. reply_len = -1;
  4511. #ifdef CONFIG_AP
  4512. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  4513. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  4514. wpa_s, buf + 11, reply, reply_size);
  4515. #endif /* CONFIG_AP */
  4516. #ifdef CONFIG_WPS_ER
  4517. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  4518. if (wpas_wps_er_start(wpa_s, NULL))
  4519. reply_len = -1;
  4520. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  4521. if (wpas_wps_er_start(wpa_s, buf + 13))
  4522. reply_len = -1;
  4523. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  4524. if (wpas_wps_er_stop(wpa_s))
  4525. reply_len = -1;
  4526. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  4527. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  4528. reply_len = -1;
  4529. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  4530. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  4531. if (ret == -2) {
  4532. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4533. reply_len = 17;
  4534. } else if (ret == -3) {
  4535. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  4536. reply_len = 18;
  4537. } else if (ret == -4) {
  4538. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  4539. reply_len = 20;
  4540. } else if (ret)
  4541. reply_len = -1;
  4542. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  4543. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  4544. reply_len = -1;
  4545. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  4546. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  4547. buf + 18))
  4548. reply_len = -1;
  4549. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  4550. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  4551. reply_len = -1;
  4552. #ifdef CONFIG_WPS_NFC
  4553. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  4554. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  4555. wpa_s, buf + 24, reply, reply_size);
  4556. #endif /* CONFIG_WPS_NFC */
  4557. #endif /* CONFIG_WPS_ER */
  4558. #endif /* CONFIG_WPS */
  4559. #ifdef CONFIG_IBSS_RSN
  4560. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  4561. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  4562. reply_len = -1;
  4563. #endif /* CONFIG_IBSS_RSN */
  4564. #ifdef CONFIG_P2P
  4565. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  4566. if (p2p_ctrl_find(wpa_s, buf + 9))
  4567. reply_len = -1;
  4568. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  4569. if (p2p_ctrl_find(wpa_s, ""))
  4570. reply_len = -1;
  4571. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  4572. wpas_p2p_stop_find(wpa_s);
  4573. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  4574. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  4575. reply_size);
  4576. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  4577. if (p2p_ctrl_listen(wpa_s, buf + 11))
  4578. reply_len = -1;
  4579. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  4580. if (p2p_ctrl_listen(wpa_s, ""))
  4581. reply_len = -1;
  4582. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  4583. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  4584. reply_len = -1;
  4585. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  4586. if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
  4587. reply_len = -1;
  4588. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  4589. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  4590. reply_len = -1;
  4591. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  4592. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  4593. reply_len = -1;
  4594. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  4595. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  4596. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  4597. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  4598. reply_size);
  4599. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  4600. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  4601. reply_len = -1;
  4602. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  4603. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  4604. reply_len = -1;
  4605. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  4606. wpas_p2p_sd_service_update(wpa_s);
  4607. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  4608. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  4609. reply_len = -1;
  4610. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  4611. wpas_p2p_service_flush(wpa_s);
  4612. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  4613. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  4614. reply_len = -1;
  4615. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  4616. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  4617. reply_len = -1;
  4618. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  4619. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  4620. reply_len = -1;
  4621. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  4622. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  4623. reply_len = -1;
  4624. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  4625. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  4626. reply_size);
  4627. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  4628. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  4629. reply_len = -1;
  4630. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  4631. p2p_ctrl_flush(wpa_s);
  4632. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  4633. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  4634. reply_len = -1;
  4635. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  4636. if (wpas_p2p_cancel(wpa_s))
  4637. reply_len = -1;
  4638. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  4639. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  4640. reply_len = -1;
  4641. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  4642. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  4643. reply_len = -1;
  4644. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  4645. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  4646. reply_len = -1;
  4647. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  4648. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  4649. reply_len = -1;
  4650. } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
  4651. if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
  4652. reply_len = -1;
  4653. #endif /* CONFIG_P2P */
  4654. #ifdef CONFIG_WIFI_DISPLAY
  4655. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  4656. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  4657. reply_len = -1;
  4658. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  4659. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  4660. reply, reply_size);
  4661. #endif /* CONFIG_WIFI_DISPLAY */
  4662. #ifdef CONFIG_INTERWORKING
  4663. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  4664. if (interworking_fetch_anqp(wpa_s) < 0)
  4665. reply_len = -1;
  4666. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  4667. interworking_stop_fetch_anqp(wpa_s);
  4668. } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
  4669. if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
  4670. NULL) < 0)
  4671. reply_len = -1;
  4672. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  4673. if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
  4674. reply_len = -1;
  4675. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  4676. if (get_anqp(wpa_s, buf + 9) < 0)
  4677. reply_len = -1;
  4678. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  4679. if (gas_request(wpa_s, buf + 12) < 0)
  4680. reply_len = -1;
  4681. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  4682. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  4683. reply_size);
  4684. #endif /* CONFIG_INTERWORKING */
  4685. #ifdef CONFIG_HS20
  4686. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  4687. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  4688. reply_len = -1;
  4689. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  4690. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  4691. reply_len = -1;
  4692. #endif /* CONFIG_HS20 */
  4693. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  4694. {
  4695. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  4696. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  4697. reply_len = -1;
  4698. else {
  4699. /*
  4700. * Notify response from timeout to allow the control
  4701. * interface response to be sent first.
  4702. */
  4703. eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
  4704. wpa_s, NULL);
  4705. }
  4706. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  4707. if (wpa_supplicant_reload_configuration(wpa_s))
  4708. reply_len = -1;
  4709. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  4710. wpa_supplicant_terminate_proc(wpa_s->global);
  4711. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  4712. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  4713. reply_len = -1;
  4714. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  4715. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  4716. wpa_s, buf + 9, reply, reply_size);
  4717. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  4718. reply_len = wpa_supplicant_ctrl_iface_log_level(
  4719. wpa_s, buf + 9, reply, reply_size);
  4720. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  4721. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  4722. wpa_s, reply, reply_size);
  4723. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  4724. #ifdef CONFIG_SME
  4725. wpa_s->sme.prev_bssid_set = 0;
  4726. #endif /* CONFIG_SME */
  4727. wpa_s->reassociate = 0;
  4728. wpa_s->disconnected = 1;
  4729. wpa_supplicant_cancel_sched_scan(wpa_s);
  4730. wpa_supplicant_cancel_scan(wpa_s);
  4731. wpa_supplicant_deauthenticate(wpa_s,
  4732. WLAN_REASON_DEAUTH_LEAVING);
  4733. } else if (os_strcmp(buf, "SCAN") == 0 ||
  4734. os_strncmp(buf, "SCAN ", 5) == 0) {
  4735. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4736. reply_len = -1;
  4737. else {
  4738. if (os_strlen(buf) > 4 &&
  4739. os_strncasecmp(buf + 5, "TYPE=ONLY", 9) == 0)
  4740. wpa_s->scan_res_handler = scan_only_handler;
  4741. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  4742. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  4743. (wpa_s->wpa_state == WPA_COMPLETED))) {
  4744. wpa_s->normal_scans = 0;
  4745. wpa_s->scan_req = MANUAL_SCAN_REQ;
  4746. wpa_s->after_wps = 0;
  4747. wpa_s->known_wps_freq = 0;
  4748. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4749. } else if (wpa_s->sched_scanning) {
  4750. wpa_printf(MSG_DEBUG, "Stop ongoing "
  4751. "sched_scan to allow requested "
  4752. "full scan to proceed");
  4753. wpa_supplicant_cancel_sched_scan(wpa_s);
  4754. wpa_s->scan_req = MANUAL_SCAN_REQ;
  4755. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4756. } else {
  4757. wpa_printf(MSG_DEBUG, "Ongoing scan action - "
  4758. "reject new request");
  4759. reply_len = os_snprintf(reply, reply_size,
  4760. "FAIL-BUSY\n");
  4761. }
  4762. }
  4763. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  4764. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  4765. wpa_s, reply, reply_size);
  4766. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  4767. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  4768. reply_len = -1;
  4769. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  4770. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  4771. reply_len = -1;
  4772. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  4773. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  4774. reply_len = -1;
  4775. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  4776. reply_len = wpa_supplicant_ctrl_iface_add_network(
  4777. wpa_s, reply, reply_size);
  4778. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  4779. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  4780. reply_len = -1;
  4781. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  4782. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  4783. reply_len = -1;
  4784. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  4785. reply_len = wpa_supplicant_ctrl_iface_get_network(
  4786. wpa_s, buf + 12, reply, reply_size);
  4787. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  4788. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  4789. wpa_s, reply, reply_size);
  4790. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  4791. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  4792. wpa_s, reply, reply_size);
  4793. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  4794. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  4795. reply_len = -1;
  4796. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  4797. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  4798. reply_len = -1;
  4799. #ifndef CONFIG_NO_CONFIG_WRITE
  4800. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  4801. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  4802. reply_len = -1;
  4803. #endif /* CONFIG_NO_CONFIG_WRITE */
  4804. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  4805. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  4806. wpa_s, buf + 15, reply, reply_size);
  4807. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  4808. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  4809. reply_len = -1;
  4810. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  4811. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  4812. reply_len = -1;
  4813. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  4814. reply_len = wpa_supplicant_global_iface_list(
  4815. wpa_s->global, reply, reply_size);
  4816. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  4817. reply_len = wpa_supplicant_global_iface_interfaces(
  4818. wpa_s->global, reply, reply_size);
  4819. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  4820. reply_len = wpa_supplicant_ctrl_iface_bss(
  4821. wpa_s, buf + 4, reply, reply_size);
  4822. #ifdef CONFIG_AP
  4823. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  4824. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  4825. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  4826. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  4827. reply_size);
  4828. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  4829. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  4830. reply_size);
  4831. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  4832. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  4833. reply_len = -1;
  4834. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  4835. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  4836. reply_len = -1;
  4837. #endif /* CONFIG_AP */
  4838. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  4839. wpas_notify_suspend(wpa_s->global);
  4840. } else if (os_strcmp(buf, "RESUME") == 0) {
  4841. wpas_notify_resume(wpa_s->global);
  4842. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  4843. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  4844. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  4845. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  4846. reply_len = -1;
  4847. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  4848. if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
  4849. reply_len = -1;
  4850. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  4851. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  4852. reply_len = -1;
  4853. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  4854. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  4855. buf + 17))
  4856. reply_len = -1;
  4857. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  4858. if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
  4859. reply_len = -1;
  4860. #ifdef CONFIG_TDLS
  4861. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  4862. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  4863. reply_len = -1;
  4864. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  4865. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  4866. reply_len = -1;
  4867. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  4868. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  4869. reply_len = -1;
  4870. #endif /* CONFIG_TDLS */
  4871. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  4872. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  4873. reply_size);
  4874. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  4875. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  4876. reply_size);
  4877. #ifdef CONFIG_AUTOSCAN
  4878. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  4879. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  4880. reply_len = -1;
  4881. #endif /* CONFIG_AUTOSCAN */
  4882. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  4883. pmksa_cache_clear_current(wpa_s->wpa);
  4884. eapol_sm_request_reauth(wpa_s->eapol);
  4885. #ifdef CONFIG_WNM
  4886. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  4887. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  4888. reply_len = -1;
  4889. } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
  4890. if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
  4891. reply_len = -1;
  4892. #endif /* CONFIG_WNM */
  4893. } else if (os_strcmp(buf, "FLUSH") == 0) {
  4894. wpa_supplicant_ctrl_iface_flush(wpa_s);
  4895. } else {
  4896. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  4897. reply_len = 16;
  4898. }
  4899. if (reply_len < 0) {
  4900. os_memcpy(reply, "FAIL\n", 5);
  4901. reply_len = 5;
  4902. }
  4903. *resp_len = reply_len;
  4904. return reply;
  4905. }
  4906. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  4907. char *cmd)
  4908. {
  4909. struct wpa_interface iface;
  4910. char *pos;
  4911. /*
  4912. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  4913. * TAB<bridge_ifname>
  4914. */
  4915. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  4916. os_memset(&iface, 0, sizeof(iface));
  4917. do {
  4918. iface.ifname = pos = cmd;
  4919. pos = os_strchr(pos, '\t');
  4920. if (pos)
  4921. *pos++ = '\0';
  4922. if (iface.ifname[0] == '\0')
  4923. return -1;
  4924. if (pos == NULL)
  4925. break;
  4926. iface.confname = pos;
  4927. pos = os_strchr(pos, '\t');
  4928. if (pos)
  4929. *pos++ = '\0';
  4930. if (iface.confname[0] == '\0')
  4931. iface.confname = NULL;
  4932. if (pos == NULL)
  4933. break;
  4934. iface.driver = pos;
  4935. pos = os_strchr(pos, '\t');
  4936. if (pos)
  4937. *pos++ = '\0';
  4938. if (iface.driver[0] == '\0')
  4939. iface.driver = NULL;
  4940. if (pos == NULL)
  4941. break;
  4942. iface.ctrl_interface = pos;
  4943. pos = os_strchr(pos, '\t');
  4944. if (pos)
  4945. *pos++ = '\0';
  4946. if (iface.ctrl_interface[0] == '\0')
  4947. iface.ctrl_interface = NULL;
  4948. if (pos == NULL)
  4949. break;
  4950. iface.driver_param = pos;
  4951. pos = os_strchr(pos, '\t');
  4952. if (pos)
  4953. *pos++ = '\0';
  4954. if (iface.driver_param[0] == '\0')
  4955. iface.driver_param = NULL;
  4956. if (pos == NULL)
  4957. break;
  4958. iface.bridge_ifname = pos;
  4959. pos = os_strchr(pos, '\t');
  4960. if (pos)
  4961. *pos++ = '\0';
  4962. if (iface.bridge_ifname[0] == '\0')
  4963. iface.bridge_ifname = NULL;
  4964. if (pos == NULL)
  4965. break;
  4966. } while (0);
  4967. if (wpa_supplicant_get_iface(global, iface.ifname))
  4968. return -1;
  4969. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  4970. }
  4971. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  4972. char *cmd)
  4973. {
  4974. struct wpa_supplicant *wpa_s;
  4975. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  4976. wpa_s = wpa_supplicant_get_iface(global, cmd);
  4977. if (wpa_s == NULL)
  4978. return -1;
  4979. return wpa_supplicant_remove_iface(global, wpa_s, 0);
  4980. }
  4981. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  4982. {
  4983. struct wpa_interface_info *prev;
  4984. while (iface) {
  4985. prev = iface;
  4986. iface = iface->next;
  4987. os_free(prev->ifname);
  4988. os_free(prev->desc);
  4989. os_free(prev);
  4990. }
  4991. }
  4992. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  4993. char *buf, int len)
  4994. {
  4995. int i, res;
  4996. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  4997. char *pos, *end;
  4998. for (i = 0; wpa_drivers[i]; i++) {
  4999. struct wpa_driver_ops *drv = wpa_drivers[i];
  5000. if (drv->get_interfaces == NULL)
  5001. continue;
  5002. tmp = drv->get_interfaces(global->drv_priv[i]);
  5003. if (tmp == NULL)
  5004. continue;
  5005. if (last == NULL)
  5006. iface = last = tmp;
  5007. else
  5008. last->next = tmp;
  5009. while (last->next)
  5010. last = last->next;
  5011. }
  5012. pos = buf;
  5013. end = buf + len;
  5014. for (tmp = iface; tmp; tmp = tmp->next) {
  5015. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  5016. tmp->drv_name, tmp->ifname,
  5017. tmp->desc ? tmp->desc : "");
  5018. if (res < 0 || res >= end - pos) {
  5019. *pos = '\0';
  5020. break;
  5021. }
  5022. pos += res;
  5023. }
  5024. wpa_free_iface_info(iface);
  5025. return pos - buf;
  5026. }
  5027. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  5028. char *buf, int len)
  5029. {
  5030. int res;
  5031. char *pos, *end;
  5032. struct wpa_supplicant *wpa_s;
  5033. wpa_s = global->ifaces;
  5034. pos = buf;
  5035. end = buf + len;
  5036. while (wpa_s) {
  5037. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  5038. if (res < 0 || res >= end - pos) {
  5039. *pos = '\0';
  5040. break;
  5041. }
  5042. pos += res;
  5043. wpa_s = wpa_s->next;
  5044. }
  5045. return pos - buf;
  5046. }
  5047. static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
  5048. const char *ifname,
  5049. char *cmd, size_t *resp_len)
  5050. {
  5051. struct wpa_supplicant *wpa_s;
  5052. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5053. if (os_strcmp(ifname, wpa_s->ifname) == 0)
  5054. break;
  5055. }
  5056. if (wpa_s == NULL) {
  5057. char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
  5058. if (resp)
  5059. *resp_len = os_strlen(resp);
  5060. else
  5061. *resp_len = 1;
  5062. return resp;
  5063. }
  5064. return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
  5065. }
  5066. static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
  5067. char *buf, size_t *resp_len)
  5068. {
  5069. #ifdef CONFIG_P2P
  5070. static const char * cmd[] = {
  5071. "P2P_FIND",
  5072. "P2P_STOP_FIND",
  5073. "P2P_LISTEN",
  5074. "P2P_GROUP_ADD",
  5075. "P2P_GET_PASSPHRASE",
  5076. "P2P_SERVICE_UPDATE",
  5077. "P2P_SERVICE_FLUSH",
  5078. "P2P_FLUSH",
  5079. "P2P_CANCEL",
  5080. "P2P_PRESENCE_REQ",
  5081. "P2P_EXT_LISTEN",
  5082. NULL
  5083. };
  5084. static const char * prefix[] = {
  5085. "P2P_FIND ",
  5086. "P2P_CONNECT ",
  5087. "P2P_LISTEN ",
  5088. "P2P_GROUP_REMOVE ",
  5089. "P2P_GROUP_ADD ",
  5090. "P2P_PROV_DISC ",
  5091. "P2P_SERV_DISC_REQ ",
  5092. "P2P_SERV_DISC_CANCEL_REQ ",
  5093. "P2P_SERV_DISC_RESP ",
  5094. "P2P_SERV_DISC_EXTERNAL ",
  5095. "P2P_SERVICE_ADD ",
  5096. "P2P_SERVICE_DEL ",
  5097. "P2P_REJECT ",
  5098. "P2P_INVITE ",
  5099. "P2P_PEER ",
  5100. "P2P_SET ",
  5101. "P2P_UNAUTHORIZE ",
  5102. "P2P_PRESENCE_REQ ",
  5103. "P2P_EXT_LISTEN ",
  5104. "P2P_REMOVE_CLIENT ",
  5105. NULL
  5106. };
  5107. int found = 0;
  5108. int i;
  5109. if (global->p2p_init_wpa_s == NULL)
  5110. return NULL;
  5111. for (i = 0; !found && cmd[i]; i++) {
  5112. if (os_strcmp(buf, cmd[i]) == 0)
  5113. found = 1;
  5114. }
  5115. for (i = 0; !found && prefix[i]; i++) {
  5116. if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
  5117. found = 1;
  5118. }
  5119. if (found)
  5120. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  5121. buf, resp_len);
  5122. #endif /* CONFIG_P2P */
  5123. return NULL;
  5124. }
  5125. static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
  5126. char *buf, size_t *resp_len)
  5127. {
  5128. #ifdef CONFIG_WIFI_DISPLAY
  5129. if (global->p2p_init_wpa_s == NULL)
  5130. return NULL;
  5131. if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
  5132. os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
  5133. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  5134. buf, resp_len);
  5135. #endif /* CONFIG_WIFI_DISPLAY */
  5136. return NULL;
  5137. }
  5138. static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
  5139. char *buf, size_t *resp_len)
  5140. {
  5141. char *ret;
  5142. ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
  5143. if (ret)
  5144. return ret;
  5145. ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
  5146. if (ret)
  5147. return ret;
  5148. return NULL;
  5149. }
  5150. static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
  5151. {
  5152. char *value;
  5153. value = os_strchr(cmd, ' ');
  5154. if (value == NULL)
  5155. return -1;
  5156. *value++ = '\0';
  5157. wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
  5158. #ifdef CONFIG_WIFI_DISPLAY
  5159. if (os_strcasecmp(cmd, "wifi_display") == 0) {
  5160. wifi_display_enable(global, !!atoi(value));
  5161. return 0;
  5162. }
  5163. #endif /* CONFIG_WIFI_DISPLAY */
  5164. return -1;
  5165. }
  5166. #ifndef CONFIG_NO_CONFIG_WRITE
  5167. static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
  5168. {
  5169. int ret = 0;
  5170. struct wpa_supplicant *wpa_s;
  5171. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5172. if (!wpa_s->conf->update_config) {
  5173. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
  5174. continue;
  5175. }
  5176. if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  5177. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
  5178. ret = 1;
  5179. } else {
  5180. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
  5181. }
  5182. }
  5183. return ret;
  5184. }
  5185. #endif /* CONFIG_NO_CONFIG_WRITE */
  5186. static int wpas_global_ctrl_iface_status(struct wpa_global *global,
  5187. char *buf, size_t buflen)
  5188. {
  5189. char *pos, *end;
  5190. int ret;
  5191. struct wpa_supplicant *wpa_s;
  5192. pos = buf;
  5193. end = buf + buflen;
  5194. #ifdef CONFIG_P2P
  5195. if (global->p2p && !global->p2p_disabled) {
  5196. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  5197. "\n"
  5198. "p2p_state=%s\n",
  5199. MAC2STR(global->p2p_dev_addr),
  5200. p2p_get_state_txt(global->p2p));
  5201. if (ret < 0 || ret >= end - pos)
  5202. return pos - buf;
  5203. pos += ret;
  5204. } else if (global->p2p) {
  5205. ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
  5206. if (ret < 0 || ret >= end - pos)
  5207. return pos - buf;
  5208. pos += ret;
  5209. }
  5210. #endif /* CONFIG_P2P */
  5211. #ifdef CONFIG_WIFI_DISPLAY
  5212. ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
  5213. !!global->wifi_display);
  5214. if (ret < 0 || ret >= end - pos)
  5215. return pos - buf;
  5216. pos += ret;
  5217. #endif /* CONFIG_WIFI_DISPLAY */
  5218. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5219. ret = os_snprintf(pos, end - pos, "ifname=%s\n"
  5220. "address=" MACSTR "\n",
  5221. wpa_s->ifname, MAC2STR(wpa_s->own_addr));
  5222. if (ret < 0 || ret >= end - pos)
  5223. return pos - buf;
  5224. pos += ret;
  5225. }
  5226. return pos - buf;
  5227. }
  5228. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  5229. char *buf, size_t *resp_len)
  5230. {
  5231. char *reply;
  5232. const int reply_size = 2048;
  5233. int reply_len;
  5234. int level = MSG_DEBUG;
  5235. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  5236. char *pos = os_strchr(buf + 7, ' ');
  5237. if (pos) {
  5238. *pos++ = '\0';
  5239. return wpas_global_ctrl_iface_ifname(global,
  5240. buf + 7, pos,
  5241. resp_len);
  5242. }
  5243. }
  5244. reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
  5245. if (reply)
  5246. return reply;
  5247. if (os_strcmp(buf, "PING") == 0)
  5248. level = MSG_EXCESSIVE;
  5249. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  5250. (const u8 *) buf, os_strlen(buf));
  5251. reply = os_malloc(reply_size);
  5252. if (reply == NULL) {
  5253. *resp_len = 1;
  5254. return NULL;
  5255. }
  5256. os_memcpy(reply, "OK\n", 3);
  5257. reply_len = 3;
  5258. if (os_strcmp(buf, "PING") == 0) {
  5259. os_memcpy(reply, "PONG\n", 5);
  5260. reply_len = 5;
  5261. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  5262. if (wpa_supplicant_global_iface_add(global, buf + 14))
  5263. reply_len = -1;
  5264. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  5265. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  5266. reply_len = -1;
  5267. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  5268. reply_len = wpa_supplicant_global_iface_list(
  5269. global, reply, reply_size);
  5270. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  5271. reply_len = wpa_supplicant_global_iface_interfaces(
  5272. global, reply, reply_size);
  5273. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  5274. wpa_supplicant_terminate_proc(global);
  5275. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  5276. wpas_notify_suspend(global);
  5277. } else if (os_strcmp(buf, "RESUME") == 0) {
  5278. wpas_notify_resume(global);
  5279. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  5280. if (wpas_global_ctrl_iface_set(global, buf + 4))
  5281. reply_len = -1;
  5282. #ifndef CONFIG_NO_CONFIG_WRITE
  5283. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  5284. if (wpas_global_ctrl_iface_save_config(global))
  5285. reply_len = -1;
  5286. #endif /* CONFIG_NO_CONFIG_WRITE */
  5287. } else if (os_strcmp(buf, "STATUS") == 0) {
  5288. reply_len = wpas_global_ctrl_iface_status(global, reply,
  5289. reply_size);
  5290. } else {
  5291. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  5292. reply_len = 16;
  5293. }
  5294. if (reply_len < 0) {
  5295. os_memcpy(reply, "FAIL\n", 5);
  5296. reply_len = 5;
  5297. }
  5298. *resp_len = reply_len;
  5299. return reply;
  5300. }