ctrl_iface.c 177 KB

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