ctrl_iface.c 145 KB

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